![](/img/trans.png)
[英]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.