English 中文(简体)
从数据库获取图像并在标签中显示
原标题:Retrieve image from database and display in label

我有一个名为 P100.jpg 的图像。 我正在调整图像大小, 并将其转换为 。 我通过插入查询将它存储到数据库 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"); 

我知道,这行不通,请帮我重新编码

问题回答

第一个错误必须在此位置 :

result = prestmt.executeUpdate();

I fear you have declared result with type ResultSet.
And when you are assigning an int, the return type of executeUpdate(), to result
obviously an SQLException is raised.

修改上面的语句如下:

int insertResult = prestmt.executeUpdate();

并且你应该成功。

推荐 :

prepstmt.setBinaryStream( 2, fis, < generity > fis. comp () );

Don t depend oavailable() method if you want to read content from a stream.
It is just an estimate and won t guarantee you that you can read till the end of stream.

Instead use the other signature of the setBinaryStream method like:
setBinaryStream( int parameterIndex, InsputStream is )
Javadoc says, The data will be read from the stream as needed until end-of-file is reached., meaning you need not read it explicitly.
With the change, your statement would look like:

prepstmt.setBinaryStream( 2, fis) ;


Image:
I have not worked much on Java GUI and images.
But can suggest you to refer to some solutions at:

  1. Add Image to Button
  2. Label with Image




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...