java获取oracle中CLOB字段,转换成String

来源:互联网 发布:软件著作权登记机构 编辑:程序博客网 时间:2024/05/06 08:22

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;
}