Acquiring a connection to a database

Before executing SQL statements, a JDBC or SQLJ application must acquire a connection to a database. The application connects to a target data source using one of two Java™ interfaces.

About this task

The two Java interfaces are:
  • DriverManager: This class connects an application to a database which is specified by a database URL.
  • DataSource: This interface is preferred over DriverManager because it allows details about the underlying database to be transparent to your application making applications more portable. The DataSource implementation is created externally to the Java program and obtained with a Java Naming and Directory Interface (JNDI) DataSource name lookup. The DataSource interface is only available in a Liberty JVM server.

In an OSGi JVM server only type 2 connectivity using DriverManager is supported for use with DB2®.

In a Liberty JVM server, you can use the DriverManager or DataSource interfaces to access Db2.

If you are using JDBC type 2 connectivity, you do not need to specify a user ID and password, as the existing CICS® Db2 security procedures are used instead. It is also advisable to use a default URL, for more detail, see Committing a unit of work.

There are JDBC examples for both OSGi and Liberty JVM servers supplied in the IBM® CICS SDK for Java. For more information, see Developing Java applications to run in a Liberty JVM server.

For more information on using the JDBC DriverManager and DataSource interfaces to acquire a connection, and sample code that you can use in your application, see the Db2 for z/OS® : Programming for Java which is appropriate for your version of Db2.