Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
514 views
in Technique[技术] by (71.8m points)

java - Updating JLabel via SetIcon from bytea data type in postgres

I am retrieving gif images from Wolfram|Alpha. In an effort to minimize queries I want to store those images and only query W|A when the data is changed, so I am storing the images as a bytea data type in my postgres db. The "save" portion seems to be working because there is data. System.out.println(rs.getString("fnPlotImg")) yields this: x4275666665726564496d6167654035356437373834323a2074797065203d203120446972656374436f6c6f724d6f64656c3a20726d61736b3d66663030303020676d61736b3d6666303020626d61736b3d666620616d61736b3d3020496e7465676572496e7465726c65617665645261737465723a207769647468203d2032303020686569676874203d20313335202342616e6473203d203320784f6666203d203020794f6666203d203020646174614f66667365745b305d2030

I have been able to successfully update the image from W|A using this bit of code:

String path = ((WAImage) element).getURL();
URL url = new URL(path);
BufferedImage image = ImageIO.read(url);
picLabel.setIcon(new ImageIcon(image));

I would like to update my application with the image from the database and have attempted this code:

byte[] ba = rs.getBytes("fnPlotImg");
try{
    picLabel.setIcon(new ImageIcon(ba));
} catch (NullPointerException e) {
    e.printStackTrace();
}

My rationale is that bytea is a byte array, getBytes() is supposed to retrieve a byte array, and ImageIcon() is supposed to handle a byte array.However, if I don't build in a null pointer exception it errors out. I presume this is because I am not saving the image to DB correctly or I am not retrieving it correctly.

All thoughts are welcome, I'm getting fatigued so I'll check in the morning with fresh eyes.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't have a installation of PostgreSQL available, but I think you should be writing/reading the image format and not the BufferedImage data.

For example, writing might look something like...

Connection con = ...;
BufferedImage img = ...;
try (PreparedStatement stmt = con.prepareStatement("insert into tableofimages (image) values (?)")) {
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        ImageIO.write(img, "png", baos);
        try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) {
            stmt.setBinaryStream(1, bais);
            int rows = stmt.executeUpdate();
            System.out.println(rows + " rows updated");
        }
    }
} catch (SQLException | IOException exp) {
    exp.printStackTrace();
}

And reading might look something like...

Connection con = ...;
try (PreparedStatement stmt = con.prepareStatement("select image from tableofimages")) {
    try (ResultSet rs = stmt.executeQuery()) {
        while (rs.next()) {
            try (InputStream is = rs.getBinaryStream(1)) {
                BufferedImage img = ImageIO.read(is);
            }
        }
    }
} catch (SQLException | IOException exp) {
    exp.printStackTrace();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...