【问题标题】:How to use setCharacterStream to update Clob in Oracle?如何使用 setCharacterStream 更新 Oracle 中的 Clob?
【发布时间】:2012-01-03 20:13:33
【问题描述】:

当我使用以下 sn-ps 更新 Oracle Clob 时:

     String toBeUpdated = ""
     StringReader reader = new StringReader(toBeUpdated);
     pStmt.setCharacterStream(parameterIndex,reader , toBeUpdated.length());

当字符串“toBeUpdated”的长度稍微大一点(一般大于5000)并且已经准备好将值存储到db中时,它没有任何异常,并且当我在方法executeUpdate()中返回了预期值运行上述代码。但奇怪的问题是我检查了数据库,发现该列为空。(应该用新值更新)。

它不是每次都发生,而是有点随机。 我尝试使用 pStmt.setString() 而不是 pStmt.setCharacterStream 一切都会好起来的。据我所知,setString 仅限于最大化字符串大小(63000),因此无法提出解决方案。

有没有人可以点亮我或经历过这个?

【问题讨论】:

    标签: java sql oracle jdbc oracle11g


    【解决方案1】:

    Oracle 提供用于从字符串存储 CLOB 的类。我使用了以下代码,它工作正常:

        long id = ...
        String content = ... // CLOB content
        try {
            Class.forName ("oracle.jdbc.OracleDriver");
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@MY_SERVER:1521:MY_DB", "user", "pass");
            String query = "UPDATE MY_TABLE SET MY_CLOB_COLUMN = ? WHERE ID = ? ";
            OraclePreparedStatement opstmt = (OraclePreparedStatement)conn.prepareStatement(query);
            opstmt.setStringForClob(1, content);
            opstmt.setLong(2, id);
            int result = opstmt.executeUpdate();
             System.out.println("Resultado para : " + tabla + " - " + columna + "  - " +  id + ":" + result);
        } catch (SQLException ex) {
            //Log here
        } catch (ClassNotFoundException ex) {
            //Log here
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-29
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      相关资源
      最近更新 更多