Java: How to insert CLOB into oracle database

Ianthe picture Ianthe · Apr 5, 2011 · Viewed 110.4k times · Source

I need to write an XML file content into oracle database where the column is of CLOB datatype. How will I do that?

Answer

Lukas Eder picture Lukas Eder · Apr 5, 2011

The easiest way is to simply use the

stmt.setString(position, xml);

methods (for "small" strings which can be easily kept in Java memory), or

try {
  java.sql.Clob clob = 
    oracle.sql.CLOB.createTemporary(
      connection, false, oracle.sql.CLOB.DURATION_SESSION);

  clob.setString(1, xml);
  stmt.setClob(position, clob);
  stmt.execute();
}

// Important!
finally {
  clob.free();
}