为什么我不能从本机 java Blob 转换 oracle BLOB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/379594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:36:11  来源:igfitidea点击:

Why I cannot cast oracle BLOB from native java Blob

javaoracleblobclasscastexception

提问by netic

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

我正在从 ResultSet 读取文件,需要将文件保存到 Oracle 数据库中。

...
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!

有没有人对此有解决方案?谢谢!

采纳答案by netic

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:

从 oracle blob 获取输出流的代码是:

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

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

setBinaryStream() 实际上是返回 java.io.OutputStream 对象

回答by Dan Vinton

java.sql.Blobis an interface. Presumably the implementation returned in your ResultSetis a different implementation to oracle.sql.BLOB?

java.sql.Blob是一个接口。据推测,您返回的ResultSet实现与oracle.sql.BLOB?

What does myfile.getClass()return?

什么myfile.getClass()回报?

回答by Thilo

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?

您那里似乎没有 oracle.sql.BLOB(如果有,它应该可以工作,BLOB 实现了 Blob)。ClassCastException 说的是什么?

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

您使用的是什么版本的 Oracle 和什么版本的 JDBC 驱动程序?

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

无论如何,不​​推荐使用getBinaryOutputStream,您应该在 JDBC (3.0) 接口中使用setBinaryStream,这可能根本不需要转到 Oracle 的内部类。