2013-04-29 76 views
0
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); 
     } 

我的文件选择器代码和转换图片为二进制:上传用java Mysql数据库图片

private javax.swing.JTextField txtTrial; 
    // End of variables declaration     
String pictureName=null; 
int s=0; 
byte[] person_image=null; 
} 

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,不会显示我选择的图片。但是,如果直接从数据库上传图片,请转到数据库,然后上传,将图片上传到数据库。可能是什么问题?

我的主要问题在这里:I GUESS,Object photo = pictureName.getClass();

什么应该是因为如果我要使用准备好的陈述,那么它会: preparedStatemnt.setBytes(10,person_image);.

但没有.getBytes();

回答

0
Object photo=pictureName.getClass(); 

这只会给你pictureName变量的类对象。我怀疑这是你想要的。如果要使用JDBC插入BLOB,则必须处理基础字节流。我建议你看看这个:http://docs.oracle.com/javase/tutorial/jdbc/basics/blob.html

哦,请使用PreparedStatement s。

+0

准备声明失败,没有明显的原因我的朋友,请参阅下面的链接:http://stackoverflow.com/questions/16196670/java-sql-sqlexception-parameter-index-out-of-range-2-number-of -parameters-w ...所以你可以pliz给我一个明确的指针(只是使用我的风格,准备好的声明使我的项目拖动没有解决方案),有点java上的newbiew – Isaiah 2013-04-29 10:59:11

+0

我看到很多原因为什么准备好的语句没有工作,但这与这个问题无关。 – NilsH 2013-04-29 11:02:31

+0

请在这里给我看看是我的代码:http://stackoverflow.com/questions/16196670/java-sql-sqlexception-parameter-index-out-of-range-2-number-of-parameters-w – Isaiah 2013-04-29 11:03:24