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

这篇博客解决了如何使用Java/JDBC将超过4000字符的长字符串插入Oracle数据库的问题。作者提到可以使用CLOB数据类型,并提供了两种方法:一是通过connection.createClob()创建CLOB对象并设置数据;二是使用PreparedStatement的setClob()方法配合Reader。博客适合初级程序员,主要关注解决实际问题,而非效率和最佳实践。
摘要由CSDN通过智能技术生成

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.

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值