Data-Base
Data-Base

Reputation: 8628

convert string to oracle.sql.clob in java?

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

Upvotes: 3

Views: 22452

Answers (3)

lu_ko
lu_ko

Reputation: 4285

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

Upvotes: 0

Stan Sokolov
Stan Sokolov

Reputation: 2270

/*********************************************************************************************
 * 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;
    }
}

Upvotes: 2

Sergey Gazaryan
Sergey Gazaryan

Reputation: 1043

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

Upvotes: 0

Related Questions