如何使用 jdbc 在 oracle 10g 中插入大型 clob 数据(> 4K 个字符)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6684214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert Large clob data(>4K characters) in oracle 10g using jdbc
提问by aquero
Could you guys please tell me how to insert clob data using jdbc. Iam using oracle10g database.
你们能告诉我如何使用jdbc插入clob数据吗?我正在使用 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,这两种方法都会失败。
回答by Denis
This works on oracle11g jdk5
这适用于 oracle11g jdk5
tempClob.length()<4000){
byte[] bytes = tempClob.getBytes();
pstmnt.setCharacterStream(2, new StringReader(new String( bytes )), bytes.length);
}
回答by Jostein Stuhaug
Here's som code I found lying around that I think does what you want:
这是我发现的一些代码,我认为可以满足您的需求:
I have a static method like this:
我有一个像这样的静态方法:
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 内容都在一个字符串中,因此它全部加载到内存中,可能不适合您,但它一直在满足我的简单需求。
EDIT:I should probably note that I used this code with a plsql procedure call. Haven't tested it directly with sql
编辑:我可能应该注意到我将此代码与 plsql 过程调用一起使用。没有直接用sql测试过
回答by Martin
If you are using Oracle 11g you will need drivers from here: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html
如果您使用的是 Oracle 11g,则需要以下驱动程序: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。
回答by Anantha Sharma
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..
看看该字段是否可以容纳大小..