JDBC Type 2 Driver Programmer's Reference for SQL/MX Release 3.2.1 (H06.26+, J06.15+)

Sample Program Accessing BLOB Data
This sample program shows the use of both the Blob interface and the PreparedStatement
interface to take a byte variable and put the variable's value into a base table that has a BLOB
column.
// LOB operations may be performed through the Blob, or
// PreparedStatement interface. This program shows examples of
// using both interfaces taking a byte[] variable and putting
// it into the cat.sch.blobtiff table.
//
// The LOB base table for this example is created as:
// >> create table blobtiff // (col1 int not null not droppable,
// tiff blob, primary key (col1));
//
// The LOB table for this example is created through the
// JdbcMxLobAdmin utility as:
// >> create table cat.sch.blobdatatbl
// (table_name char(128) not null not droppable,
// data_locator largeint not null not droppable,
// chunk_no int not null not droppable,
// lob_data varchar(3880),
// primary key(table_name, data_locator, chunk_no))
// attributes extent(1024), maxextents 768 ;
//// ***** The following is the blob interface...
// - insert the base row with EMPTY_BLOB() as value for
// the LOB column// - select the lob column 'for update'
// - load up a byte[] with the data
// - use Outputstream.write(byte[])
//
// ***** The following is the prep stmt interface...
// - need an Inputstream object that already has data
// - need a PreparedStatement object that contains the
// 'insert...' DML of the base table
// - ps.setAsciiStream() for the lob data
// - ps.executeupdate(); for the DML
//
// To run this example, issue the following:
// # java TestBLOB 1 TestBLOB.class 1000
//
import java.sql.*;
import java.io.*;
public class TestBLOB
{
public static void main (String[] args)
throws java.io.FileNotFoundException, java.io.IOException
{
int numBytes;
int recKey;
long start;
long end;
Connection conn1 = null;
// Set jdbcmx.blobTableName System Property. This property
// can also be added to the command line through
// "-Djdbcmx.blobTableName=...", or a
// java.util.Properties object can be used and passed to
// getConnection.
System.setProperty( "jdbcmx.blobTableName","cat.sch.blobdatatbl" );
if (args.length < 2) {
System.out.println("arg[0]=; arg[1]=file; arg[2]=");
return;
}
// byte array for the blob
byte[] whatever = new byte[5000];
for (int i=0; i<5000; i++) whatever[i] = 71; // "G"
String k = "K";
for (int i=0; i<5000; i++) k = k + "K";
System.out.println("string length = " + k.length());
java.io.ByteArrayInputStream iXstream = new java.io.ByteArrayInputStream(whatever);
numBytes = iXstream.available();
if (args.length == 3) numBytes = Integer.parseInt(args[2]);
recKey = Integer.parseInt(args[0]);
System.out.println("Key: " + recKey +"; Using " + numBytes + " of file " + args[1]);
try {
Class.forName("com.tandem.sqlmx.SQLMXDriver");
start = System.currentTimeMillis();
conn1 = DriverManager.getConnection("jdbc:sqlmx:");
System.out.println("Cleaning up test tables...");
Statement stmt0 = conn1.createStatement();
stmt0.execute("delete from blobdatatbl");
98 Sample Programs Accessing CLOB and BLOB Data