Acquiring a DriverManager connection to a database

Using the DriverManager class, the connection to the database is identified by a database Uniform Resource Locator (URL) that is provided to the JDBC driver.

About this task

A DriverManager connection is configured for an OSGi JVM server by specifying the Db2® JDBC jars and native DLLs in the JVM profile OSGI_BUNDLES and LIBPATH_SUFFIX JVM profile.

In a Liberty JVM server, the DriverManager connection can be configured in two ways:

Using the CICS® JDBC Liberty feature
To use the CICS JDBC Liberty feature, add the cicsts:jdbc-1.0 feature to your server.xml file.
Using one of the Liberty JDBC features
If your server.xml is configured with one of the Liberty JDBC features e.g. jdbc-4.2, then you must make the Db2 driver available to your application by configuring a global library. For example, in your server.xml:
<library id="global">
  <fileset dir="/usr/lpp/db2v13/jdbc/classes"/>
</library>

You will also need to have a dataSource element present in your server.xml. This will be added for you if you configure the -Dcom.ibm.cics.jvmserver.wlp.jdbc.driver.location option in your JVM profile.

The JDBC driver recognizes two types of URL:
Default URL
A default URL does not include the location name of a Db2 subsystem. A default URL for Db2 can be specified in one of two formats:

jdbc:db2os390sqlj:
or
jdbc:default:connection
When a default URL is specified, the application is given a connection to the local Db2 to which CICS is connected. If your installation uses Db2 data sharing, you can access all the data in your sysplex from the local Db2.
Explicit URL
An explicit URL includes the location name of a Db2 subsystem. The basic structure of an explicit URL for Db2 for z/OS® is:

jdbc:db2os390:<location-name>
or
jdbc:db2os390sqlj:<location-name>
Typically, the location name is the name of the local Db2 to which CICS is connected. However, you can specify the name of a remote Db2 to access. In this case, CICS uses the local Db2 as a pass-through, and uses Db2 Distributed Data Facilities to access the remote Db2.

It is advisable to use a default URL when accessing Db2 with type 2 connectivity. The use of an explicit URL might cause specific actions when a connection closes that could be inconvenient when multiple programs are used in the same application suite. Also, when a default URL is used, the behavior of the connection is not affected by the JDBC driver version.

To acquire a connection, your Java application needs to invoke the getConnection() method with the URL. For example:
Connection connection =
DriverManager.getConnection("jdbc:default:connection");

When connecting an OSGi Java program to Db2, add com.ibm.db2.jcc;resolution:=optional to the Import-Package in your bundle manifest.