如何使用 Java/JDBC 在 Oracle 数据库中存储长度超过 4000 个字符的字符串?

发布于 2024-09-02 16:58:15 字数 614 浏览 6 评论 0原文

我不知道如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江湖正好 2024-09-09 16:58:15

您(至少)有两个选择:

  • 使用connection.createClob()创建一个Clob,在其上设置数据,并将其设置在准备好的语句上。这适用于较小的数据

  • 使用preparedStatement.setClob(position, reader) - 这里您将有一个Reader实例。

You have (at least) two options:

  • use connection.createClob() to create a Clob, set the data on it, and set it on the prepared statement. This will work for smaller data

  • use preparedStatement.setClob(position, reader) - here you will have a Reader instance.

且行且努力 2024-09-09 16:58:15

另外,下面的解决方案解释了如何使用 PL/SQL

参考来处理此问题: http://gogates.blogspot.com/2013/09/inserting-clob-data-which-is-more-than.html


插入超过 4000 个字符的 CLOB 数据进入 Oracle 表
我试图将 4000 多个字符的数据插入到以 CLOB 作为数据类型的 Oracle 表中,但无法做到这一点..使用简单的插入语句

我使用的表结构如下所示

CREATE TABLE ClobTest (id number, clobData clob );

表结构非常简单,只有 2 列和一个 clob 数据类型。

现在...如果您运行下面的查询,它将执行而不会出现任何错误

INSERT INTO ClobTest VALUES(1, 'ClobTestData');

但是,当您将数据集的长度增加到 4000+ 时,它将不会成功。并且会给出如下错误

错误报告:
SQL 错误:ORA-01704:字符串文字太长
01704. 00000 - “字符串文字太长”
*原因:字符串文字长度超过 4000 个字符。
*操作:使用最多 4000 个字符的字符串文字。
较长的值只能使用绑定变量输入。

在谷歌搜索这个问题后,我发现,如果你想将 4000+ 字符数据插入 CLOB 数据类型,请尝试使用 PL-SQL 块来做到这一点。

根本原因是Oracle SQL不支持超过4000个字符的char/varchar数据类型;然而 PL-SQL 却这样做。

我使用下面的匿名 pl-sql 将 4000 多个字符数据插入 CLOB 数据类型

DECLARE

vClob VARCHAR(8000);

BEGIN

vClob := '<Charater data having 4000+ characters>';

INSERT INTO ClobTest VALUES(1, vClob);

END;

要验证是否真的将 4000 多个字符插入到在查询下面执行的目标中

SELECT LENGHT(clobData) FROM ClobTest 

我希望有所帮助

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

CREATE TABLE ClobTest (id number, clobData clob );

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

INSERT INTO ClobTest VALUES(1, 'ClobTestData');

However, when you increase the length of your data set 4000+ it will not succeed.and will give error as below

Error report:
SQL Error: ORA-01704: string literal too long
01704. 00000 - "string literal too long"
*Cause: The string literal is longer than 4000 characters.
*Action: Use a string literal of at most 4000 characters.
Longer values may only be entered using bind variables.

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

DECLARE

vClob VARCHAR(8000);

BEGIN

vClob := '<Charater data having 4000+ characters>';

INSERT INTO ClobTest VALUES(1, vClob);

END;

To verify if really 4000+ characters got inserted into target executed below query

SELECT LENGHT(clobData) FROM ClobTest 

I hope that helps

入怼 2024-09-09 16:58:15

这是一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文