File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases 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 Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
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!