Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.ClassCastException: java.lang.String cannot be cast to oracle.sql.BLOB

 
Seth A Jackson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all. I am a first time poster, but have found coderanch to be very helpful the last couple of months, thanks. My question concerns insertion of a String into a Db BLOB field. My original code was thus:

Blob att_blob;
String att_pdfb = <a large string coming from xml>;
byte[] att_bytes;
PreparedStatement updt_att_st = dbConn.prepareStatement("UPDATE Attachment SET update_date_time = SYSDATE, contents_raw_data_blob = ? WHERE application_id =? AND file_name = ? AND attachment_type_code = ?");

att_bytes = att_pdfb.getBytes(); ***
att_blob = dbConn.createBlob(); ***
att_blob.setBytes(1, att_bytes); ***
updt_att_st.setBlob(1, att_blob); ***
<set remaining PS variables>

and this worked fine, but I wanted to consolidate the setting of the BLOB field in the Prepared Statement, and so I decided to replace the lines marked *** with:

updt_att_st.setObject(1, att_pdfb, java.sql.Types.BLOB);

At runtime I get this:

java.lang.ClassCastException: java.lang.String cannot be cast to oracle.sql.BLOB
at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:8864)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:8396)
at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:8980)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:242)

Could someone explain this to me? I have looked around on the web, I see that others have encountered this, but I can't seem to determine if this is a bug, and if so, it's been resolved, or if perhaps I need an updated ojdbc.jar. I am currently using ojdbc6.jar. I don't see a programming error - not that there isn't one there! This is not terribly important, I was just trying to avoid charset/encoding issues with the conversion from String to bytes, by inserting the String directly into the Db as BLOB.

I should note that I use setObject throughout my app with types DOUBLE & INTEGER, with no issues.

Thanks for the help. God bless. Seth

 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A String is not a BLOB.
setObject attempts to find a setter that maps the input Type with the SQL datatype.
In this case there is no match for String and Blob.

Seth A Jackson wrote:I should note that I use setObject throughout my app with types DOUBLE & INTEGER, with no issues.


In these case presumably the types passed in were doubles and integers respectively?
 
Seth A Jackson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave. Thanks for the reply. I agree that a String is not a BLOB, but I figure since a String inherits from java.lang.Object, it is an object, and so I figure the setObject should work. And incidentally, the instances where I use setObject with DOUBLE & INTEGER, I am still trying to map a string in there, with no problem. Here's an example: CS.setObject(235, msg_map_data.get("application_fee"), java.sql.Types.DOUBLE); where msg_map_data contains strings for both the keys & the values. Granted, I'm using this with a Callable Statement, not a Prepared Statement, but I wouldn't think that would cause this issue. Am I wrong? What really seems weird to me is that the exception is saying I'm trying to cast a String to oracle.sql.BLOB, but I'm not; I'm trying to cast to a java.sql.Type.BLOB. This is very strange to me. Seth
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic