Java oracle (BLOB)资料操作

Java oracle (BLOB)文件操作

package com.able.demo;
import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.SQLException;

import com.able.dao.JdbcOracleUtil;
 
 
/* 
 * 建数据表 

create table ZIPDATA 

  DATATYPE VARCHAR2(20), 
  DATACONTENT  BLOB 

*/ 
 
/**  
 * 
 * @author 刘勋 
 */ 
public class TestZip {  
     Connection conn = null;  
       
   public TestZip(){  
    JdbcOracleUtil util = JdbcOracleUtil.getInstance();
    conn=util.getConnection();
   }  
   /**
    * 写入
    * @param file
    * @throws SQLException
    * @throws IOException
    */
   public void write(String file) throws SQLException, IOException{  
       java.sql.Statement stmt = null;   
         java.sql.Statement stmt2 = null;   
         java.sql.ResultSet rs = null;  
       conn.setAutoCommit(false);  
       String sql = "insert into ZIPDATA(DATATYPE,DATACONTENT) values('1',empty_blob())";  
       stmt = conn.createStatement();  
       stmt.executeUpdate(sql);  
         
       String sql2 = "select DATACONTENT from ZIPDATA where DATATYPE = '1' for update";  
       stmt2 = conn.createStatement();  
       rs = stmt2.executeQuery(sql2);  
       if(rs.next()){
        oracle.sql.BLOB b = (oracle.sql.BLOB)rs.getBlob(1);  
        java.io.OutputStream os = b.getBinaryOutputStream();  
        java.io.InputStream is = new java.io.FileInputStream(file);  
        int i = 0;
           while((i = is.read()) != -1){  
             os.write(i);  
           }  
       if(is!=null){  
           is.close();     
       }  
       if(os!=null){  
           os.close();     
       }  
       conn.commit();     
       System.out.println("写数据成功!");  
       }  
   }  
     
   /**
    * 读取
    * @param file
    * @throws SQLException
    * @throws IOException
    */
   private void read(String file) throws SQLException, IOException {  
        
      java.sql.Statement stmt = null;   
      java.sql.ResultSet rs = null;  
         
       String sql = "select * from mms_send_content where id=22321";  
       stmt = conn.createStatement();  
       rs = stmt.executeQuery(sql);  
       if(rs.next()){  
           oracle.sql.BLOB b = (oracle.sql.BLOB)rs.getBlob(5);  
           java.io.InputStream is = b.getBinaryStream();
           java.io.FileOutputStream fos = new java.io.FileOutputStream(file);
           int i = 0;  
           while((i = is.read())!= -1){  
            fos.write(i);  
           }  
           if(fos!=null){  
               fos.close();  
           }  
           if(is!=null){  
               is.close();     
           }  
             
       } 
       System.out.println("读取数据成功!");
    }  
      /**
       * 修改
       * @param file
       */
   private void update(String file) {  
       java.sql.Statement stmt = null;   
         java.sql.ResultSet rs = null;  
         OutputStream os=null;  
         InputStream is=null;  
         try {  
           conn.setAutoCommit(false);  
          stmt=conn.createStatement();  
          rs=stmt.executeQuery("select DATACONTENT from ZIPDATA where DATATYPE = '1' for update");  
           
           if(rs.next()){  
                oracle.sql.BLOB b = (oracle.sql.BLOB)rs.getBlob(1);
                 os = b.getBinaryOutputStream();  
                 is = new java.io.FileInputStream(file);  
                int i = 0;  
                   while((i = is.read()) != -1){  
                     os.write(i);  
                   }  
               System.out.println("写数据成功!");  
               }  
            conn.commit();     
            } catch (SQLException e) {  
                e.printStackTrace();  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }finally{  
                 try {  
                   if(is!=null){  
                        is.close();  
                    }   
                   if(os!=null){  
                       os.close();     
                   }  
                  }catch (IOException e) {  
                        e.printStackTrace();  
                    }  
                   }  
                     
            }  
     
     
   public static void main(String args[]) throws SQLException, IOException{  
    TestZip er=new TestZip();
//       er.write("D:\\1.zip");//读取文件系统,写blob  
         er.read("D:\\2.zip");//读取blob,写文件系统  
//       er.update("D:\\wulihai\\写给亲爱的猪.doc");//读取文件系统,更新blob  
   }