Inserting BLOB Values with setBlob() Method

This section describes how to insert BLOB values with the PreparedStatement.setBlob() method.

If you want to insert a BLOB column with a character string that comes from a java.sql.Blob object, you can directly set the value with PreparedStatement.setBlob() method.

To test this, I wrote the following program to copy some records with BLOB values as new records back into the same table. During the copy process, the BLOB values are also modified with some Blob object methods - The first 6 bytes are replaced with 0 values.

 * Copyright (c) All Rights Reserved.
import java.sql.*;
public class SqlServerBlobSetBlob {
  public static void main(String [] args) {
    Connection con = null;
    try { ds
        = new;
//      ds.setPortNumber(60782);
      con = ds.getConnection();

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

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

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

// Modifying the Blob object
        byte[] chuck = {(byte)0x00, (byte)0x00, (byte)0x00,
          (byte)0x00, (byte)0x00, (byte)0x00};

// Inserting a new record with setBlob()
        ps.setString(1, "Copy of "+subject);

// Checking the new records
      res = sta.executeQuery(
        "SELECT * FROM Image WHERE Subject LIKE 'Copy of %'");
      while ( {
        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+"...");

    } catch (Exception e) {
  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 performed exactly as I expected:

herong> java -cp .;mssql-jdbc-9.4.1.jre16.jar SqlServerBlobSetBlob

Copying record ID: 1
Copying record ID: 2
Copying record ID: 4
Record ID: 5
   Subject = Copy of Test on INSERT statement
   Body in HEX = 000000C8CABCCCCEB9C9CBBB...
Record ID: 6
   Subject = Copy of Test of the setBytes() method
   Body in HEX = 000000C8CABCCCCEB9C9CBBB...
Record ID: 7
   Subject = Copy of Test of setBinaryStream() methods
   Body in HEX = 0000000320B5A03B0487049A02048804AA0204BA...

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 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

 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

 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

 Archived Tutorials


 Full Version in PDF/EPUB