如何使用 Java/JDBC 在 Oracle 数据库中存储超过 4000 个字符的字符串? [英] How do I store a string longer than 4000 characters in an Oracle Database using Java/JDBC?
问题描述
我不知道如何使用 Java/JDBC 将很长的字符串插入到 Oracle 数据库中.
I’m not sure how to use Java/JDBC to insert a very long string into an Oracle database.
我有一个大于 4000 个字符的字符串,假设它是 6000.我想将这个字符串存储在 Oracle 数据库中.
I have a String which is greater than 4000 characters, lets say it’s 6000. I want to take this string and store it in an Oracle database.
这样做的方法似乎是使用 CLOB 数据类型.好的,所以我将该列声明为描述 CLOB.
The way to do this seems to be with the CLOB datatype. Okay, so I declared the column as description CLOB.
现在,当需要实际插入数据时,我有一个准备好的语句 pstmt.它看起来像 pstmt = conn.prepareStatement(INSERT INTO Table VALUES(?)")
.
Now, when it comes time to actually insert the data, I have a prepared statement pstmt. It looks like pstmt = conn.prepareStatement("INSERT INTO Table VALUES(?)")
.
所以我想使用方法pstmt.setClob()
.但是,我不知道如何创建一个包含我的 String 的 Clob 对象;没有构造函数(大概是因为它可能比可用内存大得多).
So I want to use the method pstmt.setClob()
. However, I don’t know how to create a Clob object with my String in it; there's no constructor (presumably because it can be potentially much larger than available memory).
如何将 String 放入 Clob 中?
How do I put my String into a Clob?
请记住,我不是一个非常有经验的程序员;请尽量保持解释简单.效率、良好实践等在这里不是问题,我只想要绝对最简单的解决方案.如果可能的话,我想避免下载其他软件包;现在我只使用 JDK 1.4 和标记为 ojdbc14.jar
的内容.我环顾四周,但我无法理解我找到的任何解释.
Keep in mind I’m not a very experienced programmer; please try to keep the explanations as simple as possible. Efficiency, good practices, etc. are not a concern here, I just want the absolute easiest solution. I’d like to avoid downloading other packages if it all possible; right now I’m just using JDK 1.4 and what is labelled ojdbc14.jar
. I've looked around a bit but I haven't been able to follow any of the explanations I've found.
如果您有一个不使用 Clob 的解决方案,我也愿意接受,但它必须是一列.
If you have a solution that doesn’t use Clobs, I’d be open to that as well, but it has to be one column.
推荐答案
您(至少)有两个选择:
You have (at least) two options:
使用
connection.createClob()
创建一个Clob
,在上面设置数据,在准备好的语句上设置.这适用于较小的数据
use
connection.createClob()
to create aClob
, set the data on it, and set it on the prepared statement. This will work for smaller data
使用 preparedStatement.setClob(position, reader)
- 这里你会有一个 Reader
实例.
use preparedStatement.setClob(position, reader)
- here you will have a Reader
instance.
这篇关于如何使用 Java/JDBC 在 Oracle 数据库中存储超过 4000 个字符的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!