jdbc処理oracleのclobフィールドの詳細を解析します。


import java.io.BufferedOutputStreamimport java.io.Byteary InputStream;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.Result Set;import java.sql.SQLException;

public class ClobUtil {

 /**
  *
  * @param insertSQL sql clob , empty_clob() :insert into ba valus(1,empty_clob())
  * @param updateSQL , . :select * from BA where ba_id = '"+ba.getBA_id()+"' for update
  * @param con
  * @param bigString clob
  * @param updateColumn
  * @return
  * @throws SQLException
  */
 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;
 }

 /**
  *
  * @param is
  * @param os
  * @throws IOException
  */
 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);
  }
 }

 /**
  * Clob
  * @param c
  * @return
  */
 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 "";
        }
    }

}