JDBC support
Db2 for z/OS supports applications that use Sun Microsystems JDBC interfaces to access Db2 data by using dynamic SQL. Support for JDBC enables organizations to write Java™ applications that access local Db2 data, and access remote relational data on a server that supports DRDA.
Sun Microsystems developed the JDBC specifications. The JDBC specifications define a set of APIs (based on ODBC) that allow Java applications to access relational data. The APIs provide a generic interface for writing applications that run on multiple platforms and can access any SQL database. The APIs are defined within 16 classes, and they support basic SQL functions for connecting to a database, running SQL statements, and processing results. Together, these interfaces and classes represent the JDBC capabilities by which a Java application can access relational data.
This example shows a portion of a JDBC program for that keeps an inventory of books.
/*********************************************************/
/* Determine which table to update, then build SQL */
/* statement dynamically. */
/*********************************************************/
String tableName = null;
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT TYPE FROM " +
" BOOK_TYPES WHERE " +
" TITLE = \"" + bkTitle + "\"");
if (rs.next())
{
if (rs.getString(1).equalsIgnoreCase("FICTION"))
tableName = "FICTION_BOOKS";
else
tableName = "NON_FICTION_BOOKS";
/*********************************************************/
/* PREPARE and EXECUTE the statement */
/*********************************************************/
stmt.executeUpdate("UPDATE " + tableName + " SET INVENTORY = INVENTORY-1 " +
"WHERE TITLE = \"" + bkTitle + "\"");
}
rs.close();
stmt.close();
Db2 for z/OS support for JDBC offers a number of advantages for accessing Db2 data:
- JDBC combines the benefit of running your applications in a z/OS environment with the portability and ease of writing Java applications.
- The JDBC interface offers the ability to change between drivers and access various databases without recoding your Java program.
- JDBC applications do not require precompiles or binds.
- JDBC provides a consistent interface for applications to query and retrieve system catalog information across the Db2 Database family of database management systems. This capability reduces the need to write catalog queries that are specific to each database server.