Example 6-2: Reading a stream of characters from a CLOB

import oracle.sql.*;
import java.sql.*;
import oracle.jdbc.driver.*;
import java.io.*;
import org.w3c.dom.Document;

public class ReadCLOB {
  // Read a Clob from any column in any table, returning a character Reader
  public static Reader fromColumn(Connection conn, String tableName,
                                      String colName,String idCol, String idVal)
                                  throws FileNotFoundException {
    InputStream is = fromColumnAsInputStream(conn,tableName,colName,idCol,idVal);
    return is != null ? new InputStreamReader(is) : null;
  }
  // Read a Clob from any column in any table, returning an InputStream
  public static InputStream fromColumnAsInputStream(Connection conn,
                                                        String tableName,
                                                        String colName,
                                                        String idCol,
                                                        String idVal)
                                                  throws FileNotFoundException {

    try {
      PreparedStatement p = conn.prepareStatement("SELECT " + colName   +
                                                  "  FROM " + tableName +
                                                  " WHERE " + idCol + "= ?");
      CLOB theClob = null;
      p.setString(1,idVal);
      ResultSet rs = p.executeQuery();
      boolean exists = false;
      if (rs.next()) {
        exists  = true;
        theClob = ((OracleResultSet)rs).getCLOB(1);
      }
      rs.close();
      p.close();
      if (!exists) {
        throw new FileNotFoundException(idVal);
      }
      else {
        return theClob.getAsciiStream();
      }
    }
    catch (SQLException s) {
      return null;
    }
  }
}