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