如何使用 Java/JDBC 在 Oracle 数据库中存储长度超过 4000 个字符的字符串?
我不知道如何使用 Java/JDBC 将很长的字符串插入 Oracle 数据库。
我有一个超过 4000 个字符的字符串,假设它是 6000 个字符。我想获取该字符串并将其存储在 Oracle 数据库中。
执行此操作的方法似乎是使用 CLOB 数据类型。好的,所以我将该列声明为描述 CLOB。
现在,当需要实际插入数据时,我有一个准备好的语句 pstmt。它看起来像 pstmt = conn.prepareStatement(“INSERT INTO Table VALUES(?)”)。
所以我想使用方法pstmt.setClob()
。但是,我不知道如何创建一个包含我的 String 的 Clob 对象;没有构造函数(大概是因为它可能比可用内存大得多)。
如何将字符串放入 Clob 中?
请记住,我不是一个非常有经验的程序员;请尽量使解释尽可能简单。效率、良好实践等不是这里关心的问题,我只想要绝对最简单的解决方案。如果可能的话,我想避免下载其他软件包;现在我只使用 JDK 1.4 和标记为 ojdbc14.jar
的内容。我环顾四周,但无法理解我找到的任何解释。
如果您有一个不使用 Clob 的解决方案,我也愿意接受,但它必须是一列。
I’m not sure how to use Java/JDBC to insert a very long string into an Oracle database.
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.
The way to do this seems to be with the CLOB datatype. Okay, so I declared the column as description CLOB.
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(?)”)
.
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).
How do I put my String into a Clob?
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您(至少)有两个选择:
使用
connection.createClob()
创建一个Clob
,在其上设置数据,并将其设置在准备好的语句上。这适用于较小的数据使用
preparedStatement.setClob(position, reader)
- 这里您将有一个Reader
实例。You have (at least) two options:
use
connection.createClob()
to create aClob
, set the data on it, and set it on the prepared statement. This will work for smaller datause
preparedStatement.setClob(position, reader)
- here you will have aReader
instance.另外,下面的解决方案解释了如何使用 PL/SQL
参考来处理此问题: http://gogates.blogspot.com/2013/09/inserting-clob-data-which-is-more-than.html
插入超过 4000 个字符的 CLOB 数据进入 Oracle 表
我试图将 4000 多个字符的数据插入到以 CLOB 作为数据类型的 Oracle 表中,但无法做到这一点..使用简单的插入语句
我使用的表结构如下所示
表结构非常简单,只有 2 列和一个 clob 数据类型。
现在...如果您运行下面的查询,它将执行而不会出现任何错误
但是,当您将数据集的长度增加到 4000+ 时,它将不会成功。并且会给出如下错误
在谷歌搜索这个问题后,我发现,如果你想将 4000+ 字符数据插入 CLOB 数据类型,请尝试使用 PL-SQL 块来做到这一点。
根本原因是Oracle SQL不支持超过4000个字符的char/varchar数据类型;然而 PL-SQL 却这样做。
我使用下面的匿名 pl-sql 将 4000 多个字符数据插入 CLOB 数据类型
要验证是否真的将 4000 多个字符插入到在查询下面执行的目标中
我希望有所帮助
Also, this below solution explains how to handle this using PL/SQL
Reference: http://gogates.blogspot.com/2013/09/inserting-clob-data-which-is-more-than.html
Inserting CLOB data which is more than 4000 characters into Oracle Table
I was trying to insert 4000+ characters data into an Oracle table having CLOB as data type, but could not do that.. using simple insert statement
Table Structure which I used is as below
Table structure is pretty simple, having only 2 columns a number and a clob data type.
Now... if you run below query it will get execute without any error
However, when you increase the length of your data set 4000+ it will not succeed.and will give error as below
After googling this issue, came to know that, if you want to insert 4000+ char data into CLOB data type, try to do that using PL-SQL block.
Root cause of this is, Oracle SQL do not support char / varchar data type which is more than 4000 characters; however PL-SQL does so.
I used below anonymous pl-sql to insert 4000+ character data into CLOB data type
To verify if really 4000+ characters got inserted into target executed below query
I hope that helps
这是一个 oracle 的示例.com 用于将 LOB 列与 Oracle 和 JDBC 结合使用。基本上,它为空 LOB(实际上是两个,因为它演示了 BLOB 和 CLOB)插入一个 LOB 定位器,锁定要更新的行,然后使用 BLOB 和 CLOB OutputStream 接口将数据写入 LOB。当“SELECT ... FOR UPDATE”被释放时,LOB将在数据库中刷新并且新插入的数据将可见。
Here is an example at oracle.com for using LOB columns with Oracle and JDBC. Basically, it's inserting a LOB locator for an empty LOB (actually two, since it's demonstrating both BLOBs and CLOBs), locking the row for update, and then using the BLOB and CLOB OutputStream interfaces to write the data into the LOBs. When the "SELECT ... FOR UPDATE" is released, the LOBs will be refreshed in the database and the newly inserted data will be visible.
有几种方法请参见 Java 数据库连接 (JDBC) -将字符串转换为 CLOB 并插入 Oracle 数据库
There are several methods see Java Database Connectivity (JDBC) - Convert String to CLOB and Insert into Oracle database