Search code examples
javasqlstringclob

Conversion From String to Clob and Clob to String


I am trying to save XML response which comes as StrignBuffer to method. I need to save this string data to Sql as CLOB and retrieve the same.

The response can be upto 3-4Kb, which should be retrieved to StringBuffer again

Can any one give me tutorial or sample code which doesn't include db specific methods/jars.

I can change db column type if CLOB is not suitable or any other better alternative.

Please suggest.


Solution

  • You can create a CLOB by calling connection.createClob(), then you can populate it with methods such as setString, setCharacterStream or setAsciiStream.

    Create a PreparedStatement and call its setClob() to store your CLOB in the databae.

    Retrieving the data is just the same, read it from a ResultSet and call getCharacterStream, getAsciiStream or getSubStream on it.