用于将CLOB读取到String的最有效解决方案,以及在Java中将String读取到CLOB? [英] Most efficient solution for reading CLOB to String, and String to CLOB in Java?
问题描述
我有一个很大的CLOB(超过32kB)我想用StringBuilder读取一个String。我如何以最有效的方式做到这一点?我不能使用StringBuilder的int length构造函数,因为我的CLOB的长度比int长,需要long值。
I have a big CLOB (more than 32kB) that I want to read to a String, using StringBuilder. How do I do this in the most efficient way? I can not use the "int length" constructor for StringBuilder since the lenght of my CLOB is longer than a "int" and needs a "long" value.
我是与Java I / O类不相容,并希望得到一些指导。
I am not that confortable with the Java I/O classes, and would like to get some guidance.
编辑 - 我试过了clobToString()的代码:
private String clobToString(Clob data) {
StringBuilder sb = new StringBuilder();
try {
Reader reader = data.getCharacterStream();
BufferedReader br = new BufferedReader(reader);
String line;
while(null != (line = br.readLine())) {
sb.append(line);
}
br.close();
} catch (SQLException e) {
// handle this exception
} catch (IOException e) {
// handle this exception
}
return sb.toString();
}
推荐答案
我不能对
StringBuilder
使用int length构造函数,因为我的CLOB长度超过int
并且需要long
值。
I can not use the "int length" constructor for
StringBuilder
since the length of my CLOB is longer than aint
and needs along
value.
如果CLOB长度大于fit在int中,CLOB数据也不适合String。您将不得不使用流式处理方法来处理这么多的XML数据。
If the CLOB length is greater than fits in an int, the CLOB data won't fit in a String either. You'll have to use a streaming approach to deal with this much XML data.
如果CLOB的实际长度小于整数。 MAX_VALUE
,只需强制 long
到 int
,只需输入( int)
在它前面。
If the actual length of the CLOB is smaller than Integer.MAX_VALUE
, just force the long
to int
by putting (int)
in front of it.
这篇关于用于将CLOB读取到String的最有效解决方案,以及在Java中将String读取到CLOB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!