3

I'm coding a Java function inside Oracle Database which produce allot of text! how to convert a string to CLOB (oracle.sql.CLOB) in java? what is the straight-forward way to do?

I'm trying to build a function which has String as an input with oracle.sql.CLOB as an output, but it's not working! I'm not doing any jdbc related work!

here is what I have (which is not working!)

  public static CLOB str2clob(String s)
  {
         oracle.sql.CLOB clob = null;

         try
         {
               clob.setString(0,s);

         } catch (Exception e)
         {
         }
         return clob;
  }
3

3 Answers 3

2
/*********************************************************************************************
 * From String to CLOB
 *  @return CLOB representation of string
 *********************************************************************************************/
private java.sql.Clob stringToClob(String source)
{
    try
    {
        return new javax.sql.rowset.serial.SerialClob(source.toCharArray());
    }
    catch (Exception e)
    {
        log.error("Could not convert string to a CLOB",e);
        return null;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is possible but created Clob does not work properly! For example, invoking yourClob.getAsciiStream() throws SerialException: Unsupported operation. SerialClob cannot return a the CLOB value as an ascii stream, unless instantiated with a fully implemented Clob object. For more details please see SerialClob implementation.
0

Something like this:

oracle.sql.CLOB **clob** = ((OracleResultSet)rset).getCLOB(index);

StreamReader sr = new StreamReader(**yourString**);
char[] bufr = new char[clob.getChunkSize()];


OutputStream os = new OutputStream(clob.getOutputStream());

int charsRead = 0;
for(charsRead = sr.read(bufr); charsRead>-1; charsRead = sr.read(bufr)){
   os.write(bufr, 0, charsRead);
}


os.flush();
os.close()
sr.close();

4 Comments

so my results are save as CLOB in clob ?
what about index in 'oracle.sql.CLOB clob = ((OracleResultSet)rset).getCLOB(index);'?
you write "yourString" (by using StreamReader sr) in outputStream of clob (clob.getOutputStream())
not working for me! I tried to build a function which has String as an input with oracle.sql.CLOB as an output, but it's not working! I'm not doing any jdbc related work!
0

You can use NonContextualLobCreator from Hibernate. Please see answers in What is the alternate for deprecated Hibernate.createClob(Reader reader, int length)

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.