aspose file tools*
The moose likes JDBC and the fly likes convert String a Blob 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 "convert String a Blob" Watch "convert String a Blob" New topic
Author

convert String a Blob

Jhonnathan Emilio Cardona Saineda
Ranch Hand

Joined: Jan 24, 2012
Posts: 30

hello, thanks in advance.
In the Oracle database, I have a blob type data.
How I can store a String with PreparedStatement using the method setBlob()?
i used this but don't work

byte byte_string[]=string.getBytes();
Blob blob= new SerialBlob(byte_string);
preparedStatement.setBlob(1, blob);

java.lang.ClassCastException: javax.sql.rowset.serial.SerialBlob cannot be cast to oracle.sql.BLOB

Thank you.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3433
    
  47

Well, the first problem is that String contains character data, while BLOB contains binary data. Are you sure you shouldn't be using a CLOB, which is designed to hold characters? You're currently going around this issue by converting the String to bytes, but it entails character set conversion to your default locale and it might not be what you need.

Oracle won't accept a Blob or Clob created using the new operator. This thread contains a code that can be used to store a BLOB or CLOB in Oracle, and references to two other threads.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: convert String a Blob
 
Similar Threads
create a blob and wite blob to browsers
how to write BLOB type of data from java application into an oracle table
Convert String to Blob
Inserting BLOB's in database
How to convert the BLOB into String