Sqlj Connection Technique 3: Jdbc Datasource Interface - IBM DB2 Manual

Table of Contents

Advertisement

SQLJ connection technique 3: JDBC DataSource interface

SQLJ connection technique 3 uses the JDBC DataSource as the underlying means
for creating the connection.
To use SQLJ connection technique 3, follow these steps:
1. Execute an SQLJ connection declaration clause.
Doing this generates a connection context class. The simplest form of the
connection declaration clause is:
#sql context context-class-name;
The name of the generated connection context class is context-class-name.
2. If your system administrator created a DataSource object in a different program,
follow these steps. Otherwise, create a DataSource object and assign properties
to it.
a. Obtain the logical name of the data source to which you need to connect.
b. Create a context to use in the next step.
c. In your application program, use the Java Naming and Directory Interface
(JNDI) to get the DataSource object that is associated with the logical data
source name.
3. Invoke the JDBC DataSource.getConnection method.
Doing this creates a JDBC connection object for the connection to the data
source. You can use one of the following forms of getConnection:
getConnection();
getConnection(user, password);
The meanings of the user and password parameters are:
user and password
Specify a user ID and password for connection to the data source, if the
data source to which you are connecting requires them.
If the data source is a DB2 for z/OS system, and you do not specify these
parameters, DB2 uses the external security environment, such as the RACF
security environment, that was previously established for the user. For a
CICS connection, you cannot specify a user ID or password.
4. If the default autocommit mode is not appropriate, invoke the JDBC
Connection.setAutoCommit method.
Doing this indicates whether you want the database manager to issue a
COMMIT after every statement. The form of this method is:
setAutoCommit(boolean autocommit);
For environments other than the environments for CICS, stored procedures, and
user-defined functions, the default autocommit mode for a JDBC connection is
true. To disable autocommit, invoke setAutoCommit(false).
5. Invoke the constructor for the connection context class that you created in step
1.
Doing this creates a connection context object that you specify in each SQL
statement that you execute at the associated data source. The constructor
invocation statement needs to be in the following form:
connection-context-class connection-context-object=
new connection-context-class(Connection JDBC-connection-object);
The JDBC-connection-object parameter is the Connection object that you created
in step 3.
Chapter 4. SQLJ application programming
107

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents