oracle-解析CLOB格式字段转String


SQL CLOB 是  内置类型,它将字符大对象 (Character Large Object) 存储为数据库表某一行中的一个列值。默认情况下,  驱动程序使用 SQL locator(CLOB) 实现 Clob 对象,这意味着 CLOB 对象包含一个指向 SQL CLOB 数据的逻辑  指针而不是数据本身。Clob 对象在它被创建的事务处理期间有效。
在一些  数据库系统里,也使用Text 作为CLOB的别名,比如SQL Server。
Oracle数据库中如何将Clob查出并转换为String呢,有如下两个方法:
1、 --SQL 语句
select DBMS_LOB.SUBSTR(content,4000,1) ||   

       DBMS_LOB.SUBSTR(content,4000,4001) ||   

       DBMS_LOB.SUBSTR(content,4000,8001) ||   

       DBMS_LOB.SUBSTR(content,4000,12001)   

from xxx;

2、java中将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;
}

原文地址:https://www.linuxidc.com/Linux/2012-02/53393.htm


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM