This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to upload a byte array into BLOB column?

 
Steven Ho
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Steven Ho
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jon, I have done my work based on your guidance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic