![]() |
|
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 multibyte characters.
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 http://www.informix.com/answers.
Whenever a BLOB, CLOB, text, or byte object is fetched from the database server, the data is cached into memory. If the size of the large object is bigger than the value in the LOBCACHE environment variable, the large object data is stored in a temporary file. For more information about the LOBCACHE variable, see Memory Management of Large Objects.
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().
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 in the current row must be read before you call the next() method to retrieve the next row.
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 device:
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 device. 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().