informix
Informix JDBC Driver Programmer's Guide
Programming with Informix JDBC Driver

Establishing a Connection

You must first establish a connection to an Informix database server or database before you can start sending queries and receiving results in your Java program.

You establish a connection by completing two actions:

  1. Load Informix JDBC Driver.
  2. Create a connection to either a database server or a specific database.

You have the following connection options:

Loading Informix JDBC Driver

To load Informix JDBC Driver, use the Class.forName() method, passing it the value com.informix.jdbc.IfxDriver, as shown in the following code example from the CreateDB.java program:

The Class.forName() method loads the Informix implementation of the Driver class, IfxDriver. The IfxDriver class then creates an instance of the driver and registers it with the DriverManager class.

Once you have loaded Informix JDBC Driver, you are ready to connect to an Informix database or database server.

If you are writing an applet to be viewed with Microsoft Internet Explorer, you might need to explicitly register Informix JDBC Driver to avoid platform incompatibilities.

To explicitly register the driver, use the DriverManager.registerDriver() method, as shown:

This method might register Informix JDBC Driver twice, which does not cause a problem.

Creating a Connection

To create a connection to an Informix database or database server, use the DriverManager.getConnection() method. This method creates a Connection object, which is later used to create SQL statements, send them to an Informix database, and process the results.

The DriverManager class keeps track of the available drivers and handles connection requests between appropriate drivers and databases or database servers. The url parameter of the getConnection() method is a database URL that specifies the subprotocol (the database connectivity mechanism), the database or database server identifier, and a list of properties. A second parameter to the getConnection() method, property, is the property list. See Specifying Environment Variables with the Properties Class for an example of how to specify a property list.

The following example shows a database URL that connects to a database called testDB:

The details of the database URL syntax are described in the next section.

The following code example from the CreateDB.java program shows how to connect to database testDB using Informix JDBC Driver. In the full example, the url variable, described in the preceding example, is passed in as a parameter when the program is run at the command line.

Important: The only connection type supported by Informix JDBC Driver is tcp. Shared memory and other connections types are not supported. The connection type is specified in the NETTYPE parameter of the sqlhosts or ONCONFIG file. For more information, see the Administrator's Guide for your database server.

Important: Not all methods of the Connection interface are supported by Informix JDBC Driver. For a list of unsupported methods, see Unsupported Methods.

Tip: You do not have to explicitly close a database connection. The database server closes the connection automatically.

Format of Database URLs

Informix JDBC Driver supports database URLs of the following format:

In the preceding syntax:

The following table describes the variable parts of the database URL.

Database URL Variable Required? Description
ip-address host-name Yes, unless SQLH_TYPE is defined The IP address or the host name of the computer running the Informix database server. An example of an IP address is 123.45.67.89. An example of a host name is myhost.com or myhost.informix.com. If an LDAP server or sqlhosts file provides the IP address or host name through the SQLH_TYPE property, you do not have to specify them in the database URL. For more information, see Dynamically Reading the Informix sqlhosts File.
port-number Yes, unless SQLH_TYPE is defined The port number of the Informix database server. If an LDAP server or sqlhosts file provides the port number through the SQLH_TYPE property, you do not have to specify it in the database URL. For more information, see Dynamically Reading the Informix sqlhosts File.
dbname No The name of the Informix database to which you want to connect. If you do not specify the name of a database, a connection is made to the Informix database server.
server-name Yes The name of the Informix database server to which you want to connect. This is the value of the INFORMIXSERVER environment variable. The INFORMIXSERVER environment variable is required in the database URL, unless it is included in the property list.
user password Yes The name of the user that wants to connect to the Informix database or database server, and the password of that user. You must specify both the user and the password.
name=value No A name-value pair that specifies a value for the Informix environment variable contained in the name variable, recognized by either Informix JDBC Driver or Informix database servers. The name variable is case insensitive. Informix JDBC Driver reads Informix environment variables from either the database URL or from a connection property list, described in Specifying Environment Variables with the Properties Class. The user's environment is not consulted. Refer to Supported Informix Environment Variables for a list of Informix environment variables supported by Informix JDBC Driver.

Database Versus Database Server Connections

Using the DriveManager.getConnection() method, you can create a connection to either an Informix database or an Informix database server.

To create a connection to an Informix database, specify the name of the database in the dbname variable of the database URL. If you omit the name of a database, a connection is made to the database server specified by the INFORMIXSERVER environment variable of the database URL or the connection property list.

If you connect directly to an Informix database server, you can execute an SQL statement that connects to a database later in your Java program.

All connections to both databases and database servers must include the name of an Informix database server via the INFORMIXSERVER environment variable.

Important: If you are connecting to a 5.x database server (either INFORMIX-OnLine Dynamic Server or INFORMIX-SE), you must specify the USEV5SERVER environment variable in the database URL or property list. Its value must be 1, such as USEV5SERVER=1.

The example given in Creating a Connection shows how to create a connection directly to the Informix database called testDB with the database URL.

The following example from the DBConnection.java program shows how to first create a connection to the Informix database server called myserver and then connect to the database testDB later in the Java program using the Statement.executeUpdate() method.

The following database URL is passed in as a parameter to the program when the program is run at the command line; note that the URL does not include the name of a database:

Here is the example code:

Specifying Environment Variables with the Properties Class

Informix JDBC Driver reads Informix environment variables only from the name-value pairs in the connection database URL or from a connection property list. The driver does not consult the user's environment for any environment variables. Refer to Supported Informix Environment Variables for a list of supported Informix environment variables.

To specify Informix environment variables in the name-value pairs of the connection database URL, refer to Format of Database URLs.

To specify Informix environment variables via a property list, use the java.util.Properties class to build the list of properties. The list of properties might include Informix environment variables, such as INFORMIXSERVER, as well as user and password. After you have built the property list, pass it to the DriverManager.getConnection() method as a second parameter. You still need to include a database URL as the first parameter, although in this case you do not need to include the list of properties in the URL.

The following code from the optofc.java example shows how to use the java.util.Properties class to set connection properties. It first uses the Properties.put() method to set the environment variable OPTOFC to 1 in the connection property list; then it connects to the database.

The DriverManager.getConnection() method in this example takes two parameters: the database URL and the property list. The example creates a connection similar to the example given in Creating a Connection.

The following database URL is passed in as a parameter to the example program when the program is run at the command line:

Here is the example program:

Supported Informix Environment Variables

The following table lists the Informix environment variables supported by Informix JDBC Driver.

Supported Informix Environment Variables Description
CLIENT_LOCALE Specifies the locale of the client that is accessing the database. Together with the DB_LOCALE variable, the database server uses this variable to establish the server processing locale. This variable is available on and optional for servers that support GLS.
DBANSIWARN Checks for Informix extensions to ANSI standard syntax.
DBCENTURY Enables you to specify the appropriate expansion for one- or two-digit year DATE and DATETIME values.
DBDATE Specifies the end-user formats of values in DATE columns.
DB_LOCALE Specifies the locale of the database. Together with the CLIENT_LOCALE variable, the database server uses this variable to establish the server processing locale. This variable is available on and optional for servers that support GLS.
DBSPACETEMP Specifies the dbspaces in which temporary tables are built.
DBUPSPACE Specifies the amount of system disk space that the UPDATE STATISTICS statement can use when it simultaneously constructs multiple-column distributions.
DELIMIDENT When set to Y, specifies that strings set off by double quotes are delimited identifiers.
ENABLE_CACHE_TYPE When set to 1, caches the data type information for opaque, distinct, or row data instead of asking the database server whenever a Struct or SQLData object inserts data into a column and getSQLTypeName() returns the type name information.
FET_BUF_SIZE Overrides the default setting for the size of the fetch buffer for all data except large objects. The default size is 4096 bytes.
GL_DATE Specifies the end-user formats of values in DATE columns. This variable is supported in Informix database server versions 7.2x and beyond.
IFX_AUTOFREE When set to 1, specifies that the Statement.close() method does not require a network round-trip to free the database server cursor resources if the cursor has already been closed in the database server. The database server automatically frees the cursor resources after the cursor is closed, either explicitly by the ResultSet.close() method or implicitly by the OPTOFC environment variable. After the cursor resources have been freed, the cursor can no longer be referenced. For more information, see The Auto Free Feature.
INFORMIXCONRETRY Specifies the maximum number of additional connection attempts that can be made to each database server by the client during the time limit specified by the default value of the INFORMIXCONTIME environment variable (15 seconds).
INFORMIXCONTIME Sets the timeout period for an attempt to connect to the database server. If a connection attempt does not succeed in this time, the attempt is aborted and a connection error is reported. The default value is 15 seconds.
INFORMIXOPCACHE Specifies the size of the memory cache for the staging-area blobspace of the client application.
INFORMIXSERVER Specifies the default database server to which an explicit or implicit connection is made by a client application.
INFORMIXSTACKSIZE Specifies the stack size, in kilobytes, that the database server uses for a particular client session.
JDBCTEMP Specifies where temporary files for handling smart large objects are created. You must supply an absolute pathname.
LOBCACHE Determines the buffer size for large object data that is fetched from the database server:
  • If LOBCACHE > 0, the maximum LOBCACHE number of bytes is allocated in memory to hold the data. If the data size exceeds the LOBCACHE value, the data is stored in a temporary file. If a security violation occurs during creation of this file, the data is stored in memory.
  • If LOBCACHE = 0, the data is always stored in a file. In this case, if a security violation occurs, Informix JDBC Driver makes no attempt to store the data in memory.
  • If LOBCACHE < 0, the data is always stored in memory. If the required amount of memory is not available, an error occurs. If the LOBCACHE value is not specified, the default is 4096 bytes.
  • NODEFDAC When set to YES, prevents default table and routine privileges from being granted to the PUBLIC user when a new table or routine is created in a database that is not ANSI compliant. Default is NO.
    OPTCOMPIND Specifies the join method that the query optimizer uses.
    OPTOFC When set to 1, the ResultSet.close() method does not require a network round-trip if all the qualifying rows have already been retrieved in the client's tuple buffer. The database server automatically closes the cursor after all the rows have been retrieved. Informix JDBC Driver might not have additional rows in the client's tuple buffer before the next ResultSet.next() method is called. Therefore, unless Informix JDBC Driver has received all the rows from the database server, the ResultSet.close() method might still require a network round-trip when OPTOFC is set to 1.
    PATH Specifies the directories that should be searched for executable programs.
    PDQPRIORITY Determines the degree of parallelism used by the database server.
    PLCONFIG Specifies the name of the configuration file used by the high-performance loader.
    PSORT_DBTEMP Specifies one or more directories to which the database server writes the temporary files it uses when performing a sort.
    PSORT_NPROCS Enables the database server to improve the performance of the parallel-process sorting package by allocating more threads for sorting.
    SECURITY=PASSWORD Protects the user-provided password using 56-bit encryption when it is passed from the client to the database server.
    SQLH_TYPE When set to FILE, specifies that database URL information (such as the host-name, port-number, user, and password) is specified in an sqlhosts file. When set to LDAP, specifies that this information is specified in an LDAP server. For more information, see Dynamically Reading the Informix sqlhosts File.
    USEV5SERVER When set to 1, specifies that the Java program is connecting to an INFORMIX-OnLine or INFORMIX-SE 5.x database server. This environment variable is mandatory if you are connecting to an INFORMIX-OnLine or INFORMIX-SE 5.x database server.

    For a detailed description of a particular environment variable, refer to Informix Guide to SQL: Reference. You can find the on-line version of this guide at http://www.informix.com/answers.

    Dynamically Reading the Informix sqlhosts File

    Informix JDBC Driver now supports the JNDI (Java naming and directory interface). This support enables JDBC programs to access the Informix sqlhosts file. The sqlhosts file lets a client application find and connect to an Informix database server anywhere on the network. For more information about this file, see the Administrator's Guide for your database server.

    You can access sqlhosts data from a local file or from an LDAP (lightweight directory access protocol) server. The system administrator must load the sqlhosts data into the LDAP server using an Informix-supplied utility.

    Your CLASSPATH variable must reference the JNDI JAR (Java archive) files and the LDAP SPI (service provider interface) JAR files. You must use LDAP Version 3.0 or later, which supports the object class extensibleobject.

    An unsigned applet cannot access the sqlhosts file or an LDAP server. For more information, see Using the Driver in an Applet.

    Database URL Syntax

    You can let Informix JDBC Driver look up the host name or port number in an LDAP server instead of specifying them in the database URL directly. You must specify the following properties in the database URL for the LDAP server:

    If LDAP_USER and LDAP_PASSWD are not specified, Informix JDBC Driver uses an anonymous search to search the LDAP server. The LDAP administrator must make sure that an anonymous search is allowed on the sqlhosts entry. For more information, see your LDAP server documentation.

    Here is an example database URL:

    You can also specify the sqlhosts file in the database URL. The host name and port number are read from the sqlhosts file. You must specify the following properties for the file:

    The sqlhosts file can be local or remote, so you can refer to it in the local file system format or URL format. Here are some examples:

    Here is an example database URL:

    If the database URL references the LDAP server or sqlhosts file but also directly specifies the IP address, host name, and port number, the IP address, host name, and port number specified in the database URL take precedence.

    Administration Requirements

    If you want the LDAP server to store sqlhosts information that a JDBC program can look up, the following requirements must be met:

    Utilities to Update the LDAP Server with sqlhosts Data

    The SqlhUpload and SqlhDelete utilities are packaged in ifxtools.jar, so the CLASSPATH variable must point to ifxtools.jar (which, by default, is in the lib directory under the installation directory for Informix JDBC Driver). Make sure that the CLASSPATH variable also points to the JNDI JAR files and LDAP SPI JAR files.

    Class SqlhUpload

    This utility loads the sqlhosts entries from a flat ASCII file to the LDAP server in the prescribed format. Enter the following command:

    The parameters of this command have the following meanings:

    The utility prompts for other required information, such as the Informix base DN (distinguished name) in the LDAP server, the LDAP user, and the password.

    You must convert the servicename field in the sqlhosts file to a string that represents an integer (the port number), because the Java.Socket class cannot accept an alphanumeric servicename for the port number. For more information about the servicename field, see the Administrator's Guide for your database server.

    Class SqlhDelete

    This utility deletes the sqlhosts entries from the LDAP server. Enter the following command:

    The parameters of this command have the same meanings as the parameters listed for SqlhUpload on page 2-19.

    The utility prompts for other required information, such as the Informix base DN in the LDAP server, the LDAP user, and the password.

    Password Encryption

    The SECURITY environment variable specifies the security operations that are performed when the Informix JDBC client and Informix database server exchange data. The only SECURITY option supported in Informix JDBC Driver is PASSWORD. By default, no SECURITY option is set. If the PASSWORD option is specified, the user-provided password is encrypted using 56-bit encryption when it is passed from the client to the database server.

    Both the SECURITY and PASSWORD keywords are case insensitive.

    Here is an example of the database URL syntax for the PASSWORD option:

    Configuring the Database Server

    The SECURITY=PASSWORD option is supported in the 7.31, 8.3, and 9.1x versions of the Informix database server. The connection is rejected if used with any other versions of the server.

    If the SECURITY=PASSWORD option is enabled in the Informix JDBC client, the Informix database server must also be configured to support the corresponding security options requested by the client. Otherwise, an error is returned during connection.

    To use the SPWDCSM csm option, which supports password encryption on the database server, you must configure the server's sqlhosts servername option. After this option is set on the server, only clients using the SECURITY=PASSWORD option can connect to that server name. To see if the SPWDCSM csm option is supported for your version of Informix database server, check the database server release notes. See the Administrator's Guide for your database server for general details on how to configure the csm options.

    JCE Security Package

    To use the SECURITY=PASSWORD option, you must install a JDK Java cryptography extension (JCE) compliant security package on the JDBC client and include the installation directory of the security package in the CLASSPATH variable.

    Informix has certified the SunJCE 1.2 security package, which you can download free from the following Web site:

    SunJCE is available only in the U.S. or Canada. If your site does not comply with this or other SunJCE licensing restrictions, you can try using Informix JDBC Driver with other JCE-certified security package providers. However, Informix has not tested and certified that these packages work correctly with Informix database servers configured to use the SPWDCSM csm option.

    To install the SunJCE package, download the SunJCE distribution, extract the JAR file containing the SunJCE provider packages, and make sure the CLASSPATH environment variable includes the extracted JAR filename.

    Edit the jdk1.2/lib/security/java.security file to add the following two lines:


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