IBM DB2 Manual page 122

Table of Contents

Advertisement

4. Invoke the constructor for the connection context class that you created in step
The following code uses connection technique 2 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 {
}
catch (ClassNotFoundException e) {
}
Connection jdbccon=
jdbccon.setAutoCommit(false); // Do not autocommit
Ctx myConnCtx=new Ctx(jdbccon);
#sql [myConnCtx] {SELECT LASTNAME INTO :empname FROM EMPLOYEE
Figure 29. Using connection technique 2 to connect to a data source
106
Application Programming Guide and Reference for Java
Doing this creates a JDBC connection object for the connection to the data
source. You can use any of the forms of getConnection that are specified in
"Connect to a data source using the DriverManager interface with the IBM Data
Server Driver for JDBC and SQLJ".
The meanings of the url, user, and password parameters are:
url A string that specifies the location name that is associated with the data
source. That argument has one of the forms that are specified in "Connect
to a data source using the DriverManager interface with the IBM Data
Server Driver for JDBC and SQLJ". The form depends on which JDBC
driver you are using.
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.
1 on page 105
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 on page 105.
Class.forName("com.ibm.db2.jcc.DB2Driver");
e.printStackTrace();
DriverManager.getConnection("jdbc:db2://sysmvs1.stl.ibm.com:5021/NEWYORK",
userid,password);
WHERE EMPNO='000010'};
// Create connection context class Ctx
// Declare variables for user ID and password
// Declare a host variable
// Load the JDBC driver
// Create JDBC connection object jdbccon
// Create connection context object myConnCtx
// for the connection to NEWYORK
// Use myConnCtx for executing an SQL statement
1
2
3
4

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents