java clob-string轉換工具

[java] view plain copy
  1. import java.io.Reader;  
  2. import java.sql.Clob;  
  3.   
  4. public class ClobTransfer {  
  5.      /** 
  6.      * 將String轉成Clob ,靜態方法 
  7.      *  
  8.      * @param str 
  9.      *            字段 
  10.      * @return clob對象,如果出現錯誤,返回 null 
  11.      */  
  12.     public static Clob stringToClob(String str) {  
  13.         if (null == str)  
  14.             return null;  
  15.         else {  
  16.             try {  
  17.                 java.sql.Clob c = new javax.sql.rowset.serial.SerialClob(str  
  18.                         .toCharArray());  
  19.                 return c;  
  20.             } catch (Exception e) {  
  21.                 return null;  
  22.             }  
  23.         }  
  24.     }  
  25.   
  26.     /** 
  27.      * 將Clob轉成String ,靜態方法 
  28.      *  
  29.      * @param clob 
  30.      *            字段 
  31.      * @return 內容字串,如果出現錯誤,返回 null 
  32.      */  
  33.     public static String clobToString(Clob clob) {  
  34.         if (clob == null)  
  35.             return null;  
  36.         StringBuffer sb = new StringBuffer();  
  37.         Reader clobStream = null;  
  38.         try {  
  39.             clobStream = clob.getCharacterStream();  
  40.             char[] b = new char[60000];// 每次獲取60K  
  41.             int i = 0;  
  42.             while ((i = clobStream.read(b)) != -1) {  
  43.                 sb.append(b, 0, i);  
  44.             }  
  45.         } catch (Exception ex) {  
  46.             sb = null;  
  47.         } finally {  
  48.             try {  
  49.                 if (clobStream != null) {  
  50.                     clobStream.close();  
  51.                 }  
  52.             } catch (Exception e) {  
  53.             }  
  54.         }  
  55.         if (sb == null)  
  56.             return null;  
  57.         else  
  58.             return sb.toString();  
  59.     }  
  60.       
  61.     public static String clobToString(oracle.sql.CLOB clob){  
  62.         try{  
  63.             Reader inStream = clob.getCharacterStream();  
  64.             char[] c = new char[(int) clob.length()];  
  65.             inStream.read(c);  
  66.             String data = new String(c);  
  67.             inStream.close();  
  68.             return data;  
  69.         }catch(Exception e){  
  70.             e.printStackTrace();  
  71.             return "";  
  72.         }  
  73.     }  
  74. }  

源博客地址:http://blog.csdn.net/rcfalcon/article/details/7183918
發佈了44 篇原創文章 · 獲贊 34 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章