在Java中将nvarchar(max)数据类型转换为字符串

时间:2022-10-05 17:04:33

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 ?

它看起来是作为对象存储的,我如何将它转换成普通的字符串呢?

2 个解决方案

#1


5  

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发送的链接,它成功了,只是将对象传递给这个方法,输入cast它到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();
}

#2


1  

Or we can use this -

或者我们可以用这个-

  • String getNString(int columnIndex)
  • 字符串getNString(int columnIndex)
  • String getNString(String columnLabel)
  • getNString字符串(字符串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

#1


5  

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发送的链接,它成功了,只是将对象传递给这个方法,输入cast它到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();
}

#2


1  

Or we can use this -

或者我们可以用这个-

  • String getNString(int columnIndex)
  • 字符串getNString(int columnIndex)
  • String getNString(String columnLabel)
  • getNString字符串(字符串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