4

I am executing following sql query on SQL Server 2008 using jTDS API:

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:

net.sourceforge.jtds.jdbc.ClobImpl@2b34fb

It seems it is stored as object, how can I convert it to normal string ?

6
  • What is the type in Java? DBVT_STRING? DBVT_ASTRING? Something else? Commented Aug 13, 2012 at 20:52
  • 1
    When you are getting the values off the ResultSet, I take you've already tried getString(column) and getCharacterStream(column)? Commented Aug 13, 2012 at 20:53
  • DBVT_STRING ? , even i tried , toString on it, it's still same Commented Aug 13, 2012 at 20:59
  • Might want to have a look at this link: sourceforge.net/projects/jtds/forums/forum/104389/topic/4623409 Commented Aug 13, 2012 at 21:02
  • Found this, take a look: stackoverflow.com/questions/2169732/… Commented Aug 13, 2012 at 23:35

2 Answers 2

7

Tried the Link Sent by @Roberto Navaron , It worked, just passed object to this method, type cast it to clob and it returns string

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();
}
Sign up to request clarification or add additional context in comments.

Comments

1

Or we can use this -

  • String getNString(int columnIndex)
  • String getNString(String columnLabel)

http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.