以Blob形式存取图片到Oracle

本文介绍了如何使用Java程序实现图片文件在Oracle数据库中的存储与读取。包括创建Blob对象、写入图片到数据库以及从数据库读取图片并保存到指定路径。

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

将图片存入Oracle数据库

    public int writeBlob(String path,String id) throws SQLException, IOException{
        Connection conn = null;
        PreparedStatement ps =null;
        conn = getConnetion();
        int result = 0;
        String sql = "insert into man(id,picture) values(?,?)";
        conn = getConnetion();
        try {
            ps = conn.prepareStatement(sql);
            //1.����Blob
            Blob image = conn.createBlob();
            //2.��������blob
            OutputStream out = image.setBinaryStream(1);
            //3.��ȡͼƬ,��д�������
            FileInputStream fis = new FileInputStream(path);
            byte []buf = new byte[1024];
            int len = 0;
            while((len=fis.read(buf))!=-1){
                out.write(buf, 0, len);
            }
            ps.setObject(1,id);
            ps.setBlob(2,image);
            result = ps.executeUpdate();
            fis.close();
            out.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if (ps != null) {
                    ps.close();
                }

                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                System.out.print("error close");
            }
        }

        return result;
    }

将图片从数据库中取出

    //从数据库中获取图片,path是保存路径
    //get picture from oracle
    public void downPicture(String id, String path){
        Connection conn = null;
        PreparedStatement ps =null;
        ResultSet rs = null;
        ResultSetMetaData rsmd = null;
        ArrayList<HashMap<String,Object>> result = new ArrayList<HashMap<String,Object>>();
        try {
            String sql = "Select * from man Where ID = ?";
            conn = getConnetion();
            ps =conn.prepareStatement(sql);
            ps.setObject(1, id);
            rs = ps.executeQuery();
            Blob blob =null;
            while(rs.next()) {
                blob = rs.getBlob("picture");
                break;
            }
            InputStream ins = blob.getBinaryStream();
            path = path + "jppicture" + id + ".jpg";
            File file = new File(path);
            if(!file.exists()){
                file.createNewFile();
            }
            OutputStream fos = new FileOutputStream(file);
            byte[] b = new byte[1024];
            int len = 0;
            while((len = ins.read(b))!=-1){
                fos.write(b,0,len);
            }
            fos.close();
            ins.close();
        }catch (Exception e){
            System.out.print("something error");
        }finally {
            try {

                if(rs != null){
                    rs.close();
                }

                if(ps != null){
                    ps.close();
                }

                if(conn != null){
                    conn.close();
                }
            } catch (SQLException e) {
                System.out.print("���ݿ�رճ���");
            }

        }

    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值