Sqlj Connection Technique 2: Jdbc Drivermanager Interface - IBM DB2 Manual

Table of Contents

Advertisement

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.
info
Specifies an object of type java.util.Properties that contains a set of
driver properties for the connection. For the IBM Data Server Driver for
JDBC and SQLJ, you can specify any of the properties listed in "Properties
for the IBM Data Server Driver for JDBC and SQLJ".
autocommit
Specifies whether you want the database manager to issue a COMMIT after
every statement. Possible values are true or false. If you specify false,
you need to do explicit commit operations.
The following code uses connection technique 1 to create a connection to location
NEWYORK. The connection requires a user ID and password, and does not require
autocommit. The numbers to the right of selected statements correspond to the
previously-described steps.
#sql context Ctx;
String userid="dbadm";
String password="dbadm";
String empname;
...
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
Ctx myConnCtx=
new Ctx("jdbc:db2://sysmvs1.stl.ibm.com:5021/NEWYORK",
userid,password,false);
#sql [myConnCtx] {SELECT LASTNAME INTO :empname FROM EMPLOYEE
WHERE EMPNO='000010'};
Figure 28. Using connection technique 1 to connect to a data source

SQLJ connection technique 2: JDBC DriverManager interface

SQLJ connection technique 2 uses the JDBC DriverManager interface as the
underlying means for creating the connection.
To use SQLJ connection technique 2, 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. Load a JDBC driver by invoking the Class.forName method.
v Invoke Class.forName this way:
Class.forName("com.ibm.db2.jcc.DB2Driver");
This step is unnecessary if you use the JDBC 4.0 driver.
3. Invoke the JDBC DriverManager.getConnection method.
// Create connection context class Ctx
// Declare variables for user ID and password
// Declare a host variable
// Load the JDBC driver
// Create connection context object myConnCtx
// for the connection to NEWYORK
// Use myConnCtx for executing an SQL statement
Chapter 4. SQLJ application programming
1
2
3
105

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents