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

[1545 byte] By [mboruckia] at [2007-10-2 20:15:20]
# 1

I don't think I understand your code. Why are you doing a base 64 decode? Why is your image a string?

// 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) );

I was under the impression that if you base 64 encode something you need to base 64 decode it to get back what you started with and I don't see that here.

I guess I'd first make sure that your code worked when the database is eliminated from the picture.

pthorsona at 2007-7-13 22:57:37 > top of Java-index,Java Essentials,Java Programming...
# 2

Yeah, I'm also a little confused with your code.

Do you mean

String str = object.toString();

where the object is a .gif object?

also, when you get data from the query, you should start in the first row...

with rs.first()

It looks like maybe what you're trying to do is serialize the object and write it out to the db? maybe?

You can also check to make sure your .gif ovj isn't bigger than 40k...

mshiffera at 2007-7-13 22:57:37 > top of Java-index,Java Essentials,Java Programming...