通过postgres中的bytea数据类型通过SetIcon更新JLabel [英] Updating JLabel via SetIcon from bytea data type in postgres
问题描述
我正在从Wolfram | Alpha检索gif图像。为了最大限度地减少查询,我想存储这些图像,并且只在数据更改时查询W | A,所以我将图像存储为我的 bytea
数据类型postgres db。 保存部分似乎正在起作用,因为有数据。 的System.out.println(rs.getString( fnPlotImg))
产生这样的: \x4275666665726564496d6167654035356437373834323a2074797065203d203120446972656374436f6c6f724d6f64656c3a20726d61736b3d66663030303020676d61736b3d6666303020626d61736b3d666620616d61736b3d3020496e7465676572496e7465726c65617665645261737465723a207769647468203d2032303020686569676874203d20313335202342616e6473203d203320784f6666203d203020794f6666203d203020646174614f66667365745b305d2030
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
我已经能够使用这段代码成功更新W | A中的图像:
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();
}
我的理由是bytea是一个字节数组,getBytes()应该是检索一个字节数组,ImageIcon()应该处理一个字节数组。但是,如果我没有构建一个空指针异常,它就会出错。我认为这是因为我没有正确地将图像保存到DB或者我没有正确地检索它。
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.
推荐答案
我没有安装PostgreSQL,但我认为你应该写/读图像格式而不是 BufferedImage
数据。
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();
}
这篇关于通过postgres中的bytea数据类型通过SetIcon更新JLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!