JDBC for SQL Server - Herong's Tutorial Examples - v3.14, by Herong Yang
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.
/* SqlServerBlobSetBlob.java * Copyright (c) HerongYang.com. All Rights Reserved. */ import java.io.*; import java.sql.*; public class SqlServerBlobSetBlob { public static void main(String [] args) { Connection con = null; try { com.microsoft.sqlserver.jdbc.SQLServerDataSource ds = new com.microsoft.sqlserver.jdbc.SQLServerDataSource(); ds.setServerName("localhost"); // ds.setPortNumber(60782); ds.setInstanceName("SQLEXPRESS"); ds.setDatabaseName("AdventureWorks2019"); ds.setUser("Herong"); ds.setPassword("T0pSecret"); 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) 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"); // Modifying 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.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 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
JDBC (Java Database Connectivity) Introduction
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
Using Connection Pool with JDBC
JDBC-ODBC Bridge Driver - sun.jdbc.odbc.JdbcOdbcDriver
JDBC-ODBC Bridge Driver - Flat Text Files
JDBC-ODBC Bridge Driver - MS Access