日期:2014-05-16  浏览次数:20303 次

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)
        {
           
        }
&nb