INFORMIX
Informix JDBC Driver Programmer's Guide
Chapter 2: Programming with Informix JDBC Driver
Contents Index

Querying the Database

Informix JDBC Driver complies with the JDBC API specification for sending queries to a database and retrieving the results. The driver supports almost all the methods of the Statement, PreparedStatement, CallableStatement, ResultSet, and ResultSetMetaData interfaces.

Refer to "Restrictions and Limitations" for a list of methods that are not supported by Informix JDBC Driver and should not be used in your Java program. This reference also includes a list of methods that behave differently than described in the JDBC API specification.

Manipulating Informix BYTE and TEXT Data Types

The section describes the Informix BYTE and TEXT data types and how to manipulate columns of these data types with the JDBC API.

The BYTE data type is a data type for a simple large object that stores any kind of data in an undifferentiated byte stream. Examples of binary data include spreadsheets, digitized voice patterns, and video clips. The TEXT data type is a data type for a simple large object that stores any kind of text data. It can contain both single and multi-byte characters.

Both data types have no maximum size, and columns of either data type have a theoretical limit of 231 bytes and a practical limit determined by your disk capacity.

For more detailed information about the Informix BYTE and TEXT data types, refer to Informix Guide to SQL: Reference and Informix Guide to SQL: Syntax. You can find the on-line version of both of these guides at the following Web site:

Inserting into or Updating BYTE and TEXT Columns

To insert into or update BYTE and TEXT columns, read a stream of data from a source, such as an operating system file, and transmit it to the database as a java.io.InputStream object. The PreparedStatement interface provides methods for setting an input parameter to this Java input stream. When the statement is executed, Informix JDBC Driver makes repeated calls to the input stream, reading its contents and transmitting those contents as the actual parameter data to the database.

For BYTE data types, use the PreparedStatement.setBinaryStream() method to set the input parameter to the InputStream object. For TEXT data types, use the PreparedStatement.setAsciiStream() method.

The following example from the ByteType.java program shows how to insert the contents of the operating system file data.dat into a column of data type BYTE:

The example first creates a java.io.File object that represents the operating system file data.dat. The example then creates a FileInputStream object to read from the File object. The FileInputStream object is cast to its superclass InputStream, which is the expected data type of the second parameter to the PreparedStatement.setBinaryStream() method. The setBinaryStream() method is executed on the already prepared INSERT statement, which sets the input stream parameter. Finally, the PreparedStatement.executeUpdate() method is executed, which actually inserts the contents of the data.dat operating system file into the BYTE column of the table.

The TextType.java program shows how to insert data into a TEXT column. It is very similar to inserting into a BYTE column, except the method setAsciiStream() is used to set the input parameter instead of setBinaryStream().

Selecting from BYTE and TEXT Columns

After you select from a table into a ResultSet object, you can use the ResultSet.getBinaryStream() and ResultSet.getAsciiStream() methods to retrieve a stream of binary or ASCII data from BYTE and TEXT columns, respectively. Both methods return an InputStream object, which can be used to read the data in chunks.

All the data in the returned stream must be read before you get the value of any other column in the table. The next call to a getXXX() method implicitly closes the stream.

The following example from the ByteType.java program shows to how select data from a BYTE column and print out the data to the standard output:

The example first puts the result of a SELECT statement into a ResultSet object. It then executes the method ResultSet.getBinaryStream() to retrieve the BYTE data into a Java InputStream object.

The method dispValue(), whose Java code is also included in the example, is used to actually print out the contents of the column to standard output. The dispValue() method uses byte arrays and the InputStream.read() method to systematically read the contents of the BYTE column.

The TextType.java program shows how to select data from a TEXT column. It is very similar to selecting from a BYTE column, except the getAsciiStream() method is used instead of getBinaryStream().

Manipulating Informix INTERVAL Data Types

The Informix INTERVAL data type stores a value that represents a span of time. INTERVAL data types are divided into two types: year-month intervals and day-time intervals. A year-month interval can represent a span of years and months, and a day-time interval can represent a span of days, hours, minutes, seconds, and fractions of a second.

The following example from the Interval.java program shows how to insert into and select from the two types of INTERVAL data types:

Informix-Specific Information About Querying a Database

This section describes the Informix-specific information you need to know to use Informix JDBC Driver to query an Informix database and process the results.

The Informix JDBC Driver implementation of the Statement.execute() method returns a single ResultSet object. This differs from the JDBC API specification, which states that the method can return multiple ResultSet objects.

Be sure to always explicitly close a Statement, PreparedStatement, and CallableStatement object by calling the appropriate close() method in your Java program when you have finished processing the results of an SQL statement. This immediately deallocates the resources that have been allocated to execute your SQL statement. Although the ResultSet.close() method closes the ResultSet object, it does not deallocate the resources allocated to the Statement, PreparedStatement, or CallableStatement objects.

Example of Sending a Query to an Informix Database

The following example from the SimpleSelect.java program shows how to use the PreparedStatement interface to execute a SELECT statement that has one input parameter:

The program first uses the Connection.prepareStatement() method to prepare the SELECT statement with its single input parameter. It then assigns a value to the parameter using the PreparedStatement.setObject() method and executes the query with the PreparedStatement.executeQuery() method.

The program returns resulting rows in a ResultSet object, through which the program iterates with the ResultSet.next() method. The program retrieves individual column values with the ResultSet.getShort() method, since the data type of the selected column is SMALLINT.

Finally, both the ResultSet and PreparedStatement objects are explicitly closed with the appropriate close() method.

For more information on which getXXX() methods should be used to retrieve individual column values, refer to "Mapping Data Types".




Informix JDBC Driver Programmer's Guide, Version 1.22
Copyright © 1998, Informix Software, Inc. All rights reserved.