SQLJ connection technique 1 uses the JDBC DriverManager interface
as the underlying means for creating the connection.
To use SQLJ connection technique 1, follow these steps:
- 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.
- Load a JDBC driver by invoking the Class.forName method.
- For the IBM® Data Server Driver for JDBC and SQLJ,
invoke Class.forName this
way:
Class.forName("com.ibm.db2.jcc.DB2Driver");
This step is unnecessary if you use the JDBC 4.0 driver
or later.
- For the DB2® JDBC
Type 2 Driver for Linux, UNIX, and Windows, which is deprecated, invoke Class.forName this
way:
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
- 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 one of the following
forms:
connection-context-class connection-context-object=
new connection-context-class(String url, boolean autocommit);
connection-context-class connection-context-object=
new connection-context-class(String url, String user,
String password, boolean autocommit);
connection-context-class connection-context-object=
new connection-context-class(String url, Properties info,
boolean autocommit);
The meanings of the 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.
- info
- Specifies an object of type java.util.Properties that
contains a set of driver properties for the connection. For the DB2 JDBC
Type 2 Driver for Linux, UNIX and Windows (DB2 JDBC
Type 2 Driver), you should specify only the user and password properties. 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.
Figure 1. Using connection
technique 1 to connect to a data source#sql context Ctx; // Create connection context class Ctx 1
String userid="dbadm"; // Declare variables for user ID and password
String password="dbadm";
String empname; // Declare a host variable
…
try { // Load the JDBC driver
Class.forName("com.ibm.db2.jcc.DB2Driver"); 2
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
Ctx myConnCtx= 3
new Ctx("jdbc:db2://sysmvs1.stl.ibm.com:5021/NEWYORK",
userid,password,false); // Create connection context object myConnCtx
// for the connection to NEWYORK
#sql [myConnCtx] {SELECT LASTNAME INTO :empname FROM EMPLOYEE
WHERE EMPNO='000010'};
// Use myConnCtx for executing an SQL statement