2013-03-20 27 views
0

我有這段代碼來保存圖像在MySQL中,但它不起作用。保存任何圖像到MySQL作爲Blob

FileInputStream fis = null; 
PreparedStatement ps = null; 
MyDB.con.setAutoCommit(false); 
File pic = new File(txtPicPath.getText().trim()); 
fis = new FileInputStream(pic); 
ps = MyDB.con.prepareStatement("insert into `photo`(`Employee ID`, Picture) values (?, ?)"); 
ps.setString(1, Data.User.getText()); 
ps.setBinaryStream(2, fis, pic.length()); 
ps.executeUpdate(); 
MyDB.con.commit(); 
JOptionPane.showMessageDialog(rootPane, "Upload Successfully"); 
btnPicdelete.setEnabled(true); 
btnBrowse.setEnabled(false); 
btnUpload.setEnabled(false); 
txtPicPass.setText("");  
txtPicPath.setText(""); 

的問題是:

java.io.FileNotFoundException: image1.jpg (The system cannot find the file specified) 

我上傳的圖片名稱爲image1.jpg

+0

錯誤清清楚楚地寫着'系統無法找到該文件specified'。你確定你在指定路徑中有image1.jpg嗎? – 2013-03-20 07:29:00

+0

檢查圖像路徑是否正確,或者你把文件放在正確的目錄下。 – Ankit 2013-03-20 07:31:04

+0

指定文件的完整路徑。同時檢查它是否存在。 – 2013-03-20 07:31:53

回答

0

你需要指定完整路徑的圖像在你的jTextField或ü可以使用getClass().getResource()作爲替代!

例子:

URL url = this.getClass().getResource("../Image/pyPic.jpg"); 
       InputStream is = url.openStream(); 
+0

我應該在哪裏放? – kelvzy 2013-03-20 07:57:13

+0

更新了我的答案! – 2013-03-20 08:04:22

+0

但現在我怎樣才能將該圖像修復成我的jlabel的寬度或大小? @ c.pramod – kelvzy 2013-03-21 02:52:11

0

我的文件名必須是:

txtPicPath.setText(file.getAbsoluteFile().toString())