package com.storm.app;import java.io.*;
import java.sql.*;public class BlobTest {
    private static final String driver = "oracle.jdbc.driver.OracleDriver";
    private static final String url = "jdbc:oracle:thin:@localhost:1521:oradb";
    private static final String insert =
            "insert into bt values (?, empty_blob())";
    private static final String update =
            "select bf from bt where pkid=? for update";
    private static final String query =
            "select bf from bt where pkid=?";
    private Connection conn;
    private byte[] buf = new byte[128];    public static void main(String[] args) {
        BlobTest bt = new BlobTest();
        bt.write();
        bt.read();
    }    private void write() {
        try {
            conn = this.getConnection();
            conn.setAutoCommit(false);
            PreparedStatement stmt = conn.prepareStatement(insert);
            stmt.setInt(1, 1);
            stmt.executeUpdate();
            
            stmt = conn.prepareStatement(update);
            stmt.setInt(1, 1);
            ResultSet rs = stmt.executeQuery();            if (rs.next()) {
                Blob blob = rs.getBlob("bf");
                OutputStream os = ((oracle.sql.BLOB) blob).
                                  getBinaryOutputStream();                byte[] b = new byte[128];
                for (int i=0; i < b.length; i++)
                    b[i] = (byte)i;
                ByteArrayInputStream is = new ByteArrayInputStream(b);                int len = 0;
                while ((len = is.read(buf)) != -1)
                    os.write(buf, 0, len);
                os.flush();
                os.close();
            }
            rs.close();
            stmt.close();
            conn.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException sex) {
                sex.printStackTrace();
            }
        }
    }    private void read() {
        try {
            conn = this.getConnection();
            PreparedStatement stmt = conn.prepareStatement(query);
            stmt.setInt(1, 1);
            ResultSet rs = stmt.executeQuery();            if (rs.next()) {
                Blob blob = rs.getBlob("bf");
                InputStream is = blob.getBinaryStream();
                ByteArrayOutputStream os = new ByteArrayOutputStream();                int len = 0;
                while ((len = is.read(buf)) != -1)
                    os.write(buf, 0, len);
                byte[] b = os.toByteArray();
                os.flush();
                os.close();                for (int i = 0; i < b.length; i++)
                    System.out.println(b[i]);
            }
            rs.close();
            stmt.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException sex) {
                sex.printStackTrace();
            }
        }
    }    private Connection getConnection() throws SQLException {        try {
            Class.forName(driver);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
            System.exit( -1);
        }        return DriverManager.getConnection(url, "wd", "password");
    }
}