try {
PreparedStatement stmt = session.connection().prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while (rs.next())
{
Clob clob = (Clob)rs.getObject(1);
result = ClobToString(clob);
}
} catch (HibernateException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
session.close();
}
//oracle.sql.Clob类型转换成String类型
public String ClobToString(Clob clob) throws SQLException, IOException {
String reString = "";
Reader is = clob.getCharacterStream();// 得到流
BufferedReader br = new BufferedReader(is);
String s = br.readLine();
StringBuffer sb = new StringBuffer();
while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
sb.append(s);
s = br.readLine();
}
reString = sb.toString();
return reString;
}
本文介绍了一种从Oracle数据库中读取Clob类型数据并将其转换为String的方法。通过使用PreparedStatement执行SQL查询,并借助BufferedReader逐行读取Clob内容,最终实现Clob到String的完整转换。
370

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



