Retrieving BLOB from DB
I'm having difficulties retrieving a BLOB. Below is the code used to insert the BLOB and my attempt to retrieve it and create an Image object with it. The Image Object isn't being created with the correct data from the BLOB.
How should I be retrieving the BLOB based on how it was inserted into the table? It looks like I'm getting data from the Select statement, but it's just not in the correct format.
// BLOB Insert Code.
String str = "A lot of data"; // The data is a .gif Object in a String Object.
String tmpStr = new String( Base64.decode(str) );
// COL1 is defined as a BLOB(40k) column in the database.
String sql = "INSERT INTO S.TEST_TABLE ( COL1 ) VALUES (?)";
...
PreparedStatement ps = conn.prepareStatement( sql );
ByteArrayInputStream sIn = new ByteArrayInputStream( tmpStr.getBytes() );
ps.setBinaryStream( 1, (InputStream)sIn, tmpStr.length() );
ps.executeUpdate();
conn.commit();
// BLOB Retrieve Code.
String sql2 = "SELECT COL1 FROM S.TEST_TABLE";
Blob blob = null;
Long len = 0;
String tmp = null;
Image img = null;
ResultSet rs = stmt.executeQuery( sql2 );
while ( rs.next() )
{
blob = rs.getBlob("COL1");
len = blob.length();
tmp = new String ( ( blob.getBytes(1, (int)len)) );
img = Image.getInstance( tmp.getBytes() );
...
}
Thank you for the help.
Message was edited by:
mborucki
Message was edited by:
mborucki

