Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java将图片上载到Mysql数据库_Java_Mysql_Spring Jdbc - Fatal编程技术网

使用java将图片上载到Mysql数据库

使用java将图片上载到Mysql数据库,java,mysql,spring-jdbc,Java,Mysql,Spring Jdbc,我的文件选择器代码和将图片转换为二进制: Connection cn; Statement st; PreparedStatement pstmt=null; PreparedStatement pst; ResultSet rs; Object fname, mname, lname, bdate, nation, statusq,InstNo, photo, combo, place, mimi; int status; p

我的文件选择器代码和将图片转换为二进制:

  Connection cn;
    Statement st;
    PreparedStatement pstmt=null;
    PreparedStatement pst;
    ResultSet rs;
    Object fname, mname, lname, bdate, nation, statusq,InstNo,  photo, combo, place, mimi; 
    int status;



   private void btnNextMouseClicked(java.awt.event.MouseEvent evt) {                                     

        fname=txtFirtsName.getText();
        lname=txtLastName.getText();
        mname=txtMiddleName.getText();
        InstNo=txtInstituteNo.getText();
        place=txtPlacBirth.getText();

        //photo=txtPicturePath.getText();
        status=combostatus.getSelectedIndex();

        Object dave=((JTextField)chooserBirthDate.getDateEditor().getUiComponent()).getText();
        Object isa=combonation.getSelectedItem();

        Object photo=pictureName.getClass();
       // pst.setBytes();
        // bdate=((JTextField)chooserBirthDate.getDateEditor().getUiComponent()).getText();


        if(status==1){
            statusq=("In Active");
    }
        else{
            statusq="Active";}

        try{


       String addrecords="insert into brothers(FirstName, MiddleName, LastName, BirthDate, BirthPlace, Nationality, InstituteNumber, Status, Picture) values('"+
        fname +"', '" +
        mname +"', '" +
        lname +"', '" +
        dave +"', '" +
        place +"', '" +
        isa +"', '" +
        InstNo +"', '" +
        statusq +"', '" +
        photo +"')"; 

        //wrapField();

        st.executeUpdate(addrecords);
        }
我在代码底部声明了以下变量:

 private void btnFileChooserActionPerformed(java.awt.event.ActionEvent evt) {                                               
        JFileChooser izoChooser=new JFileChooser();
        izoChooser.showOpenDialog(null);
        File pictureBrother=izoChooser.getSelectedFile();
        pictureName=pictureBrother.getAbsolutePath();
        txtPicturePath.setText(pictureName);

        try {

          File image=new File(pictureName);
          FileInputStream fis=new FileInputStream(image);

          ByteArrayOutputStream bos=new ByteArrayOutputStream();
          byte[] buf=new byte[1024];

          for(int readNum; (readNum=fis.read(buf))!=-1;){

              bos.write(buf,0,readNum);

          }
           person_image=bos.toByteArray();
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
            //e.printStackTrace();
        }
问:我的代码没有抛出错误,但在数据库中,无论我选择哪幅图片,它在图片的BLOB列上只注册了8B。但是,如果直接从数据库上传图片,先转到数据库,然后再上传,图片就会上传到数据库。可能是什么问题

我的主要问题是:我想,objectphoto=pictureName.getClass()

应该是什么,因为如果我使用事先准备好的声明,那么它将是: 已准备好的语句设置字节(10,人像)

但是没有.getBytes()

这将只为您提供
pictureName
变量的类对象。我怀疑那是你想要的。如果要使用JDBC插入BLOB,必须处理底层字节流。我建议您阅读以下内容:


哦,还有,请使用
PreparedStatement
s.

PreparedStatement失败了,没有明显的原因,我的朋友,请看下面的链接:你能给我一个明确的指示吗(用我的风格,PreparedStatement让我的项目拖得很长,没有解决方案),对javaI有点陌生我看到了很多原因,为什么准备好的语句不起作用,但这与这个问题无关。请告诉我这是我的代码:你的另一个问题已经有很多很好的答案了。你应该在那里继续讨论。这个问题是关于斑点的,我已经给了你们一些关于这个特殊问题的建议。
 private javax.swing.JTextField txtTrial;
    // End of variables declaration                   
String pictureName=null;
int s=0;
byte[] person_image=null; 
}
Object photo=pictureName.getClass();