JDBC Type 2 Driver 3.0 Programmer's Reference (SQL/MX 3.x)

Writing Binary Data to a BLOB Column
You can write data to the BLOB column by using Blob interfaces. The following code illustrates using the
setBinaryStream method of the Blob interface to write BLOB data.
Blob myBlob = null
// Stream begins at position 1
long pos = 1;
// Example string containing binary data
String s = "BINARY_DATA";
for (int i=0; i<5000; i++) s = s + "DATA";
// Obtain the output stream to write Blob data
OutputStream os = myBlob.setBinaryStream(pos);
// write Blob data using OutputStream
byte[] myBlobData = s.getBytes();
os.write(myBlobData);
The JDBC/MX driver splits the output stream into chunks and stores the chunks in the LOB table.
Inserting a BLOB Column by Using the PreparedStatement Interface
You can also insert a BLOB column that has binary data from a FileInputSteam. You must use PreparedStatement
interface to insert the BLOB column.
FileInputStream inputBinary = new FileInputStream(myBlobTestFile);
int blobLen = inputBinary.available();
PreparedStatement ps = conn.prepareStatement("insert
into myTable (myBlobColumn) values (?)");
ps.setBinaryStream(1, inputBinary, blobLen);
ps.executeUpdate();
The JDBC/MX driver reads the data from FileInputSteam and writes the data to the LOB table. The JDBC/MX
driver substitutes the next-available data locator for the parameter of the BLOB column in the table.
Inserting a Blob Object by Using the setBlob Method
Your JDBC application cannot directly instantiate a Blob object. To perform an equivalent operation:
1. Obtain a Blob object by using the getClob method of the ResultSet interface.
2. Insert the Blob object into another row by using the setBlob method of the PreparedStatement interface.
In this situation, the JDBC/MX driver generates a new data locator and copies the contents of the source Blob into the
new Blob object when the application issues the setBlob method of the PreparedStatement interface.
Reading Binary Data from a BLOB Column
You can read binary data from the BLOB column by using the Blob interface or InputStream. The following code
illustrates how to read the binary data from the BLOB column by using the Blob interface:
// Obtain the Blob from ResultSet
Blob myBlob = rs.getBlob("myBlobColumn");
// Obtain the input stream to read Blob data
InputStream is = myBlob.getBinaryStream();
// read Blob data using the InputStream
byte[] myBlobData = new byte[length];
is.read(myBlobData, offset, length);
To read binary data from the BLOB column by using InputStream