JDBC Type 4 Driver Programmer's Reference for SQL/MX Release 3.1 (H06.23+, J06.12+)
Use the EMPTY_BLOB() function in the insert statement to create an empty BLOB column in the database.
Use setBinaryStream method of the Blob interface to obtain the InputStream to read BLOB data.
Use getBinaryStream method of the Blob interface to obtain the OutputStream to write BLOB data.
Use setBinaryStream of the PreparedStatement interface to write the data to the BLOB column.
Inserting a BLOB Column by Using the Blob Interface
When you insert a row containing a BLOB data type, you can insert the row using an empty BLOB value before the column can be updated with
real
BLOB data. To insert an empty BLOB value in an SQL/MX database, specify EMPTY_BLOB() function for the BLOB column in the insert
statement.
The Type 4 driver scans the SQL string for the EMPTY_BLOB() function and substitutes the next-available data locator.
Note:
The EMPTY_BLOB() function is an HP NonStop specific function and might not work on other databases.
Do not use the EMPTY_BLOB() function when using the PreparedStatement interface.
Then, obtain the handle to the empty BLOB column by selecting the BLOB column for update. The following code illustrates how to obtain the
handle to an empty
BLOB column:
Blob myBlob = null;
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("Select myBlobColumn
from myTable where …For update");
if (rs.next())
myBlob = rs.getBlob(1);
You can now write data to the BLOB column. See Writing Binary Data to a BLOB Column.
Note: Limitation: Do not rename the BLOB column in the select query.
Writing Binary Data to a BLOB Column
To write data to the BLOB column, use the Blob interface. The following code illustrates using the setBinaryStream method of the Blob interface
to write
BLOB data:
// Stream begins at position: 1
long pos = 1;
// String containing the binary data
String s ;
// 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 Type 4 driver splits the output stream into chunks and stores the chunks in the LOB table.
Inserting a BLOB Column by Using the PreparedStatement Interface
To insert a BLOB column that has binary data from an InputStream, use the PreparedStatement interface:
InputStream inputBinary;
PreparedStatement ps = conn.prepareStatement("insert
into myTable (myBlobColumn) values (?)");
ps.setBinaryStream(1, inputBinary, length_of_data);
ps.executeUpdate();
The Type 4 driver reads the data from InputStream and writes the data to the LOB table. The Type 4 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 getBlob 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 Type 4 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










