Using ByteBuffers with the IMS Universal JDBC Driver
The IMS Universal JDBC driver allows the use of INSERT, SELECT, and UPDATE options for full segments using java.nio.ByteBuffer as an I/O area .
You can use ByteBuffers as a qualification within the WHERE clause and also use them as an I/O area to INSERT, SELECT, and UPDATE full segment I/O areas from an SQL Query. This feature can be accessed by using the new reserved field in your SQL query: DFSIOSEG_{YOUR_SEGMENT_NAME. You do not have to define this field in your DBD source and it need not exist in the IMS Catalog. IMS Universal JDBC driver will automatically process this field when it is detected in the SQL Query.
Inserting data using ByteBuffers
You can insert a full segment I/O area using IMS Universal JDBC driver with a PreparedStatement. The following code sample illustrates inserting a full segment I/O area using ByteBuffers:
String preparedInsertStatement = "INSERT INTO WARD (DFSIOSEG_WARD, HOSPITAL_HOSPCODE) values (?, 'R1210010000A')";
PreparedStatement pst = conn.prepareStatement(preparedInsertStatement);
//Prepare buffer for inserting a ward patient
ByteBuffer bufferInsert = ByteBuffer.allocate(900);
bufferInsert.position(0);
bufferInsert.putShort((short) 900);
bufferInsert.put("0010".getBytes("Cp1047"));
bufferInsert.put("COOL WARD".getBytes("Cp1047"));
// Set prepared value for the buffer and execute the PreparedStatement
pst.setObject(1, bufferInsert);
pst.execute();
Selecting data using ByteBuffers
You can retrieve full segment I/O areas by specifying the hidden DFSIOSEG field
in your SQL query. After obtaining a ResultSet from your query, you can obtain your
ByteBuffer object simply by getting an Object from your ResultSet using the
new DFSIOSEG field as a parameter and casting it to a ByteBuffer object. If
required, you can add additional fields in conjunction with the DFSIOSEG field. The
following code sample illustrates how to retrieve a full segment I/O area using ByteBuffers:
String selectQuery = "SELECT DFSIOSEG_WARD, WARDNAME FROM WARD WHERE HOSPITAL_HOSPCODE = 'R1210010000A' AND WARDNO = '0010'";
ResultSet rs = st.executeQuery(selectQuery);
while(rs.next())
{
String wardName = rs.getString("WARDNAME");
ByteBuffer buffer = (ByteBuffer)rs.getObject("DFSIOSEG_WARD");
}
Updating data using ByteBuffers
Updating a full segment I/O area using the IMS Universal JDBC driver can also be achieved using a PreparedStatement. The following code sample illustrates how to update a full segment I/O area using ByteBuffers :
String preparedUpdateStatement = "UPDATE WARD SET DFSIOSEG_WARD = ?
WHERE HOSPITAL_HOSPCODE = 'R1210010000A' AND WARDNO = '0010'";
PreparedStatement pst = conn.prepareStatement(preparedInsertStatement);
// Prepare buffer for inserting a ward patient
ByteBuffer bufferUpdate = ByteBuffer.allocate(900);
bufferUpdate.position(0);
bufferUpdate.putShort((short) 900);
bufferUpdate.put("0010".getBytes("Cp1047"));
bufferUpdate.put("COOL WARD".getBytes("Cp1047"));
// Set prepared value for the buffer and execute the PreparedStatement
pst.setObject(1, bufferUpdate);
pst.execute();
Considerations for Byte Buffers and Type-2 Java applications
If your JDBC application is running in Type-2 mode, you have the option of obtaining a direct ByteBuffer to optimize the JVM storage use. You can obtain direct storage in the following ways:
- Directly using the java.nio.ByteBuffer class
ByteBuffer directBuffer = ByteBuffer.allocateDirect(10); - Using the Universal Drivers Application object to obtain direct 31-bit
storage
Application app = new ApplicationFactory.createApplication(); ByteBuffer directBuffer = app.get31BitDirectByteBuffer(20);
ByteBuffer.allocateDirect()
will not work with your application and you must directly request 31-bit storage using the
Application.get31BitDirectByteBuffer() method.
ByteBuffer.allocateDirect() works in a 31-bit JVM.It is important to release and free the buffers correctly in your application. You can free buffers that were obtained using the Application class in the following ways:
- Specifically freeing buffers
Application app = new ApplicationFactory.createApplication(); ByteBuffer directBuffer = app.get31BitDirectByteBuffer(20); // Doing work app.free31BitDirectByteBuffer(directBuffer); - IMS silently tracks all buffers allocated using the Application class and
automatically releases all the checked out buffers during the
end() procedure.Application app = new ApplicationFactory.createApplication(); ByteBuffer directBuffer = app.get31BitDirectByteBuffer(20); // Doing work app.end();