Home | Previous Page | Next Page   Working With Informix Types >

SERIAL and SERIAL8 Data Types

IBM Informix JDBC Driver provides support for the Informix SERIAL and SERIAL8 data types through the methods getSerial() and getSerial8(), which are part of the implementation of the java.sql.Statement interface.

Because the SERIAL and SERIAL8 data types do not have an obvious mapping to any JDBC API data types from the java.sql.Types class, you must import Informix-specific classes into your Java program to handle SERIAL and SERIAL8 columns. To do this, add the following import line to your Java program:

import com.informix.jdbc.*;

Use the getSerial() and getSerial8() methods after an INSERT statement to return the serial value that was automatically inserted into the SERIAL or SERIAL8 column of a table, respectively. The methods return 0 if any of the following conditions are true:

If you execute the getSerial() or getSerial8() method after a CREATE TABLE statement, the method returns 1 by default (assuming the new table includes a SERIAL or SERIAL8 column). If the table does not contain a SERIAL or SERIAL8 column, the method returns 0. If you assign a new serial starting number, the method returns that number.

If you want to use the getSerial() and getSerial8() methods, you must cast the Statement or PreparedStatement object to IfmxStatement, the Informix-specific implementation of the Statement interface. The following example shows how to perform the cast:

cmd = "insert into serialTable(i) values (100)";
stmt.executeUpdate(cmd);
System.out.println(cmd+"...okay");
int serialValue = ((IfmxStatement)stmt).getSerial();
System.out.println("serial value: " + serialValue);

If you want to insert consecutive serial values into a column of data type SERIAL or SERIAL8, specify a value of 0 for the SERIAL or SERIAL8 column in the INSERT statement. When the column is set to 0, the database server assigns the next-highest value.

For more detailed information about the Informix SERIAL and SERIAL8 data types, refer to IBM Informix: Guide to SQL Reference and IBM Informix: Guide to SQL Syntax. You can find both of these guides online at http://www.ibm.com/software/data/informix/pubs/library/.

Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]