oracle ResultSet.getBlob() 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1228827/
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 18:43:25  来源:igfitidea点击:

ResultSet.getBlob() Exception

oraclejdbcora-17004

提问by dfa

The Code:

编码:

ResultSet rs = null;

try { 
    conn = getConnection();
    stmt = conn.prepareStatement(sql);
    rs = stmt.executeQuery();

    while (rs.next()) {
        Blob blob = rs.getBlob("text");
        byte[] blobbytes = blob.getBytes(1, (int) blob.length());
    String text = new String(blobbytes);

The result:

结果:

java.sql.SQLException: Invalid column type: getBLOB not implemented for class oracle.jdbc.driver.T4CClobAccessor
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:111)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)
at oracle.jdbc.driver.Accessor.unimpl(Accessor.java:357)
at oracle.jdbc.driver.Accessor.getBLOB(Accessor.java:1299)
at oracle.jdbc.driver.OracleResultSetImpl.getBLOB(OracleResultSetImpl.java:1280)
at oracle.jdbc.driver.OracleResultSetImpl.getBlob(OracleResultSetImpl.java:1466)
at oracle.jdbc.driver.OracleResultSet.getBlob(OracleResultSet.java:1978)

I have class12_10g.zip in my class path. I've googled and have found essentially only one site on this particular problem, and it wasn't helpful at.

我的类路径中有 class12_10g.zip。我在谷歌上搜索过,基本上只找到了一个关于这个特定问题的网站,而且它没有帮助。

Does anyone have any ideas on this?

有没有人对此有任何想法?



A little background: We were converting one of our databases from MySQL to Oracle. Within the MySQL DB, one of the fields is a longtext which is treated as a BLOB in the code. The SQL developer workbench by default converts longtext to CLOB (make sense to me) but the code was expecting Blob. I guess the error wasn't that nice: oracle.jdbc.driver.T4CClobAccessor (though it does mention Clob).

一点背景:我们正在将我们的一个数据库从 MySQL 转换为 Oracle。在 MySQL 数据库中,其中一个字段是长文本,在代码中被视为 BLOB。默认情况下,SQL 开发人员工作台将长文本转换为 CLOB(对我来说很有意义),但代码需要 Blob。我想错误不是那么好:oracle.jdbc.driver.T4CClobAccessor(尽管它确实提到了Clob)。

When I tried the following:

当我尝试以下操作时:

rs = stmt.executeQuery();

while (rs.next()) {
   byte[] blobbytes = rs.getBytes("text");
   String text = new String(blobbytes);
}

it threw an unsupported exception - all I had to do in the first place was compare the types in the newly created Oracle DB with what the code was expecting (unfortunately I just assumed they would match).

它抛出了一个不受支持的异常——我首先要做的就是将新创建的 Oracle DB 中的类型与代码期望的类型进行比较(不幸的是,我只是假设它们会匹配)。

Sorry guys! Not that I've put much thought into it, now I have to figure out why the original developers used BLOB types for longtext

对不起大家!倒不是我想太多了,现在我必须弄清楚为什么最初的开发人员将 BLOB 类型用于长文本

回答by DreadPirateShawn

Not sure about making the Blob object work -- I typically skip the Blob step:

不确定使 Blob 对象工作——我通常跳过 Blob 步骤:

rs = stmt.executeQuery();

while (rs.next()) {
   byte[] blobbytes = rs.getBytes("text");
   String text = new String(blobbytes);
}

回答by dfa

try to use the latest version of the drivers (10.2.0.4). Try also the drivers for JDK 1.4/1.5 since classes12 are for JDK 1.2/1.3.

尝试使用最新版本的驱动程序 (10.2.0.4)。也请尝试 JDK 1.4/1.5 的驱动程序,因为 classes12 适用于 JDK 1.2/1.3。

回答by JeeBee

I have a utility method in a DAO superclass of all my DAOs:

我在所有 DAO 的 DAO 超类中有一个实用方法:

protected byte[] readBlob(oracle.sql.BLOB blob) throws SQLException {

    if (blob != null) {
        byte[] buffer = new byte[(int) blob.length()];
        int bufsz = blob.getBufferSize();
        InputStream is = blob.getBinaryStream();
        int len = -1, off = 0;
        try {
            while ((len = is.read(buffer, off, bufsz)) != -1) {
                off += len;
            }
        } catch (IOException ioe) {
            logger.debug("IOException when reading blob", ioe);
        }
        return buffer;
    } else {
        return null;
    }
}

// to get the oracle BLOB object from the result set:
oracle.sql.BLOB blob= (oracle.sql.BLOB) ((OracleResultSet) rs).getBlob("blobd");

Someone will now say "why didn't you just do XYZ", but there was some issue at the time that made the above more reliable.

现在有人会说“你为什么不做 XYZ”,但当时有一些问题使上述更可靠。

回答by cagcowboy

Try...

尝试...

  PreparedStatement stmt = connection.prepareStatement(query);
  ResultSet rs = stmt.executeQuery();
  rs.next();
  InputStream is = rs.getBlob(columnIndex).getBinaryStream();

...instead?

...反而?

回答by user1741202

When JDBC returns a ResultSet from an Oracle database it always returns an OracleResultSet. If you are typing it as a ResultSet, java upcasts it to the standard SQL ResultSet. OracleResultSet overrides most of the data type methods, because Oracle datatypes are not standard SQL types. In other words, that worked because you cast the rs as an OracleResultSet, and used it's getBlob method.

当 JDBC 从 Oracle 数据库返回一个 ResultSet 时,它总是返回一个 OracleResultSet。如果您将其作为 ResultSet 键入,java 会将其向上转换为标准 SQL ResultSet。OracleResultSet 覆盖了大多数数据类型方法,因为 Oracle 数据类型不是标准的 SQL 类型。换句话说,这是有效的,因为您将 rs 转换为 OracleResultSet,并使用它的 getBlob 方法。