Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic