java 向数据库ORACLE写BLOB

package com.test;

import java.nio.ByteBuffer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class WriteBLOB {

    private Connection conn = null;

    private final static String PSQL = "insert into TESTBLOB(NUMCONTENTID,BLOBCONTENT) "
            + "values(?,EMPTY_BLOB())";

    private final static String SSQL = "select BLOBCONTENT from TESTBLOB where NUMCONTENTID = ? for update";

    private final static String USQL = "update TESTBLOB set BLOBCONTENT = ?  where NUMCONTENTID = ?";

//    public static String SQL_BLOBID = "select TESTBLOB_SEQ.nextVal from dual";    
    
    public WriteBLOB() throws Exception {
        Connection conn = null;
        try {
            conn = getConnection();
            conn.setAutoCommit(false);

            ByteBuffer bb = ByteBuffer.allocate(10836);
            bb.position(0);
            bb.putInt(1);

            //调用 insertBLOB 方法 的contentid 应该使用序列这里为了简单就直接写死了
           
            //下面的插入方式应该是你想要的方法 在数据库中会看到 0000 0001 0000 0000 0000....
            //但是你这里好像没有真正的是用 ByteBuffer
            byte [] b1 = bb.array();
            insertBLOB(conn, 1, b1);
           
            //下面是使用 ByteBuffer 后的插入 在数据库中会看到 0000 0001
            //不知道你想要什么样子的,你自己选择不吧
            bb.flip();
            byte [] b2 = new byte [bb.remaining()];
            bb.get(b2);
            insertBLOB(conn, 2, b2);
           
        } catch (Exception ex) {
            ex.printStackTrace();
            conn.rollback();
        } finally {
            if (conn != null)
                conn.close();
        }
    }

    public void insertBLOB(Connection conn, int contentid, byte[] content)
            throws Exception {
        PreparedStatement pstmt = null;
        PreparedStatement pstmt2 = null;
        int id = contentid;
        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 = content;
            pc.putBytes(1, data);

            pstmt2.setBlob(1, pc);
            pstmt2.setInt(2, id);
            pstmt2.executeUpdate();

        } finally {
            try {
                pstmt.close();
                pstmt2.close();
            } catch (Exception EE) {
            }
        }
        return;
    }

    public static Connection getConnection() throws Exception {
        Connection conn = null;
        Statement stmtTemp = null;
        Class.forName("oracle.jdbc.driver.OracleDriver");
        conn = DriverManager.getConnection(
                "jdbc:oracle:thin:@192.168.1.26:1521:orcl", "oracl", "123456");
        stmtTemp = conn.createStatement();
        return conn;
    }

    public static void main(String[] args) {
        try {
            WriteBLOB wb = new WriteBLOB();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值