Java CLOB Example

SQL3 type is CLOB (Character Large OBject) for storing a large text in the character format. JDBC 2 introduced the interfaces java.sql.Blob and java.sql.Clob to support mapping for these new SQL types. JBDC 2 also added new methods, such as getBlob, setBinaryStream, getClob, setBlob, and setClob, in the interfaces ResultSet and PreparedStatement to access SQL.
CLOB : Reading

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery
    (“select clob_column from clob_table”);
while (rs.next())
{
CLOB blob = ((OracleResultSet)rs).getCLOB(1);
Reader reader = clob.getCharacterStream();
char [] buffer = new char[10];
int length = 0;
while ( (length  = reader.read (buffer)) != -1)
{
System.out.println (new String(buffer));
}
is.close();
}
CLOB : Writing
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery
  (“select clob_column from clob_table for update”);
while (rs.next())
{
  CLOB clob = ((OracleResultSet)rs).getCLOB(1);
   Writer writer = clob.getCharacterOutputStream();
   FileInputStream src = new FileInputStream(“tmp”);
   byte [] buffer = new byte[512];
   int read  = 0;
  while ( (read  = src.read(buffer)) != -1)
  {
     writer.write(buffer, 0, read);  // write clob.
  }
  src.close();
  writer.close();
}

Search:

Recent Posts