2012-05-25 123 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
相关问题