JDBC 中处理CLOB字段

public void insertMarketRept(List list, String sql) throws SQLException,
IOException {
if (list.size() > 0) {
conn = JdbcUtilsOracle.getConnection();
conn.setAutoCommit(false);

PreparedStatement prest = conn
.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);

for (int i = 0; i < list.size(); i++) {
kuNews knews1 = (kuNews) list.get(i);
prest.setString(1, knews1.getTitle());
prest.setDate(2, (Date) knews1.getPtime());

String str = knews1.getContent();
java.io.Reader clobReader = new java.io.StringReader(str);
prest.setCharacterStream(3, clobReader, str.length());

prest.addBatch();
}
prest.executeBatch();

conn.commit();
JdbcUtilsOracle.free(rs, st, conn);
System.out.println("有" + list.size() + "条数据更新!!!");

} else {
System.out.println("没有需要更新的数据!!!");
}
}

public void selectContent() throws SQLException, IOException {
String sql = "select  tn.id, tn.content from t_news_detail tn where tn.section_id=267";
conn = JdbcUtilsOracle.getConnection();
st = conn.createStatement();
rs = st.executeQuery(sql);
while (rs.next()) {
System.out.println("id::::" + rs.getObject(1));

if (rs.getClob(2) != null) {
Reader inStreamDoc = rs.getClob(2).getCharacterStream();
// 取得clob的长度
char[] tempDoc = new char[(int) rs.getClob(2).length()];
inStreamDoc.read(tempDoc);
inStreamDoc.close();
System.out.println("clob::::" + new String(tempDoc));
}
}
JdbcUtilsOracle.free(rs, st, conn);
}

你可能感兴趣的:(jdbc)