在oracle 数据库中使用 Blob 字段存储 一张图片并读取

1. 进行数据库的设置

 create table image_lob(t_id varchar2(5) not null,t_image blob not null);
 
 create or replace directory "images" as 'f:\pic\';
 
 create or replace procedure img_insert(tid varchar2,filename varchar2)as
     f_lob bfile;
    b_lob blob;
    begin
    insert into image_lob(t_id,t_image)
    values(tid,empty_blob())return t_image into b_lob;
    f_lob:=bfilename('images',filename);
    dbms_lob.fileopen(f_lob,dbms_lob.file_readonly);
    dbms_lob.loadfromfile(b_lob,f_lob,
    dbms_lob.getlength(f_lob));
    dbms_lob.fileclose(f_lob);
    commit;
    end;
    /

在 f:\pic 文件夹下(这个需要自己建立), 一张图片 cat.png 
exec img_insert('1','cat.png');  或者  CALL IMG_INSERT('2','cat.png');

--查看的代码
select * from image_lob;
select t_image from image_lob where t_id = '1' for update;
 
 
2. 使用 java 对 blob 数据库进行读取 
package JavaTest;


import SQLUtils.SQLUtils;


import java.sql.Blob;
import java.sql.Connection;
import java.sql.ResultSet;


public class CallBlob {
    public CallBlob() {
        super();
    }
          
        public static void main(String[] args)  
        {  
            try  
            {  
                //1. 建立连接  
                String driveName = "oracle.jdbc.driver.OracleDriver" ; 
                String url = "jdbc:oracle:thin:@localhost:1521:orcl";  
                String userName = "hr"; 
                String passWord="hr";
                Connection conn =  SQLUtils.getConnection(driveName, url, userName, passWord, false);
                  
                //2. 查询数据  
                String sql = "select t_image from image_lob where t_id = '2'";  
                ResultSet rs = SQLUtils.getResultSet(conn, sql);
               
                //3. 读取Blob类型数据 ,并写入对应的位置 
                Blob blob = SQLUtils.getBlob(rs, 1);
                byte[] temp = new byte[(int)blob.length()];  
                String fileUrl = "E://img.png" ; 
                SQLUtils.writePicToSomeWhere(blob, temp, fileUrl);
            } catch (Exception e)  
            {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }  
}



package SQLUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public final  class SQLUtils {
    public SQLUtils() {
        super();
    }
    /**
     *根据相关的条件,返回一个  jdbc 的连接 
     * @param driverName 驱动名称
     * @param connUrl 连接字符串
     * @param userName 数据库名称
     * @param passWord 数据库密码
     * @param isCommit 是否自动提交 sql 语句 
     * @return 一个 jdbc 的 connecion  
     */
    public static Connection getConnection(String driverName, String connUrl , String userName, String passWord,Boolean isCommit){
        Connection conn = null ; 
        try {
            Class.forName(driverName);
             conn = DriverManager.getConnection(connUrl,userName,passWord);  
            conn.setAutoCommit(isCommit);  
        } catch (ClassNotFoundException e) {
            System.out.println("没有找到驱动异常!");
        } catch (SQLException e) {
            System.out.println("连接字符串错误或者数据库用户名密码错误异常!");
        }
        return conn; 
    }
   /**
     *根据 connection 和 sql 语句 , 返回一个 ResultSet 对象 
     * @param conn 数据库连接的 Connection 
     * @param sql 要执行的 SQL语句 
     * @return 得到 ResultSet 对象 
     */
    public static ResultSet getResultSet(Connection conn , String sql){
        ResultSet rs = null ; 
        try {
           Statement stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);  
        } catch (SQLException e) {
            System.out.println("sql 语句执行失败!");
        }
        return rs ; 
    }
   /**
     *根据   ResultSet 和 index 获取Blob 数据 
     * @param rs    数据返回的 ResultSet 
     * @param index 数据库表中的 字段位置, 从一开始的
     * @return 读取的 Blob 数据 
     */
   public static Blob getBlob(ResultSet rs, int index){
       Blob blob = null;
        try {
            if(rs.next())
       {
           blob = rs.getBlob(index);
       }
        } catch (SQLException e) {
            System.out.println("读取Blob 数据失败, 请检查 index 的位置!");
        }
        return blob ; 
   }
   /**
     *将获得的 Blob 数据, 存储到硬盘指定的位置 
     * @param blob
     * @param temp
     * @param fileUrl
     */
   public static void writePicToSomeWhere(Blob blob,byte[] temp,String fileUrl){
       InputStream in = null;
       FileOutputStream fout = null ; 
        try {
            in = blob.getBinaryStream();
            in.read(temp);
            File file = new File(fileUrl);
            fout = new FileOutputStream(file);
            fout.write(temp);
        } catch (SQLException e) {
            System.out.println("从 blob 获得inputStream失败!");
        } catch (IOException e) {
            System.out.println("io 读写失败!");
            }  finally  {
            try{
                in.close();  
                fout.close();
            }   catch   (Exception e){
                System.out.println("输入流或输出流关闭失败!");
            }
            }
   }

}





转载于:https://my.oschina.net/u/193184/blog/352698

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值