NonStop Server for Java (NSJ) Programmer's Guide (NSJ 2.0+)
// create a PreparedStatement object for the connection
PreparedStatement psmt = con.PreparedStatement(insertString);
// set values to be inserted including a BLOB and insert a new row in the table.
psmt.setString(1, "Mickey Mouse");
psmt.setObject(2, MickyMousePic); //MickyMousePic is a serializable Java object.
psmt.executeUpdate( );
Reading a BLOB From the Database
A JDBC application can retrieve a BLOB object from the database using the getObject( ) method in the ResultSet
interface. A BLOB can also be retrieved as a byte array using the ResultSet interface. Using the SQLMP JDBC Driver, a
BLOB can be read from a database table using the following methods supported by SQLMPResultSet.
public Object getObject(int columnIndex)
public Object getObject(String columnName)
public byte[ ] getBytes(int columnIndex)
public byte[ ] getBytes(String columnName)
where:
columnName
is the name of the BLOB column.
columnIndex
is the index of the BLOB column.
Errors encountered during the retrieval of a BLOB from its BLOB file generate a SQLException with the following
message:
SQLMP: error message. See File System error ## for further details.
where:
error message
is the error encountered while performing this operation.
error ##
is the File System error returned while reading the BLOB from a BLOB file.
Note: Retrieving a BLOB from a database runs as a waited operation, so reading a BLOB within a thread blocks
that thread.
Example of Retrieving a BLOB From the Database
This example assumes that a java.util.Properties object with BLOB properties has been created as described in the
previous example and a connection granted using this Properties object. The sample table $VOL2.MYVOL.EMPLOYEE
contains two columns:
EMPNAME, which stores an employee's name
EMPPIC, which stores an employee's picture.
java.lang.String selectString = new String("SELECT * FROM $VOL2.MYVOL.EMPLOYEE");
// create a Statement object for the connection
stmt = conn.createStatement( );
// get a ResultSet with the contents of the table.
ResultSet results = stmt.executeQuery(selectString);
//retrieve content of first row
String name = results.getString(1);
Object blob = results.getObject(2);
Note: The BLOB can also be retrieved as an array of bytes instead of as an object using the following: