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"); 

我知道,它不能工作。请帮我重新编码。

4

1 回答 1

0

第一个错误必须在此:

result = prestmt.executeUpdate();

我担心你已经result用 type声明了ResultSet
并且当您分配int, 的返回类型时executeUpdate()result
显然SQLException会提高 an 。

像这样修改上面的语句:

int insertResult = prestmt.executeUpdate();

你应该在这方面取得成功。

建议

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

available()如果您想read从流中获取内容,请不要依赖方法。
这只是一个估计,并不能保证您可以阅读到流结束。

而是使用该setBinaryStream方法的其他签名,例如: Javadoc 说,,这意味着您无需明确阅读它。 更改后,您的声明将如下所示:
setBinaryStream( int parameterIndex, InsputStream is )
The data will be read from the stream as needed until end-of-file is reached.

prestmt.setBinaryStream(2, fis);


图片
我在 Java GUI 和图片方面工作不多。
但可以建议您参考一些解决方案:

  1. 将图像添加到按钮
  2. 带有图像的标签
于 2012-05-27T06:22:11.473 回答