Why I cannot cast oracle BLOB from native java Blob

℡╲_俬逩灬. 提交于 2019-12-23 14:56:09

问题


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!


回答1:


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




回答2:


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?




回答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.



来源:https://stackoverflow.com/questions/379594/why-i-cannot-cast-oracle-blob-from-native-java-blob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!