Java将zip存入oralce的blob

加入4个jar包(按照一下顺序加入):

ojdbc14.jar

com.bea.core.datasource6.binding_1.4.0.0.jar

com.bea.core.datasource6_1.4.0.0.jar

com.bea.core.datasource_1.4.0.0.jar

 

//后三个包可以用weblogic内的bea\weblogic81\server\lib下的weblogic.jar

但是在8启动时服务稍慢,11g时会快!网上说是因为weblogic区分jar浪费时间

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;

public class blob1 {

 /**
  * @param args
  */
 public static boolean insertFile(String table, String cpcode, String sbym,
  String fileName, String file_) {
  String strSql = "";
  Connection conn = null;
  Statement pstmt = null;
  FileInputStream fs = null;
  long flength = 0;
  File file = new File(file_);
  if (file.exists()) {
   try {
    fs = new FileInputStream(file_);
   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   flength = file.length();
  } else {
   return false;
  }
  try {
   
    conn = DBConnection.getConnection();
    if(table.equals("zsfkinfo")){
    strSql = "insert into "
      + table
      + " (id,sb_id,cpcode,sb_ym,cpemail,subject,body,filename,attachment,FK_FLAG,flag,note,maildate) values("+table+"_ID_SEQ.NEXTVAL,"+table+"_ID_SEQ.NEXTVAL,"+cpcode+","+sbym+",'123','123','123','"+fileName+"',empty_blob(),'R','R','123',sysdate)";
    }else if(table.equals("fkinfo")){
     strSql = "insert into "
      + table
      + " (id,sb_id,cpcode,sb_ym,cpemail,subject,body,filename,attachment,FK_FLAG,flag,maildate,fk_code) values("+table+"_ID_SEQ.NEXTVAL,"+table+"_ID_SEQ.NEXTVAL,"+cpcode+","+sbym+",'123','123','123','"+fileName+"',empty_blob(),'R','R',sysdate,'123')";
    }else if(table.equals("dzfkinfo")){
     strSql = "insert into "
      + table
      + " (id,sb_id,cpcode,sb_ym,cpemail,subject,body,filename,attachment,FK_FLAG,flag,maildate,fk_code) values("+table+"_ID_SEQ.NEXTVAL,"+table+"_ID_SEQ.NEXTVAL,"+cpcode+","+sbym+",'123','123','123','"+fileName+"',empty_blob(),'R','R',sysdate,'123')";
    }
   conn.setAutoCommit(false);
   pstmt = conn.createStatement();
   pstmt.executeUpdate(strSql);
   String strSql2 =null;
   
    strSql2 = "SELECT attachment FROM "+table+" WHERE id=(select max(id) from "+table+") and cpcode="+cpcode+" and sb_ym="+sbym+" FOR UPDATE";
  
   ResultSet rs = pstmt.executeQuery(strSql2);
   while (rs.next()) {
    System.out.println( rs.getBlob(1).getClass());
    /* 取出此BLOB对象 */
    weblogic.jdbc.vendor.oracle.OracleThinBlob blob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) rs.getBlob(1);
             //os.write(arrayByte, iStartData, flength);
    OutputStream outStream = blob.getBinaryOutputStream();
    byte[] b = new byte[blob.getBufferSize()];

            int len = 0;

            try {
       while ( (len = fs.read(b)) != -1) {

         outStream.write(b, 0, len);

       }
       fs.close();

       outStream.flush();

       outStream.close();

       conn.commit();

      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }

   }   
   try {
    fs.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   return true;
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   try {
    if (pstmt != null) {
     pstmt.close();
    }
    if (conn != null) {
     conn.close();
    }
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  return false;
 }
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  //blob1 b = new blob1();
  
  //blob1.insertFile("zsfkinfo","2102210097","201203","2102210097_wm20120328.zip","C:/Documents and Settings/Administrator/桌面/fk/2102210097_wm20120328.zip");
  blob1.insertFile("fkinfo","2102230429","201203","ysfk_xgm_2102230429.zip","C:/Documents and Settings/Administrator/桌面/fk/ysfk_xgm_2102230429.zip");
  blob1.insertFile("dzfkinfo","2102230429","201203","fk_xgm.zip","C:/Documents and Settings/Administrator/桌面/fk/fk_xgm.zip");

 


  
 }

}

你可能感兴趣的:(java,String,File,table,insert,byte)