15214

Converting nvarchar(max) data type to string in Java

Question:

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 ?

Answer1:

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(); }

Answer2:

Or we can use this -

<ul><li>String getNString(int columnIndex) </li> <li>String getNString(String columnLabel)</li> </ul>

<a href="http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html" rel="nofollow">http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html</a>

Recommend

  • maven dependency for sqlserver2010?
  • jdbc:jtds Connection to SQL server [duplicate]
  • Cannot connect to Sql Server via Android Test
  • How can I enforce a query timeout for MS SQL Server queries run via the MS JDBC Driver?
  • Encode/decode AMR
  • How to wrap UTF-8 encoded C++ std::strings with Swig in C#?
  • Simple script doesn't show anything on the Output in LuaEdit
  • Is there a limit on how long a cfquery with cfqueryparam can get?
  • Hibernate - Cannot connect to DB
  • Maven use Encrypted passwords in POM
  • JPA/Hibernate - Entity name seems to be important. If I rename to “Bob” works fine
  • Pointer to a logger class provide for all other classes? [duplicate]
  • How to know if a JFrame is on screen in a multi screen environment
  • Does the MySQL IN clause execute the subquery multiple times?
  • insert a picture into database(sqlite) with java code. what should i do?
  • OpenGL - Object Transformations and VBOs
  • How to call a procedure using NHibernate that returns result from multiple tables?
  • F#: In which memory area is the continuation stored: stack or heap?
  • How can I let users share their location in Bot Framework webchat channel?
  • Get used tables from sql query [duplicate]
  • HttpURLConnection Closing IO Streams
  • How to 'create temp table as select' in Slick?
  • what makes a request a new request in asp.net C#
  • Keep this build forever option - Jenkins
  • Parsing a CSV string while ignoring commas inside the individual columns
  • Converting a WriteableBitmap image ToArray in UWP
  • Is there any way to access browser form field suggestions from JavaScript?
  • Get object from AWS S3 as a stream
  • Cross-Platform Protobuf Serialization
  • Validaiting emails with Net.Mail MailAddress
  • MySQL WHERE-condition in procedure ignored
  • Convert array of 8 bytes to signed long in C++
  • Hazelcast - OperationTimeoutException
  • Web-crawler for facebook in python
  • Do I've to free mysql result after storing it?
  • trying to dynamically update Highchart column chart but series undefined
  • Understanding cpu registers
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • java string with new operator and a literal