File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC 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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
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
Similar Threads
problem with logic !
Parsing encoded data?
DataOutputStream / WAVE steganography
How to upload a byte array into BLOB column?
Conversion Blob to Byte Array