简体   繁体   English

如何使用 jdbc 在 oracle 10g 中插入大 clob 数据(> 4K 个字符)

[英]How to insert Large clob data(>4K characters) in oracle 10g using jdbc

Could you guys please tell me how to insert clob data using jdbc.你们能否告诉我如何使用 jdbc 插入 clob 数据。 Iam using oracle10g database.我正在使用 oracle10g 数据库。

I was able to insert clob data having length < 4000 using the below 2 methods我能够使用以下 2 种方法插入长度 < 4000 的 clob 数据

1. 1.

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

2. 2.

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

When the length of the clob data is large for example abt 29k, both these methods fail.当 clob 数据的长度很大时,例如 abt 29k,这两种方法都会失败。

This works on oracle11g jdk5这适用于 oracle11g jdk5

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

If you are using Oracle 11g you will need drivers from here: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html If you are using Oracle 11g you will need drivers from here: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html

ojdbc5.jar for JDK 1.5 or ojdbc6.jar for JDK 1.6 ojdbc5.jar 用于 JDK 1.5 或 ojdbc6.jar 用于 JDK 1.6

The jar will need to be included in your classpath. jar 需要包含在您的类路径中。

Amazingly Oracle 11g can store Clobs of 8 to 128 Tera bytes.令人惊讶的是,Oracle 11g 可以存储 8 到 128 Tera 字节的 Clob。

Here's som code I found lying around that I think does what you want:这是我发现的一些代码,我认为它可以满足您的需求:

I have a static method like this:我有一个像这样的 static 方法:

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;
}
}

And then you can use it something like this:然后你可以像这样使用它:

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

This require the entire clob content to be in a string, so it's all loaded into memory, perhaps not suitable for you, but it's been working for my simple needs.这要求整个 clob 内容都在一个字符串中,所以它全部加载到 memory 中,也许不适合你,但它一直在满足我的简单需求。

EDIT: I should probably note that I used this code with a plsql procedure call.编辑:我可能应该注意到我将此代码与 plsql 过程调用一起使用。 Haven't tested it directly with sql没有直接用 sql 测试过

have you tried to check the max size of the field... SELECT LENGTH(clobfield) FROM table您是否尝试检查该字段的最大大小... SELECT LENGTH(clobfield) FROM table

see if the field can accomodate the size..看看这个字段是否可以容纳大小..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM