【问题标题】:How to insert Large clob data(>4K characters) in oracle 10g using jdbc如何使用 jdbc 在 oracle 10g 中插入大 clob 数据(> 4K 个字符)
【发布时间】:2013-05-21 18:32:53
【问题描述】:

你们能告诉我如何使用 jdbc 插入 clob 数据吗?我正在使用oracle10g数据库。

我能够使用以下 2 种方法插入长度

1.

tempClob.length()<4000){
   pstmnt.setClob(colNumber, tempClob );
}

2.

tempClob.length()<4000){
   Reader reader =tempClob.getCharacterStream();
   pstmnt.setClob(colNumber, tempClob );
   pstmnt.setCharacterStream(colNumber, reader, new Long(tempClob.length()).intValue());
}

当 clob 数据的长度很大时,例如 abt 29k,这两种方法都会失败。

【问题讨论】:

标签: java oracle jdbc oracle10g clob


【解决方案1】:

这适用于 oracle11g jdk5

tempClob.length()<4000){
    byte[] bytes = tempClob.getBytes();
    pstmnt.setCharacterStream(2, new StringReader(new String( bytes  )), bytes.length);
}

【讨论】:

    【解决方案2】:

    如果您使用的是 Oracle 11g,您将需要以下驱动程序: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html

    JDK 1.5 的 ojdbc5.jar 或者 JDK 1.6 的 ojdbc6.jar

    jar 需要包含在您的类路径中。

    令人惊讶的是,Oracle 11g 可以存储 8 到 128 Tera 字节的 Clob。

    【讨论】:

    • 感谢马丁的回复。但我使用的是 oracle 10g 数据库。你能帮我解决这个问题吗?
    • 这没有回答实际问题。
    【解决方案3】:

    这是我发现的一些代码,我认为它可以满足您的需求:

    我有一个这样的静态方法:

    public class DBUtil {
    public static CLOB getCLOB(String innStr, Connection conn) throws SQLException, 
                                                       IOException {
      CLOB tempClob = null;
        // If the temporary CLOB has not yet been created, create new
        tempClob = CLOB.createTemporary(conn, true, CLOB.DURATION_SESSION);
    
        // Open the temporary CLOB in readwrite mode to enable writing
        tempClob.open(CLOB.MODE_READWRITE);
        // Get the output stream to write
        Writer tempClobWriter = tempClob.getCharacterOutputStream();
        // Write the data into the temporary CLOB
        tempClobWriter.write(innStr);
    
        // Flush and close the stream
        tempClobWriter.flush();
        tempClobWriter.close();
    
        // Close the temporary CLOB
        tempClob.close();
      return tempClob;
    }
    }
    

    然后你可以像这样使用它:

    CLOB tempClob = DBUtil.getCLOB(longString, connection);
    pstmt.setCLOB(colnumber, tempClob);
    

    这要求整个 clob 内容都在一个字符串中,所以它全部加载到内存中,也许不适合你,但它一直在满足我的简单需求。

    编辑:我可能应该注意到我将此代码与 plsql 过程调用一起使用。没有直接用sql测试过

    【讨论】:

    • 我已经将数据作为 Clob 添加到数据库中。我相信上面的代码是用于创建一个 clob 并插入它。我面临的问题是,当数据长度> 4k时,它会失败。非常感谢您的回答。
    • 没必要那么复杂。请参阅丹尼斯的回答或在这里:stackoverflow.com/a/8164127/330315
    【解决方案4】:

    您是否尝试过检查该字段的最大大小... SELECT LENGTH(clobfield) FROM table

    看看字段是否可以容纳大小..

    【讨论】:

      猜你喜欢
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多