简体   繁体   中英

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. I have a column that actually stores an BLOB image in it. 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?

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); lines. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM