首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

java 跟Blob交互

2012-07-23 
java 和Blob交互1.如何从数据库中读出BLOBimport java.io.Fileimport java.sql.Blobimport java.sql.Res

java 和Blob交互
1.如何从数据库中读出BLOB
import java.io.File;
import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.Statement;


public class Callora {

    public static void main(String[] args) {
try {

            getEmployee();
        } catch (Exception e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
    }
   
     static void getEmployee() throws Exception {
   
        Class.forName("oracle.jdbc.OracleDriver");
        java.sql.Connection con = null;
        String url="jdbc:oracle:thin:@10.5.241.29:1521:ncore";
       
        String user = "intoper";
        String password = "rabbit";
        String sql = "SELECT AUDIT_DATA FROM EAI_AUDIT WHERE interface_TYPE='123'";
        String sql1 = "SELECT AUDIT_DATA FROM EAI_AUDIT";
        Blob blob = null;
         byte b[]=null;//保存从BLOB读出的字节
         int i=0;
        
        try {
        
          con = java.sql.DriverManager.getConnection(url,user,password);
          Statement stmt = con.createStatement();
          ResultSet rs = stmt.executeQuery("select MSG_MEMO from INT_QMSG_MEMO" +
                " where CREATE_TIME >to_date('2006-9-28 18:34','yyyy-MM-dd HH24:mi') " +
                "and CREATE_TIME <to_date('2006-9-28 18:40','yyyy-MM-dd HH24:mi')  order by CREATE_TIME desc");
          while(rs.next()){
              java.io.BufferedOutputStream bos = new java.io.BufferedOutputStream(new java.io.FileOutputStream(new File("c:\\aaa\"+i+".xml")));
              blob = rs.getBlob(1);
              //保存从BLOB读出的字节
              b = blob.getBytes(1,(int)blob.length());
              System.out.println(1);
              bos.write(b,0,b.length);
              i++;
            bos.close();
          }
        }
        catch(Exception e)
        {
           
        }
        finally {
          try {
            con.close();
          }
          catch (Exception e) {
            e.printStackTrace();
          }
        }


      }
}

如何写入BLOB
import java.util.*;
import java.sql.*;
import java.io.*;
import oracle.sql.*;

public class ToOracle
{
    Connection con = null;
    PreparedStatement prst = null;
    Statement  stmt = null;
    ResultSet rs = null;
    public ToOracle()
    {
        run();
    }
   
    void run()
    {
        String Driver_Name="oracle.jdbc.driver.OracleDriver";
        String url="jdbc:oracle:thin:@10.5.228.37:1521:billtest";
        String FilePath = "C:\\xu\\2006_09_27 14_53_31\";
        File property = new File(FilePath+"filerecord.txt");
        File COMPONENT = null; 
        BufferedReader readin = null;
        InputStream sampleFileStream=null;
        byte[] buffer = null;
        String temp =null;
        String[] buf=null;
        String special = "275_277";
        int product_type=0;
       
        try
        {
            Class.forName(Driver_Name);
            con=DriverManager.getConnection(url,"situser","situser");
            con.setAutoCommit(false);
            readin = new BufferedReader(new InputStreamReader(new FileInputStream(property)));
           
            while((temp=readin.readLine())!=null)
            {
                buf=temp.split("__");
                if(buf.length==4)
                {
                    COMPONENT = new File(FilePath+"COMPONENT  "+buf[0]+".xml");
                    if((special.indexOf(buf[2].toString()))!=-1)
                    {
                        if(buf[2].toString()=="275")
                           product_type=4;
                        else
                           product_type=1;
                        prst=con.prepareStatement("insert into did_root_order(order_id,order_type,crm_integration_id,order_date,eai_order_data,root_si_code,product_type,has_asst_change)"+
                                                   " values(?,0,?,to_date('"+buf[3]+"','yyyy-MM-dd HH24:mi:ss'),EMPTY_BLOB(),?,?,0)");
                        prst.setString(1,buf[0].toString());
                        prst.setString(2,buf[1].toString());
                        prst.setString(4,buf[2].toString());
                        prst.setInt(5,product_type);
                        prst.execute();
                       
                        stmt=con.createStatement();
                        rs=stmt.executeQuery("select eai_order_data from did_root_order where order_id='"+buf[0]+"' for update");

                        if(rs.next())
                        {
                            Blob b = rs.getBlob(1);
                            OutputStream blobOutputStream = ((BLOB)b).getBinaryOutputStream();
                            sampleFileStream = new FileInputStream(COMPONENT);
                            buffer = new byte[10* 1024];
                            int nread = 0;
                            while( (nread= sampleFileStream.read(buffer)) != -1 )
                               blobOutputStream.write(buffer, 0, nread);
                            sampleFileStream.close();
                            blobOutputStream.close();                               
                        }
                    }
                    else
                    {
                        if(buf[2].toString()=="276")
                           product_type=5;
                        else if(buf[2].toString()=="278")
                           product_type=2;
                        else
                           product_type=1;
                          
                        prst=con.prepareStatement("insert into did_sub_order(order_id,order_type,crm_integration_id,parent_asset_id,eai_order_data,sub_si_code,product_type,order_date)"+
                                                   " values(?,0,?,'0',EMPTY_BLOB(),?,?,to_date('"+buf[3]+"','yyyy-MM-dd HH24:mi:ss'))");
                        prst.setString(1,buf[0].toString());
                        prst.setString(2,buf[1].toString());
                        prst.setString(3,buf[2].toString());
                        prst.setInt(4,product_type);
                        prst.execute();   
                       
                        stmt=con.createStatement();
                        rs=stmt.executeQuery("select eai_order_data from did_sub_order where order_id='"+buf[0]+"' for update");
                       
                        if(rs.next())
                        {
                            Blob b = rs.getBlob(1);
                            OutputStream blobOutputStream = ((BLOB)b).getBinaryOutputStream();
                            sampleFileStream = new FileInputStream(COMPONENT);
                            buffer = new byte[10* 1024];
                            int nread = 0;
                            while( (nread= sampleFileStream.read(buffer)) != -1 )
                               blobOutputStream.write(buffer, 0, nread);
                            sampleFileStream.close();
                            blobOutputStream.close();                              
                        }                                       
                    }
                }
            }
        }
        catch(Exception e)
        {
            System.out.println("oracle initial error");
        }       
    }
   
    public static void main(String[] args)
    {
        ToOracle t = new ToOracle();
    }
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xuqian_2004/archive/2006/09/29/1306261.aspx

热点排行