Displaying images from MySQL database on a single column of JTable

前端 未结 1 387
孤街浪徒
孤街浪徒 2020-12-04 04:16

I\'m trying to display images retrieve from MySQL database of blob datatype. Could not figure out what is the problem that causes the image column to display data like this

相关标签:
1条回答
  • 2020-12-04 04:38

    Since you used preparedstatement.setBlob(1, InputStream); to store the image, I have to assume that you stored the physical image file/format and not just the pixel data.

    You need to read back this image format and convert to a supported image format for Swing/Java.

    Start by getting a Blob reference to the database field...

    Blob blob = rs.getBlob(1);
    

    Once you have a Blob, you can use it's binary InputStream and read the data...

    BufferedImage image = null;
    try (InputStream is = blob.getBinaryStream()) {
        image = ImageIO.read(is);
    } catch (IOException exp) {
        exp.printStackTrace();
    }
    

    Now, you can make it an ImageIcon using new ImageIcon(image) and put this within your table model...

    0 讨论(0)
提交回复
热议问题