java操作Oracle大数据类型BLOB的存取

本文介绍了一个Java类,用于处理Oracle数据库中的BLOB类型数据,包括文件的存储和读取。通过示例代码展示了如何使用PreparedStatement执行SQL语句,实现文件的插入与更新操作,并从数据库中读取文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

条件准备

Oracle数据库中有表如下
CREATE TABLE BOOK(

ID NUMBER PRIMARY KEY,

F BLOB

);

 ConnectionManager类不解释。

package org.db;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 此类掩饰java操作Oracle BLOB类型
 * @author wanghao
 *
 */
public class SaveFile {

    private final static String PSQL = "insert into BOOK(ID,F) " + "values(?,EMPTY_BLOB())";

    private final static String SSQL = "select F from BOOK where ID = ? for update";

    private final static String USQL = "update BOOK set F = ?  where ID = ?";
   
    /**
     * 将文件存入数据库
     * @param file    要存入数据库的文件
     * @param id    为了掩饰方便,手动输入 ID
     * @throws Exception
     */
    public void save(File file,int id) throws Exception {
        Connection conn = ConnectionManager.getConnection();
        conn.setAutoCommit(false);
        PreparedStatement pstmt = null;
        PreparedStatement pstmt2 = null;
        try {
            pstmt = conn.prepareStatement(PSQL);
            pstmt2 = conn.prepareStatement(USQL);
            pstmt.setInt(1, id);
            try {
                pstmt.executeUpdate();// 在数据库中插入空对象
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            pstmt = conn.prepareStatement(SSQL);
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();// 查询新插入的记录
            oracle.sql.BLOB pc = null;
            while (rs.next()) {
                pc = (oracle.sql.BLOB) rs.getBlob(1);
            }
            byte[] data = file2Byte(file);
            pc.putBytes(1, data);
            pstmt2.setBlob(1, pc);
            pstmt2.setInt(2, id);
            pstmt2.executeUpdate();
            conn.commit();
        } finally {
            try {
                pstmt.close();
                pstmt2.close();
            } catch (Exception EE) {
            }
        }
    }
   
    /**
     * 将文件转成byte数组
     * @param file   被转成 byte数组的file
     * @return
     * @throws Exception
     */
    public byte[] file2Byte(File file) throws Exception{
        FileInputStream fin = new FileInputStream(file);
        byte[] b = new byte[(int) file.length()];
        fin.read(b);
        fin.close();
        return b;
    }
   
    /**
     * 从数据库中将文件写入到本地硬盘
     * @param id
     * @throws Exception
     */
    public void readFile(int id) throws Exception{
        byte[] b = new byte[1024];
        Connection conn = ConnectionManager.getConnection();
        PreparedStatement pstmt = null;
        String sql = "select F from BOOK where ID = ?";
        pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, id);
        ResultSet rs = pstmt.executeQuery();// 查询新插入的记录
        oracle.sql.BLOB pc = null;
        while (rs.next()) {
            pc = (oracle.sql.BLOB) rs.getBlob(1);
        }
        InputStream in = pc.getBinaryStream();
        OutputStream os = new FileOutputStream(new File("T:\\io\\java_temp.pdf"));
        int len = 0;
        while((len=in.read(b))!=-1){
            os.write(b,0,len);
        }
        os.flush();
        os.close();
        in.close();
        rs.close();
        pstmt.close();
        conn.close();
    }

    public static void main(String[] args) throws Exception{
        SaveFile sf = new SaveFile();
//        File file = new File("T:\\io\\java.pdf");
        int id = 2;
//        sf.save(file, id);
        sf.readFile(id);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值