2012-05-25 79 views
1

我有一個圖像名爲P100.jpg。我正在調整它,它被轉換成ZP100.png。我通過插入查詢將它存儲到數據庫MySQL中。從數據庫檢索圖像並在標籤中顯示

File imageFile = new File("F:\\POSTERS\\Roses\\ZP100.png"); 
    FileInputStream fis = new FileInputStream(imageFile); 

    String insertImage = "insert into image values(?,?)"; 
    prestmt = con.prepareStatement(insertImage); 
    prestmt.setInt(1,4); 
    prestmt.setBinaryStream(2, fis, fis.available()); 
    result = prestmt.executeUpdate(); 

現在我想檢索該圖像並通過將其分配給標籤來顯示在窗體上。

String selectImage = "select img from image"; 
    prestmt = con.prepareStatement(selectImage); 

但它給我的異常,因爲

java.sql.SQLException: Can not issue executeUpdate() for SELECTs 

對於一個標籤賦予圖像,我有:

image.setText("ZP100.png"); 

我知道,這是行不通的。請幫我重新編碼這個。

+0

任何人都可以請幫我...... – Anjali

+0

檢查我的答案 –

回答

0

第一誤差必須在此:

result = prestmt.executeUpdate(); 

我怕你已經宣佈resultResultSet類型。
當你分配一個intexecuteUpdate()返回類型,以result
明顯的SQLException提高。

修改上面的語句是這樣的:

int insertResult = prestmt.executeUpdate(); 

,你應該得到它成功了。

建議

prestmt.setBinaryStream(2,FIS,fis.available());

,如果你想從流read內容不依賴Øavailable()方法。
這只是一個估計並不能保證你可以閱讀直到流結束。

而是使用setBinaryStream方法類似的其他簽名:
setBinaryStream(int parameterIndex, InsputStream is)
的Javadoc說,The data will be read from the stream as needed until end-of-file is reached.,這意味着你不需要顯式地讀它。
有了變化,你的聲明看起來像:

prestmt。setBinaryStream(2,fis);


圖片
我沒有工作太多關於Java GUI和圖像。
但可以建議你參考一些解決方案在:

  1. Add Image to Button
  2. Label with Image
相關問題