英文:
Converting CLOB to String removes all new line characters
问题
我有一个启用了AES加密的H2数据库。它有一个CLOB类型的列。该列包含带有换行字符的长文本(有时候)。当我在H2控制台中检查表时,输出显示插入了所有换行符,但当我查询该表时,就没有换行字符。我正在使用以下代码将CLOB对象转换为String对象:
public 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 | IOException e) {
e.printStackTrace();
}
return sb.toString();
}
我认为这里出了一些问题。是否有其他有效的方法来进行CLOB到String的转换。
英文:
I have an H2 database with AES encryption enabled. It has a column of type CLOB. The column contains long texts with new line characters(some times). When I check the table in H2 console I get outputs with all the new lines inserted but when I query the table, there are no new line characters. I am using this code to convert a CLOB object to String object:
public 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 | IOException e) {
e.printStackTrace();
}
return sb.toString();
}
I think something is going here. Is there any other efficient way of doing this CLOB to String conversion.
答案1
得分: 5
从数据库的CLOB
列中获取一个String
值,可以使用以下方法之一:
String text = resultSet.getString("MyClobColumn");
- 如果数据已经从
ResultSet
中作为Clob
(使用getClob()
)获取,可以使用clob.getSubString()
:
String text = clob.getSubString(1, (int) clob.length());
- 如果出于某种不明原因,您有一个
Reader
(来自resultSet.getCharacterStream()
或clob.getCharacterStream()
),请分块复制字符,并记得使用try-with-resources,例如:
String text;
try (Reader reader = clob.getCharacterStream()) {
StringBuilder sb = new StringBuilder();
char[] buf = new char[8192];
for (int len; (len = reader.read(buf)) != -1; )
sb.append(line, 0, len);
text = sb.toString();
}
或者在Java 10+中使用:
String text;
try (Reader reader = clob.getCharacterStream()) {
StringWriter sw = new StringWriter();
reader.transferTo(sw);
text = sw.toString();
}
当然,这三种方法都假设文本适合于String
,即它的长度不超过约21亿字符,或者仅约10亿字符,这取决于Java版本和所使用的字符(非Latin1)。
英文:
To get a String
value from a CLOB
column in the database, use one of the following:
-
String text = resultSet.getString("MyClobColumn");
-
If the data is already taken from the
ResultSet
as aClob
(usinggetClob()
), useclob.getSubString()
:String text = clob.getSubString(1, (int) clob.length());
-
If you for some obscure reason have a
Reader
(fromresultSet.getCharacterStream()
orclob.getCharacterStream()
), copy the characters in blocks, and remember to use try-with-resources, e.g.String text; try (Reader reader = clob.getCharacterStream()) { StringBuilder sb = new StringBuilder(); char[] buf = new char[8192]; for (int len; (len = reader.read(buf)) != -1; ) sb.append(line, 0, len); text = sb.toString(); }
Or with Java 10+:
String text; try (Reader reader = clob.getCharacterStream()) { StringWriter sw = new StringWriter(); reader.transferTo(sw); text = sw.toString(); }
All three of course assumes that the text will fit in a String
, i.e. that it is less than about 2.1 billion characters, or only about 1 billion characters depending on Java version and characters used (non-Latin1).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论