六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 40|回复: 0

Oracle Blob (二进制文件的读写)

[复制链接]

升级  0%

56

主题

56

主题

56

主题

举人

Rank: 3Rank: 3

积分
200
 楼主| 发表于 2013-1-15 02:49:36 | 显示全部楼层 |阅读模式
import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.sql.Connection;import java.sql.SQLException;/* * 建数据表create table IMAGE_TAB(  IMG_ID VARCHAR2(2),  IMAGE  BLOB)*//**  * * @author wulihai */public class test { Connection conn = null;    public test(){   conn=DBUtil.getConn();   }   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 image_tab(img_id,image) values(1,empty_blob())";   stmt = conn.createStatement();   stmt.executeUpdate(sql);              String sql2 = "select image from image_tab where img_id = 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);       }    conn.commit();              if(is!=null){       is.close();          }       if(os!=null){       os.close();          }       System.out.println("写数据成功!");       }   }      private void read(String file) throws SQLException, IOException {    java.sql.Statement stmt = null;   java.sql.ResultSet rs = null;      String sql = "select image from image_tab where img_id=1 ";   stmt = conn.createStatement();       rs = stmt.executeQuery(sql);       if(rs.next()){       oracle.sql.BLOB b = (oracle.sql.BLOB)rs.getBlob(1);       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();          }              }       }      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 image from image_tab where img_id=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{   test er=new test();   er.write("D:\\wulihai\\合影.jpg");//读取文件系统,写blob   er.read("D:\\wulihai\\合影2.jpg");//读取blob,写文件系统   er.update("D:\\wulihai\\写给亲爱的猪.doc");//读取文件系统,更新blob   er.read("D:\\wulihai\\写给亲爱的猪2.doc");//再次读取blob,写文件系统   }       }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表