[英]How to retrieve image of type blob from MySQL in JTable using Java?
[英]How to retrieve BLOB image from the database and in which data type?
我有以下结构。 我一起加入一些表来从我的SQL数据库中获取结果。 我有一个实际存储BLOB图像的列。 我想知道如何在变量中检索此信息,然后可以使用该变量在图形用户界面(例如摇摆窗口)中加载此图像。
这是我的代码,我将如何阅读其他 - 字符串 - 数据库的字段。
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.?
}
}
}
最后一个想法是如何使用给定的图片(来自文件系统)将此图像作为BOLB存储在数据库中?
干杯。
我读到了这个,但我认为这个实现无法帮助我。
你去,只需集成你和我的循环:
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();
}
当然记得正确修改路径和InputStream is = resultSet.getBinaryStream(3); 线。 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.