Databases Reference
In-Depth Information
5.12.1 Getting connection context
The SQLj program needs to connect to the database before executing any SQL
statement. For getting a connection context, following these steps:
1. Declare a class for connection context. This can be done using the following
statement:
# sql context context-class-name
This declaration creates a class with the name context-class-name. To use
this class to create a context object, declaration must be global and should
not be inside the class.
2. Load the JDBC driver. You do this the same way we have done for the JDBC
program.
3. Invoke the constructor of the context class.
The Example 5-26 shows how to establish the connection in the SQLj program.
Example 5-26 SQLj connection context
#sql context ctx; // This should be outside the class
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
ctx ctx1=new ctx(“jdbc:db2:sample”,false);
A Connection context for the SQLj program can also be created using the
Connection object. See Example 5-27.
Example 5-27 SQLj Connection Context from Connection object
#sql context ctx; // This should be outside the class
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
Connection con=DriverManager.getConnection();
ctx ctx1=new ctx(con);
5.12.2 Manipulating data
A connection context class object can be used to execute any SQL statement.
To monitor and control the SQL statements while executing, we need to create
the ExecutionContext class. An ExecutionContext class object can be created by
calling the method getExecutionContext of connection context. Some of the
ExecutionContext methods are applicable before executing the SQL statement
while some of them are applicable only after execution of the statement.
Search WWH ::




Custom Search