将大型文本文件上传到 DB2 CLOB 字段

发布于 2024-10-02 19:26:59 字数 1267 浏览 0 评论 0原文

好吧,我已经在这个问题上头撞墙有一段时间了,所以就这样吧...

我目前正在编写一个 Java (1.5) 程序,它可以将非常大(200MB+)的文本文件上传到 DB2 CLOB Column,但遇到了一些问题。

在我的研究过程中,几个代码示例建议我使用PreparedStatement方法setCharacterStream()将数据上传到CLOB字段,给我这样的结果:

 PreparedStatement preparedStatement =  
      connection.prepareStatement("INSERT INTO BOOKCOVERS VALUES(?,?)"); 
File imageFile = new File("c:\\redbookcover.jpg"); 
InputStream inputStream = new FileInputStream(imageFile); 
preparedStatement.setString(1," 0738425826"); 
preparedStatement.setBinaryStream(2,inputStream,(int)(imageFile.length())); 
preparedStatement.executeUpdate(); 

这很好,除了>setCharacterStream() 方法需要一个 LENGTH 值,指定要写入字段的字符数。考虑到这一点,这会将可写入 CLOB 字段的字符数限制为 Integer.MAX_VALUE。做一点数学计算,这会将我可以上传的文本文件的大小限制为 524288 字节,或大约半兆字节。由于 CLOB 字段最多可以存储 2GB 的数据,所以我有点困惑。

我发现在 Java 1.6 中,PreparedStatement 的 setCharacterStream() 方法已被修改为不需要长度,并且将读取直到遇到文件结尾。不幸的是,我需要使用 Java 1.5,所以这不是一个有效的选项。

在最后一项研究中,我读到 Java 1.5 的PreparedStatement 具有类似的功能,Javadoc 声称:

“...发送可能更实用 它通过 java.io.Reader 对象。这 数据将从流中读取为 需要直到到达文件末尾。”

如果存在,为什么 setCharacterStream() 方法仍然需要长度参数?您会为长度参数传递什么来使用此功能?是我有什么更好的方法吗

Alrighty, I've been banging my head against the wall on this one for a while, so here it goes...

I'm currently writing a Java (1.5) program that can upload very large (200MB+) text files to a DB2 CLOB Column, but have been running into a few issues.

During my research, several code examples suggested that I use the PreparedStatement method setCharacterStream() to upload data to the CLOB Field, giving me something like this:

 PreparedStatement preparedStatement =  
      connection.prepareStatement("INSERT INTO BOOKCOVERS VALUES(?,?)"); 
File imageFile = new File("c:\\redbookcover.jpg"); 
InputStream inputStream = new FileInputStream(imageFile); 
preparedStatement.setString(1," 0738425826"); 
preparedStatement.setBinaryStream(2,inputStream,(int)(imageFile.length())); 
preparedStatement.executeUpdate(); 

This is fine, except for the fact that the setCharacterStream() method requires a LENGTH value, specifying the number of characters to write to the field. With this in mind, this limits the number of characters that can be written to CLOB Field to Integer.MAX_VALUE. Doing a little math, this will limit the size of the text file that I can upload to 524288 bytes, or about half of a Megabyte. Since a CLOB field can store up to 2GB of data, I'm a bit confused.

I see that in Java 1.6 the PreparedStatement's setCharacterStream() method has been modified to not require a length, and will read until end-of-file is encountered. Unfortunately, I'm required to use Java 1.5, so this is not a valid option.

In one last bit of research, I read that Java 1.5's PreparedStatement has similar functionality, where the Javadoc is claiming:

"...it may be more practical to send
it via a java.io.Reader object. The
data will be read from the stream as
needed until end-of-file is reached."

If this exists, why does the setCharacterStream() method still require a length parameter? What would you pass in for the length parameter to use this functionality? Is there a better way to do this that I'm missing?

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦晓ヶ微光ヅ倾城 2024-10-09 19:26:59

正如您所指出的,setCharacterStream 的 1.5 规范对于读取到文件末尾还是直到达到长度并不明确。

将指定参数设置为
给定 Reader 对象,它是
给定的字符数长度。什么时候
输入一个非常大的 UNICODE 值
一个 LONGVARCHAR 参数,它可能是
通过发送更实用
java.io.Reader 对象。数据将
根据需要从流中读取
直到到达文件末尾。数据库连接
司机会做任何必要的事情
从 UNICODE 转换为
数据库字符格式。

如果您传递 Integer.MAX_VALUE 作为长度参数,它可能会像您想要的那样工作。尝试一下,看看会发生什么。

As you noted, the 1.5 spec for setCharacterStream is ambiguous about whether it reads to end-of-file or until length is reached.

Sets the designated parameter to the
given Reader object, which is the
given number of characters long. When
a very large UNICODE value is input to
a LONGVARCHAR parameter, it may be
more practical to send it via a
java.io.Reader object. The data will
be read from the stream as needed
until end-of-file is reached. The JDBC
driver will do any necessary
conversion from UNICODE to the
database char format.

It may work like you want if you pass Integer.MAX_VALUE as the length parameter. Try it and see what happens.

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