java、oracle对CLOB处理

oracle CLOB字段转换位VARCHAR

1.实际上处理CLOB字段的时候,直接TO_CHAR,当长度超过4000的时候,会报错,提示列被截取;

CLOB转varchar2:select to_char(CLOB字段) from table

 

2.直接使用SUBSTR对CLOB字段进行截取,是不能起到任何作用的;

 

3.可以使用dbms_lob.substr(clobcolumn,4000),对CLOB字段进行截取;截取的长度是4000还是2000根据存储的是汉字和数据决定长度;

 

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

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

  

posted @ 2017-10-20 21:55  N神3  阅读(2694)  评论(0编辑  收藏  举报