2013-04-29 108 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