【java/oracle】往blob字段里写入数据例子

表:

create table tb0504(
    id number(12),
    content blob,
    primary key(id)
);

写入代码:

package com.hy.lab.blob;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class BlobWriter {
    //-- 以下为连接Oracle数据库的四大参数
    private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
    private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
    private static final String USER = "luna";
    private static final String PSWD = "1234";

    public static void main(String[] args){
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(URL, USER, PSWD);

            String insertSql="insert into tb0504(id,content) values(?,?)";
            pstmt = conn.prepareStatement(insertSql);

            // 方式一:文件转byte数组
            InputStream is1=new FileInputStream("c:\\temp1\\2.jpg");
            byte[] arr=new byte[is1.available()];
            is1.read(arr);
            Blob blob= conn.createBlob();
            blob.setBytes(1,arr);
            pstmt.setLong(1,1);
            pstmt.setBlob(2,blob);
            pstmt.executeUpdate();

            // 方式二:文件直接给blob字段
            InputStream is2=new FileInputStream("c:\\temp1\\picturesShow1022final_src_210808.rar");
            pstmt.setLong(1,2);
            pstmt.setBlob(2,is2);
            pstmt.executeUpdate();

            // 方式三:设置blob字段的二进制流
            InputStream is3=new FileInputStream("c:\\temp1\\media-cool-master.zip");
            pstmt.setLong(1,3);
            pstmt.setBinaryStream(2,is3);
            pstmt.executeUpdate();

            System.out.println("完成写入");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                pstmt.close();
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

查询代码:

SQL> select * from tb0504;

        ID
----------
CONTENT
--------------------------------------------------------------------------------
         1
FFD8FFE000104A46494600010201004800480000FFE112424578696600004D4D002A000000080007
011200030000000100010000011A00050000000100000062011B0005000000010000006A01280003

         2
526172211A0700CF907300000D0000000000000030F77460903000190000001900000002643A1CDE
A50608531D300B002000000073657474696E672E74787400F0C63A43433A5C68795C7069635C6761

         3

        ID
----------
CONTENT
--------------------------------------------------------------------------------
504B03040A00000000006EA15B54000000000000000000000000120009006D656469612D636F6F6C
2D6D61737465722F5554050001706A1B62504B03040A00000008006EA15B549572CDAD17020000AD


SQL> select id,length(content) from tb0504;

        ID LENGTH(CONTENT)
---------- ---------------
         1          529688
         2          400741
         3         4651776

SQL>

可见三个文件内容确已写入。

END

 

posted @ 2022-05-04 14:12  逆火狂飙  阅读(507)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东