Copying BLOB Values to New Rows

This section describes how to copy BLOB values to new rows with getBlob() and setBlob() methods.

The previous test shows that we can not update a Blob object returned from the getBlob() method. But we can use it without any changes with the PreparedStatement.setBlob() method to copy it to a new row. Here is a test program:

/* OracleBlobSetBlobRevised.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.io.*;
import java.sql.*;
public class OracleBlobSetBlobRevised {
  public static void main(String [] args) {
    Connection con = null;
    try {
      oracle.jdbc.pool.OracleDataSource ds
        = new oracle.jdbc.pool.OracleDataSource();
      ds.setDriverType("thin");
      ds.setServerName("localhost");
      ds.setPortNumber(1521);
      ds.setDatabaseName("XE");
      ds.setUser("Herong");
      ds.setPassword("TopSecret");
      con = ds.getConnection();

// Deleting records for re-testing
      Statement sta = con.createStatement();
      sta.executeUpdate(
        "DELETE FROM Image WHERE Subject LIKE 'Copy of %'");

// Creating a PreparedStatement for inserting new records
      PreparedStatement ps = con.prepareStatement(
        "INSERT INTO Image (Subject, Body, ID) VALUES (?,?,?)");

// Looping though the first 3 records
      ResultSet res = sta.executeQuery(
        "SELECT * FROM Image ORDER BY ID");
      int i = 0;
      while (res.next() && i<3) {
        i++;
        System.out.println("Copying record ID: "+res.getInt("ID"));
        String subject = res.getString("Subject");
        Blob body = res.getBlob("Body");

// Oracle does not allow to modify the Blob object
//        byte[] chuck = {(byte)0x00, (byte)0x00, (byte)0x00,
//          (byte)0x00, (byte)0x00, (byte)0x00};
//        body.setBytes(1,chuck);

// Inserting a new record with setBlob()
        ps.setString(1, "Copy of "+subject);
        ps.setBlob(2,body);
        ps.setInt(3,res.getInt("ID")+100);
        ps.executeUpdate();
      }
      ps.close();
      res.close();

// Checking the new records
      res = sta.executeQuery(
        "SELECT * FROM Image WHERE Subject LIKE 'Copy of %'");
      while (res.next()) {
        System.out.println("Record ID: "+res.getInt("ID"));
        System.out.println("   Subject = "+res.getString("Subject"));
        byte[] body = res.getBytes("Body");
        String bodyHex = bytesToHex(body, 32);
        System.out.println("   Body in HEX = "+bodyHex+"...");
      }
      res.close();

      sta.close();
      con.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public static String bytesToHex(byte[] bytes, int max) {
    StringBuffer buffer = new StringBuffer();
    for (int i=0; i<bytes.length && i<max; i++) {
      buffer.append(Integer.toHexString(bytes[i] & 0xFF));
    }
    return buffer.toString().toUpperCase();
  }
}

The program worked correctly:

herong> java -cp .;ojdbc11.jar OracleBlobSetBlobRevised

Copying record ID: 1
Copying record ID: 2
Copying record ID: 3
Record ID: 101
   Subject = Copy of Test on INSERT statement
   Body in HEX = C9CBBBCCCEB9C8CABCCCCEB9C9CBBB...
Record ID: 102
   Subject = Copy of Test of the setBytes() method
   Body in HEX = C9CBBBCCCEB9C8CABCCCCEB9C9CBBB...
Record ID: 103
   Subject = Copy of Test of setBinaryStream() methods
   Body in HEX = CAFEBABE000320BAA03D04A704BA0204A804CA0204D8...

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 Installing and Running Java DB - Derby

 Derby (Java DB) JDBC Driver

 Derby (Java DB) JDBC DataSource Objects

 Java DB (Derby) - DML Statements

 Java DB (Derby) - ResultSet Objects of Queries

 Java DB (Derby) - PreparedStatement

 MySQL Installation on Windows

 MySQL JDBC Driver (MySQL Connector/J)

 MySQL - PreparedStatement

 MySQL - Reference Implementation of JdbcRowSet

 MySQL - JBDC CallableStatement

 MySQL CLOB (Character Large Object) - TEXT

 MySQL BLOB (Binary Large Object) - BLOB

 Oracle Express Edition Installation on Windows

 Oracle JDBC Drivers

 Oracle - Reference Implementation of JdbcRowSet

 Oracle - PreparedStatement

 Oracle - JBDC CallableStatement

 Oracle CLOB (Character Large Object) - TEXT

Oracle BLOB (Binary Large Object) - BLOB

 Overview of BLOB (Binary Large Object)

 Create Tables with CLOB Columns

 Inserting BLOB Values with SQL INSERT Statements

 Inserting BLOB Values with setBytes() Method

 Inserting BLOB Values with setBinaryStream() Method

 Closing InputStream Too Early on setBinaryStream()

 Retrieving BLOB Values with getBytes() Method

 Retrieving BLOB Values with getBinaryStream() Method

 Retrieving BLOB Values with getBlob() Method

 Inserting BLOB Values with setBlob() Method

Copying BLOB Values to New Rows

 Microsoft SQL Server Express Edition

 Microsoft JDBC Driver for SQL Server

 Microsoft JDBC Driver - Query Statements and Result Sets

 Microsoft JDBC Driver - DatabaseMetaData Object

 Microsoft JDBC Driver - DDL Statements

 Microsoft JDBC Driver - DML Statements

 SQL Server - PreparedStatement

 SQL Server CLOB (Character Large Object) - TEXT

 SQL Server BLOB (Binary Large Object) - BLOB

 JDBC-ODBC Bridge Driver - sun.jdbc.odbc.JdbcOdbcDriver

 JDBC-ODBC Bridge Driver - Flat Text Files

 JDBC-ODBC Bridge Driver - MS Access

 JDBC-ODBC Bridge Driver - MS SQL Server

 Summary of JDBC Drivers and Database Servers

 Using Connection Pool with JDBC

 Archived Tutorials

 References

 Full Version in PDF/EPUB