• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Conversion Blob to Byte Array

 
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to convert a Blob into a Byte array but sometimes when i run the application I get this exception

Blob may not be manipulated from creating session

this is the way i am converting Blob to byte Array



Is there another way to convert a java.sql.Blob into a Byte array
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I've used bobs with the following piece of code. You could adapt it to what you require:

public void setBlob(PreparedStatement pstmt, int index, Object obj) throws DAOException, SQLException {
ByteArrayOutputStream b_out = new ByteArrayOutputStream();
try {
ObjectOutputStream o_out = new ObjectOutputStream(b_out);
o_out.writeObject(obj);
o_out.close();
} catch (IOException e) {
log.error(e);
throw new DAOException(e);
}
byte[] b = b_out.toByteArray();
ByteArrayInputStream b_in = new ByteArrayInputStream(b);


pstmt.setBinaryStream(index, b_in, b.length);
}

this method could be used by your insert or update method like this:
setBlob(pstmt1, 2, myObject);

Cheers
Parham
reply
    Bookmark Topic Watch Topic
  • New Topic