INFORMIX
IBM Informix Embedded SQLJ User's Guide
Appendix A: Connecting to Databases
Contents Index

Connecting to Databases

"Connecting to a Database" describes how Embedded SQLJ programs connect to databases. This appendix provides background information and information about using nondefault connection contexts.

The ConnectionManager Class

You use the ConnectionManager class to make a connection to a database, as described in "Connecting to a Database". The ConnectionManager class has two methods:

The newConnection() method creates and returns a new JDBC Connection object using the current values of the DRIVER, DBURL, UID, and PWD attributes. If any of the needed attributes is null, or a connection cannot be established, an error message is printed to System.out, and the program exits.

The initContext() method returns the currently installed default context. If the current default context is null, a new default context instance is created and installed using a connection obtained from a call to getConnection.

Database URLs

The DBURL data member of the ConnectionManager class and the value for the -url option that you specify for on-line checking are database URLs (see "On-Line Checking" for information about on-line checking). Database URLs specify the subprotocol (the database connectivity mechanism), the database or server identifier, and a list of properties.

Your Embedded SQLJ program uses Informix JDBC Driver to connect to an Informix database. Informix JDBC Driver supports database URLs of the following format:

In the preceding syntax:

JHimportantJHImportant: Spaces are not allowed in the database URL.

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

Database URL Variable Required? Description

ip-address or

domain-name

Yes

The IP address or the domain name of the computer running the Informix database server.

An example of an IP address is 123.45.67.89.

An example of a domain name is myhost.com.

port-number

Yes

The port number of the Informix database server.

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 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.

username

Yes

The name of the user you want to connect to the Informix database or database server as.

password

Yes

The password of the user specified by username.

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 value of name is case insensitive.

Refer to the Informix JDBC Driver Programmer's Guide for information about environment variables supported by Informix JDBC Driver and how to set them.

Using Nondefault Connection Contexts

This section explains how to use nondefault connection contexts. Embedded SQLJ uses a connection-context object to manage the connection to the database in which you want an SQL statement to execute. You can specify different connection-context objects for different SQL statements in the same Embedded SQLJ program, as shown in the sample program MultiConnect.sqlj included in this section.

To use a nondefault connection context

    1. Define the connection-context class by using an Embedded SQLJ connection statement. The syntax of the connection statement is as follows:

    2. Create a connection-context object for connecting to the database.

    3. Specify the connection-context object in your Embedded SQLJ statement in parentheses following the #sql string.

MultiConnect.sqlj

The sample program MultiConnect.sqlj creates two databases with two tables, Orders and Items, and inserts two records in the Orders table with corresponding records in the Items table. The program prints the order line items for all the orders from both tables, which reside in different databases, by creating separate connection contexts for each database.

MultiConnect.sqlj calls the methods executeSQLScript() and getConnect(). These methods are contained in demoUtil.java, which follows this program.




IBM Informix Embedded SQLJ User's Guide, Version 1.0
Copyright © 1999, IBM Corporation. All rights reserved.