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.

About this task

You use the default connection by specifying your SQL statements without a connection context object. When you use this technique, you do not need to load a JDBC driver unless you explicitly use JDBC interfaces in your program.
The default connection context can be:
  • The connection context that is associated with the data source that is bound to the logical name jdbc/defaultDataSource
  • An explicitly created connection context that has been set as the default connection context with the ConnectionContext.setDefaultContext method. This method of creating a default connection context is not recommended.

In a stored procedure that runs on Db2 for z/OS®, or for a CICS® or IMS application, when you use the default connection, Db2 for z/OS uses the implicit connection.

Example

The following SQLJ execution clause does not have a connection context, so it uses the default connection context.
#sql {SELECT LASTNAME INTO :empname FROM EMPLOYEE 
   WHERE EMPNO='000010'};  // Use default connection for
                           // executing an SQL statement