This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes Conversion Blob to Byte Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Conversion Blob to Byte Array" Watch "Conversion Blob to Byte Array" New topic

Conversion Blob to Byte Array

Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
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
Parham Emami

Joined: Jul 11, 2003
Posts: 15
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);
} catch (IOException 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);

I agree. Here's the link:
subject: Conversion Blob to Byte Array
It's not a secret anymore!