java 向資料庫ORACLE寫BLOB

來源:互聯網
上載者:User

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();//在資料庫中插入Null 物件
            } 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();
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.