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.
The following code uses connection technique 4 to create a connection to a location
with logical name jdbc/sampledb. The connection requires a user ID and password.
SQLJ connection technique 5: Use a previously created
connection context
SQLJ connection technique 5 uses a previously created connection context to
connect to the data source.
In general, one program declares a connection context class, creates connection
contexts, and passes them as parameters to other programs. A program that uses
the connection context invokes a constructor with the passed connection context
object as its argument.
Program CtxGen.sqlj declares connection context Ctx and creates instance oldCtx:
#sql context Ctx;
...
// Create connection context object oldCtx
Program test.sqlj receives oldCtx as a parameter and uses oldCtx as the argument
of its connection context constructor:
void useContext(sqlj.runtime.ConnectionContext oldCtx)
// oldCtx was created in CtxGen.sqlj
{
Ctx myConnCtx=
new Ctx(oldCtx); // Create connection context object myConnCtx
// from oldCtx
#sql [myConnCtx] {SELECT LASTNAME INTO :empname FROM EMPLOYEE
WHERE EMPNO='000010'};
// Use myConnCtx for executing an SQL statement
...
}
SQLJ connection technique 6: Use the default connection
SQLJ connection technique 6 uses the default connection to connect to the data
source. It should be used only in situations where the database thread is controlled
by another resource manager, such as the Java stored procedure environment.
#sql public static context Ctx
with (dataSource="jdbc/sampledb"); 2
// Create connection context class Ctx
String userid="dbadm"; // Declare variables for user ID and password
String password="dbadm";
String empname; // Declare a host variable
...
Ctx myConnCtx=new Ctx(userid, password); 3
// Create connection context object myConnCtx
// for the connection to jdbc/sampledb
#sql [myConnCtx] {SELECT LASTNAME INTO :empname FROM EMPLOYEE
WHERE EMPNO='000010'};
// Use myConnCtx for executing an SQL statement
Figure 31. Using connection technique 4 to connect to a data source
Chapter 4. SQLJ application programming 109