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();
}
}
本文提供了一个实用的代码示例,展示如何使用JDBC从数据库中查询CLOB类型的字段,并将获取的数据写入到本地文件中。此方法已在DB2和Oracle数据库上验证过。
669

被折叠的 条评论
为什么被折叠?



