Java Database Connectivity (JDBC)

Java Database Connectivity (JDBC) is a Java API for connecting and interactive with relational databases.

The JDBC features are listed in the following table:

Table 1. JDBC features
Feature Support Supported Java Versions Supported Enterprise Java Versions
jdbc-4.0 JDBC version 4.0
  • Java SE 8
  • Java SE 11
  • Java SE 17
  • Java EE 6
jdbc-4.1 JDBC version 4.1
  • Java SE 8
  • Java SE 11
  • Java SE 17
  • Java EE 6
  • Java EE 7
jdbc-4.2 JDBC version 4.2
  • Java SE 8
  • Java SE 11
  • Java SE 17
  • Java EE 6
  • Java EE 7
  • Java EE 8
  • Jakarta EE 8
  • Jakarta EE 9
  • Jakarta EE 10
jdbc-4.3 JDBC version 4.3
  • Java SE 11
  • Java SE 17
  • Java EE 6
  • Java EE 7
  • Java EE 8
  • Jakarta EE 8
  • Jakarta EE 9
  • Jakarta EE 10
cicsts:jdbc-1.0 CICS® support for JDBC for Db2® type 2 connections
  • Java SE 8
  • Java SE 11
  • Java SE 17
  • Java EE 6
  • Java EE 7
  • Java EE 8
  • Jakarta EE 8
  • Jakarta EE 9
  • Jakarta EE 10

For more information about configuring JDBC support, see Configuring database connectivity with JDBC.

Developing applications with database interaction

There are two methods of interacting with the Db2 database.

Data Source

The javax.sql.DataSource can be looked up in JNDI by using the jndiName defined in the dataSource or cicsts_dataSource configuration element. This javax.sql.DataSource object can be used to get a java.sql.Connection object to interact with the database.

Depending on the implementation of javax.sql.DataSource, it might be important to commit or roll back the connection before closing. For more information, see Differences between the Liberty JDBC features and the CICS JDBC feature.

Driver Manager

The java.sql.DriverManager class can be used to get java.sql.Connection to interact with the database. The connection URL is jdbc:default:connection for type 2 connectivity in CICS. For more information, see Connecting to a data source using the DriverManager interface with the IBM Data Server Driver for JDBC and SQLJ

Differences between the Liberty JDBC features and the CICS JDBC feature

Liberty provides JDBC features jdbc-4.0, jdbc-4.1, jdbc-4.2, and jdbc-4.3 to connect to various relational databases. In CICS Liberty, Liberty JDBC support can be used to connect to a Db2 database through the CICS DB2CONN with type 2 connectivity, or remotely with a type 4 connectivity. Liberty JDBC supports the java.sql and javax.sql APIs and provides implementations of javax.xml.DataSource through the dataSource configuration.

CICS provides a JDBC feature cicsts:jdbc-1.0 to support type 2 connectivity through the CICS DB2CONN. CICS supports the java.sql API, and provides an implementation of javax.sql.DataSource through the cicsts_dataSource configuration.
Note: The cicsts_dataSource is not fully compatible with dataSource. Some Liberty features require the use of a dataSource. For example, a dataSource must be used for batch-1.0 persistence.

CICS and Liberty JDBC support transactions that use the JDBC API. For JDBC type 2 connectivity, Db2 updates are managed as part of the CICS unit of work through the CICS DB2CONN resource. Liberty and CICS JDBC differ in how connections are managed.

Liberty JDBC will automatically close the connection after processing completes, causing the transaction to be rolled back or committed, depending on the commitOrRollbackOnCleanup attribute of the dataSource element. The default setting is rollback, so an implicit java.sql.Connection.commit() must be called, causing the CICS unit of work to be committed.

CICS JDBC is managed by the CICS unit of work. The database updates are committed or rolled back when the CICS task commits or rolls back.

Table 2. JDBC transactional differences
Type Connection Type Autocommit Default Clean Behavior
CICS JDBC Type 2 false Commit the CICS UOW.
Liberty JDBC Type 2 false Roll back the CICS UOW.
Type 4 true or false Commit the local transaction. The CICS UOW is not affected.

A global transaction can be used to manage the transactional scope data sources. In global transactions, the transactional behavior for Liberty and CICS javax.sql.DataSource is consistent. For more information about global transactions in Liberty, see Java Transaction API (JTA).

Examples

Connecting to a database with the javax.sql.DataSource interface, with a dataSource or cicsts_dataSource configuration specifying the jndiName jdbc/defaultCICSDataSource.
import java.sql.Connection;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.sql.DataSource;

public class DataSourceExample
{
    // Inject the DataSource resource using JNDI
    @Resource(name = "jdbc/defaultCICSDataSource")
    private DataSource dataSource;

    public void accessDb() throws SQLException
    {
        try(Connection conn = dataSource.getConnection())
        {
            // Interact with SQL using the Connection object
        }
    }
}
Connecting to a database with the java.sql.DriverManager class that uses the cicsts_jdbcDriver configuration.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DriverManagerExample
{
    public void accessDatabase() throws SQLException
    {
        try(Connection conn = DriverManager.getConnection("jdbc:default:connection"))
        {
            // Interact with SQL using the Connection object
        }
    }
}