Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I cannot cast oracle BLOB from native java Blob

I am reading file from ResultSet and it's required to save file into Oracle Database.

...
ResultSet rs = ...
java.sql.Blob myfile = rs.getBlob("field")
java.io.OutputStream os = ((oracle.sql.BLOB) myfile).getBinaryOutputStream();

I get get this error message

java.lang.ClassCastException

Any one have solution to this? Thanks!

like image 212
netic Avatar asked Dec 31 '22 07:12

netic


2 Answers

I have found the solution. I'd like to share with those who has this problem.

The code to get outputstream from oracle blob is:

java.io.OutputStream os = ((oracle.sql.BLOB) myBlob).setBinaryStream(1L);

setBinaryStream() is actually returning java.io.OutputStream object

like image 159
netic Avatar answered Jan 07 '23 04:01

netic


java.sql.Blob is an interface. Presumably the implementation returned in your ResultSet is a different implementation to oracle.sql.BLOB?

What does myfile.getClass() return?

like image 23
Dan Vinton Avatar answered Jan 07 '23 05:01

Dan Vinton