Sample of JDBC query a CLOB column and write into file.
This code snippet is verified on both DB2 and Oracle.
public void getCLOBColumn(Connection conn, String outfile) {
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT <CLOBCOL> FROM <TABLE> WHERE ...");
FileWriter fw = new FileWriter(outfile, true); // append mode
while (rs.next()) {
Clob clob = rs.getClob(1);
String clobstr = clob.getSubString(1, (int) clob.length());
fw.write(clobstr);
fw.write("\n");
}
fw.close();
rs.close();
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}