栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 数据库 > Oracle

解析jdbc处理oracle的clob字段的详解

Oracle 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解析jdbc处理oracle的clob字段的详解

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
复制代码 代码如下:
public class ClobUtil {

 
 public static Boolean clobInsert(String insertSQL,String updateSQL,Connection con,String bigString,String updateColumn ) throws SQLException{
      // 结果集
      ResultSet rs = null;
      // 插入数据库的sql
      String query = insertSQL;
      // 设置不自动提交
      con.setAutoCommit(false);
      // 定义预处理
      java.sql.PreparedStatement pstmt = con.prepareStatement( query);
      // 执行插入语句
      pstmt.executeUpdate();
      //清空
      pstmt = null;
      // 执行更改
      query = updateSQL;
         //显示执行带有修改方式的select
      pstmt = con.prepareStatement(query);
      rs = pstmt.executeQuery();
      // 采用流的方式处理结果集
      if(rs.next()) 
      {
        // 得到指定的clob字段
        oracle.sql.CLOB singnaturedateClob = (oracle.sql.CLOB)rs.getClob(updateColumn); 
        // 把clob字段放到输出流当中
        BufferedOutputStream out = new BufferedOutputStream(singnaturedateClob.getAsciiOutputStream());
        // 判断传入的数据是否为空
        if(bigString!=null){
        try{
         // 把要保存的数据转换成输入流
         InputStream  is = (InputStream)(new   ByteArrayInputStream(bigString.getBytes())); 
      copyStream( is, out );
      out.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
        }
      }
      rs.close();
      con.commit();

   return true;
 }

 
 public static void copyStream( InputStream is, OutputStream os )
 throws IOException
 {
  byte[] data             = new byte[4096];
  int readed              = is.read(data);
  while (readed != -1)
  {
   os.write(data,0,readed);
   readed = is.read(data);
  }
 }

 
 public static String getClobString(Clob c) { 
        try {

            Reader reader=c.getCharacterStream();
            if (reader == null) {
                return null;
            }
            StringBuffer sb = new StringBuffer();
            char[] charbuf = new char[4096];
            for (int i = reader.read(charbuf); i > 0; i = reader.read(charbuf)) {
                sb.append(charbuf, 0, i);
            }
            return sb.toString();
        } catch (Exception e) {
            return "";
        }
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/172065.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号