将大型文本文件上传到 DB2 CLOB 字段
好吧,我已经在这个问题上头撞墙有一段时间了,所以就这样吧...
我目前正在编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所指出的,
setCharacterStream
的 1.5 规范对于读取到文件末尾还是直到达到长度并不明确。如果您传递
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.It may work like you want if you pass
Integer.MAX_VALUE
as the length parameter. Try it and see what happens.