• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to upload a byte array into BLOB column?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jon, I have done my work based on your guidance.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic