4

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!

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

@ericbn Please do not edit the question in a way that changes its meaning/intent. Instead, add a comment indicating the way in which you think it should be changed.
3

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?

1 Comment

I guess you are right. these two Blog are not compatible. I now use weblogic.jdbc.common.OracleBlob, it works fine. Although it is deprecated. But will keep it until we upgrade to Java 5 or higher.
3

You seem to not have an oracle.sql.BLOB there (if you did, it should work, BLOB implements Blob). What does the ClassCastException say it is?

What version of Oracle and what version of the JDBC driver are you using?

getBinaryOutputStream is deprecated anyway, you should use setBinaryStream in the JDBC (3.0) interface, which probably removes the need to go to Oracle's internal class at all.

1 Comment

You had a good tip on using "setBinaryStream" defined in JDBC 3.0. Unfortunately my application still using 1.4. Again will wait until we upgrade :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.