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