在 Java 中将 nvarchar(max) 数据类型转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11942073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting nvarchar(max) data type to string in Java
提问by Pradeep
I am executing following sql query on SQL Server 2008 using jTDS API:
我正在使用 jTDS API 在 SQL Server 2008 上执行以下 sql 查询:
SELECT a , b , c FROM [db].[dbo].[table] where d = 1;
And data type for these three fields are as follows:
这三个字段的数据类型如下:
a -- nvarchar(255)
b -- nvarchar(255)
c -- nvarchar(max)
When I execute query using Java, the string values for a and b are plain text, but for c I am getting following value:
当我使用 Java 执行查询时,a 和 b 的字符串值是纯文本,但对于 c 我得到以下值:
net.sourceforge.jtds.jdbc.ClobImpl@2b34fb
It seems it is stored as object, how can I convert it to normal string ?
它似乎存储为对象,如何将其转换为普通字符串?
回答by Pradeep
Tried the Link Sent by @Roberto Navaron , It worked, just passed object to this method, type cast it to clob and it returns string
尝试了@Roberto Navaron 发送的链接,它起作用了,只是将对象传递给此方法,将其类型转换为 clob 并返回字符串
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();
}
回答by Ravindra
Or we can use this -
或者我们可以使用这个 -
- String getNString(int columnIndex)
- String getNString(String columnLabel)
- String getNString(int columnIndex)
- String getNString(String columnLabel)
http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html
http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html