aspose file tools*
The moose likes JDBC and the fly likes How to upload a byte array into BLOB column? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to upload a byte array into BLOB column?" Watch "How to upload a byte array into BLOB column?" New topic
Author

How to upload a byte array into BLOB column?

Steven Ho
Greenhorn

Joined: Jan 16, 2003
Posts: 15
I hava a BLOB column inside sample table. On the other hand I manage to get a byte array (byte[]) from files uploaded by form input field.
Is it possible to upload the byte array into the field? I am trying to add a file into database.
I tried to find a way to convert the byte array into BLOB using
byte[] b = .... populate it with byte data
Blob blob = null;
blob.setBytes(1, b);
This doesn't work because the blob object is null.
Please help, thank you all.
Jon Dornback
Ranch Hand

Joined: Apr 24, 2002
Posts: 137
I solved this by using the following code (try{} blocks ommitted):

The basic idea is that you use a PreparedStatement, which allows you to set a BinaryStream as part of the insert. Then, wrap the byte[] array with a new ByteArrayInputStream, and set that as the binary stream to be inserted.
to get the byte array back out of the database, use this:


use the [CODE] tags - it makes it much easier for people to help you.
Steven Ho
Greenhorn

Joined: Jan 16, 2003
Posts: 15
Thank you Jon, I have done my work based on your guidance.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to upload a byte array into BLOB column?