处理blob和clob的通用类

  1. public class LobUtil {
  2.     private static final int BUFFER_SIZE = 4096;
  3.     private LobUtil() {
  4.     }
  5.     public static synchronized void writeBLOB(ResultSet rs, String fieldName,
  6.             byte[] buf) throws SQLException, IOException {
  7.         if (buf == null) {
  8.             return;
  9.         }
  10.         OutputStream os = null;
  11.         Blob b = rs.getBlob(fieldName);
  12.         if (b == null) {
  13.             throw new SQLException("刚刚插入或更新的BLOB指针为空!");
  14.         } else {
  15.             // if (Conn.isUseConnectionPool()){
  16.             // os =
  17.             // ((weblogic.jdbc.vendor.oracle.OracleThinBlob)b).getBinaryOutputStream();
  18.             // }
  19.             // else{
  20.             // os = ((oracle.sql.BLOB)b).getBinaryOutputStream();
  21.             // }
  22.             /**
  23.              * @todo 需要最终改造为通用数据库处理接口
  24.              */
  25.             // <modify author="jdyao" time="2005-07-26">
  26.             try {
  27.                 Method methodToInvoke = b.getClass().getMethod(
  28.                         "getBinaryOutputStream", (Class[]) null);
  29.                 os = (OutputStream) methodToInvoke.invoke(b, (Object[]) null);
  30.             } catch (Exception e) {
  31.                 throw new SQLException(e.getMessage());
  32.             }
  33.             // os = ( (oracle.sql.BLOB) b).getBinaryOutputStream();
  34.             // </modify>
  35.             BufferedOutputStream bos = new BufferedOutputStream(os);
  36.             bos.write(buf);
  37.             bos.flush();
  38.             bos.close();
  39.         }
  40.     }
  41.     public static synchronized void writeBLOB(ResultSet rs, String fieldName,
  42.             InputStream in) throws SQLException, IOException {
  43.         if (in == null) {
  44.             return;
  45.         }
  46.         OutputStream os = null;
  47.         Blob b = rs.getBlob(fieldName);
  48.         if (b == null) {
  49.             throw new SQLException("刚刚插入或更新的BLOB指针为空!");
  50.         } else {
  51.             // if (Conn.isUseConnectionPool()){
  52.             // os =
  53.             // ((weblogic.jdbc.vendor.oracle.OracleThinBlob)b).getBinaryOutputStream();
  54.             // }
  55.             // else{
  56.             // os = ((oracle.sql.BLOB)b).getBinaryOutputStream();
  57.             // }
  58.             // <modify author="jdyao" time="2005-07-26">
  59.             try {
  60.                 Method methodToInvoke = b.getClass().getMethod(
  61.                         "getBinaryOutputStream", (Class[]) null);
  62.                 os = (OutputStream) methodToInvoke.invoke(b, (Object[]) null);
  63.             } catch (Exception e) {
  64.                 throw new SQLException(e.getMessage());
  65.             }
  66.             // os = ( (oracle.sql.BLOB) b).getBinaryOutputStream();
  67.             // </modify>
  68.             BufferedOutputStream bos = new BufferedOutputStream(os);
  69.             BufferedInputStream bis = new BufferedInputStream(in);
  70.             byte buf[] = new byte[BUFFER_SIZE];
  71.             int len = -1;
  72.             while ((len = bis.read(buf)) != -1) {
  73.                 bos.write(buf, 0, len);
  74.             }
  75.             bis.close();
  76.             bos.flush();
  77.             bos.close();
  78.         }
  79.     }
  80.     public static synchronized void writeCLOB(ResultSet rs, String fieldName,
  81.             String buf) throws SQLException, IOException {
  82.         if (buf == null) {
  83.             return;
  84.         }
  85.         Writer wr = null;
  86.         Clob c = rs.getClob(fieldName);
  87.         if (c == null) {
  88.             throw new SQLException("刚刚插入或更新的CLOB指针为空!");
  89.         } else {
  90.             // <modify author="jdyao" time="2005-07-26">
  91.             try {
  92.                 Method methodToInvoke = c.getClass().getMethod(
  93.                         "getCharacterOutputStream", (Class[]) null);
  94.                 wr = (Writer) methodToInvoke.invoke(c, (Object[]) null);
  95.             } catch (Exception e) {
  96.                 throw new SQLException(e.getMessage());
  97.             }
  98.             // wr = ((oracle.sql.CLOB) c).getCharacterOutputStream();
  99.             // </modify>
  100.             BufferedWriter bw = new BufferedWriter(wr);
  101.             bw.write(buf);
  102.             bw.flush();
  103.             bw.close();
  104.         }
  105.     }
  106.     public static synchronized void writeCLOB(ResultSet rs, String fieldName,
  107.             InputStream in) throws SQLException, IOException {
  108.         if (in == null) {
  109.             return;
  110.         }
  111.         Writer wr = null;
  112.         Clob c = rs.getClob(fieldName);
  113.         if (c == null) {
  114.             throw new SQLException("刚刚插入或更新的CLOB指针为空!");
  115.         } else {
  116.             //<modify author="jdyao" time="2005-07-26">
  117.             try {
  118.               Method methodToInvoke = c.getClass().getMethod(
  119.                   "getCharacterOutputStream", (Class[]) null);
  120.               wr = (Writer) methodToInvoke.invoke(c, (Object[]) null);
  121.             } catch (Exception e) {
  122.               throw new SQLException(e.getMessage());
  123.             }
  124.             //wr = ((oracle.sql.CLOB) c).getCharacterOutputStream();
  125.             //</modify>
  126.             
  127.             BufferedWriter bw = new BufferedWriter(wr);
  128.             BufferedReader br = new BufferedReader(new InputStreamReader(in));
  129.             char buf[] = new char[BUFFER_SIZE];
  130.             int len = -1;
  131.             while ( (len = br.read(buf)) != -1) {
  132.               bw.write(buf, 0, len);
  133.             }
  134.             br.close();
  135.             bw.flush();
  136.             bw.close();
  137.         }
  138.     }
  139. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值