java clob-string转换工具

来源:互联网 发布:淘宝衣服商城男装 编辑:程序博客网 时间:2024/05/21 17:09
[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
0 0
原创粉丝点击