• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

convert String a Blob

  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
pie. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic