【问题标题】:PreparedStatement.setBinaryStream for informix JDBC driver用于 informix JDBC 驱动程序的 PreparedStatement.setBinaryStream
【发布时间】:2015-10-02 13:03:51
【问题描述】:

我正在使用以下函数将二进制流值设置为准备好的语句。

void setBlobValue(String value, PreparedStatement prepStmt, int index) throws SQLException, IOException {
    if (value != null) {
        InputStream inputStream = new ByteArrayInputStream(CharacterEncoder.getSafeText(value).getBytes());
        if (inputStream != null) {
            prepStmt.setBinaryStream(index, inputStream, inputStream.available());
        } else {
            prepStmt.setBinaryStream(index, inputStream, 0);
        }
    } else {
        prepStmt.setBinaryStream(index, null, 0);
    }
}

我使用此函数为 H2、mysql、mssql、oracle、oracle_rac、DB2 和 informix 数据库设置准备好的语句。当使用informix 并发送null 作为值时,setBinaryStream 方法会给出NullPointerException,即使在其他数据库中它也可以正常工作。

这是什么原因?我该如何解决这个问题?

【问题讨论】:

  • 可能是驱动的bug,换成setNull试试吧

标签: database jdbc blob informix


【解决方案1】:

我认为这是 Informix JDBC 驱动程序中的错误。

您已经检查了inputStream 是否为null,所以我认为最好使用setNull() 而不是setBinaryStream()

   if (inputStream != null) {
        prepStmt.setBinaryStream(index, inputStream, inputStream.available());
    } else {
        prepStmt.setNull(index);
    }

【讨论】:

    【解决方案2】:

    这是由

    修复的
    prepStmt.setBinaryStream(index, new ByteArrayInputStream(CharacterEncoder.getSafeText("").getBytes()), 0);
    

    这是当前informix 驱动程序的一个错误。以下是另外两种解决方法。

    setNULL(1,IfxTypes.IFX_TYPE_BLOB)
    
    setObject(1,null)
    

    【讨论】:

      猜你喜欢
      • 2015-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      • 2011-12-16
      • 2012-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多