[英]How to retrieve BLOB image from the database and in which data type?
I have the following structure. 我有以下结构。 I am joining some tables together to get a result from my SQL database. 我一起加入一些表来从我的SQL数据库中获取结果。 I have a column that actually stores an BLOB image in it. 我有一个实际存储BLOB图像的列。 I would like to know how can I retrieve this information in a variable that can then be used to load this image in a Graphical User Interface such as swing window. 我想知道如何在变量中检索此信息,然后可以使用该变量在图形用户界面(例如摇摆窗口)中加载此图像。
Here is my code on how I am reading other - String - fields of the database. 这是我的代码,我将如何阅读其他 - 字符串 - 数据库的字段。
ResultSet result = statement.executeQuery(SQLQuery);
ResultSetMetaData metaData = rs.getMetaData();
while (result.next())
{
for (int i = 1; i <= columnsNumber; i++)
{
String AttributeName = metaData.getColumnName(i);
String AttributeValue = result.getString(i);
if(AttributeName == "UserName")
{
String userName = AttributeValue;
}
if(AttributeName == "UserImage")
{
//code here to get the BLOB user Image.?
}
}
}
And lastly an idea of how can, with a given picture (from the filesystem) to store this image as BOLB in the database? 最后一个想法是如何使用给定的图片(来自文件系统)将此图像作为BOLB存储在数据库中?
Cheers. 干杯。
I read about this one but I think this implementation can't help me. 我读到了这个,但我认为这个实现无法帮助我。
There you go, just integrate both your and my loop: 你去,只需集成你和我的循环:
while (resultSet.next()) {
// String name = resultSet.getString(1);
// String description = resultSet.getString(2);
File image = new File("your path");
FileOutputStream fos = new FileOutputStream(image);
byte[] buffer = new byte[1];
InputStream is = resultSet.getBinaryStream(3);
while (is.read(buffer) > 0) {
fos.write(buffer);
}
fos.close();
}
Of course remember to properly modify path and InputStream is = resultSet.getBinaryStream(3); 当然记得正确修改路径和InputStream is = resultSet.getBinaryStream(3); lines. 线。 Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.