[英]How to insert Large clob data(>4K characters) in oracle 10g using jdbc
你们能否告诉我如何使用 jdbc 插入 clob 数据。 我正在使用 oracle10g 数据库。
我能够使用以下 2 种方法插入长度 < 4000 的 clob 数据
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,这两种方法都会失败。
这适用于 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
ojdbc5.jar 用于 JDK 1.5 或 ojdbc6.jar 用于 JDK 1.6
jar 需要包含在您的类路径中。
令人惊讶的是,Oracle 11g 可以存储 8 到 128 Tera 字节的 Clob。
这是我发现的一些代码,我认为它可以满足您的需求:
我有一个像这样的 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;
}
}
然后你可以像这样使用它:
CLOB tempClob = DBUtil.getCLOB(longString, connection);
pstmt.setCLOB(colnumber, tempClob);
这要求整个 clob 内容都在一个字符串中,所以它全部加载到 memory 中,也许不适合你,但它一直在满足我的简单需求。
编辑:我可能应该注意到我将此代码与 plsql 过程调用一起使用。 没有直接用 sql 测试过
您是否尝试检查该字段的最大大小... SELECT LENGTH(clobfield) FROM table
看看这个字段是否可以容纳大小..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.