JDBC

Team-Fly    

 
DB2 Universal Database for OS/390 v7.1 Application Certification Guide
By Susan Lawson
Table of Contents
Chapter 13.  Java Programming

JDBC

JDBC is an API that provides a uniform SQL interface to a large selection of relational databases and a common base on which higher level interfaces and tools can be developed. DB2 for OS/390's support for JDBC enables you to write Java applications that access local DB2 data or remote relational data on a server that supports Distributed Relational Database Architecture (DRDA).

To understand JDBC, knowing about its purpose and background is helpful. Sun Microsystems developed the specifications for a set of APIs that allow Java applications to access relational data. The purpose of the APIs is to provide a generic interface for writing platform-independent applications that can access any SQL database. The APIs are defined within several classes and interfaces that support basic SQL functionality for connecting to a database, executing SQL statements, and processing results. On the server side, JDBC supports both fenced and unfenced UDFs and stored procedures in Java. Of course, on OS/390, all stored procedures are fenced to protect the environment.

On the client side, JDBC is very similar to DB2 ODBC and has the same features as DB2 ODBC, with a few more advantages. First, by using JDBC you can code Java applets to be executed by a Java-enabled browser, allowing your data to be accessed from the Web without additional client software. Second, you can access relational databases by using an object-oriented interface, because JDBC is integrated into the Java language.

A JDBC applet is a program written in Java that can be invoked by any Java-enabled Web browser when a page is displayed. By using the facilities of JDBC, the applet can connect to the database and retrieve and display data. Use of JDBC applets allows computers that can access the Internet to access your data on DB2.

Advantages of DB2 JDBC

DB2 JDBC offers a number of advantages for accessing DB2 data:

  • JDBC combines the benefit of running your applications in an OS/390 environment with the portability and ease of writing Java applications. Using the Java language, you can write an application on any platform and execute it on any platform to which the Java Development Kit (JDK) is ported.

  • The ability to develop an application once and execute it anywhere offers the potential benefits of reduced development, maintenance, and systems management costs, and flexibility in supporting diverse hardware and software configurations.

  • The JDBC interface offers the ability to change between drivers and access a variety of databases without recoding your Java program.

  • JDBC applications do not require precompiles.

DB2 for OS/390 SQLJ/JDBC Driver

The Sun JDBC specification provides for 4 types of drivers, two of which are used by application writers, and two that are used by driver writers.

Type 1: ODBC drivers. ODBC binary codeand in many cases, database client codemust be loaded on each client machine that uses a JDBC-ODBC Bridge. Sun provides a JDBC-ODBC Bridge driver, which is appropriate for experimental use and for situations in which no other driver is available.

Type 2: A native-API partly Java technology-enabled driver: This type of driver converts JDBC calls into calls on the client API for Oracle, Sybase, Informix, DB2, or other DBMS. Note that, like the bridge driver, this style of driver requires that some binary code be loaded on each client machine.

Type 3: Pure Java Driver for Database Middleware: This style of driver translates JDBC calls into the middleware vendor's protocol, which is then translated to a DBMS protocol by a middleware server. The middleware provides connectivity to many different databases.

Type 4: Direct-to-Database Pure Java Driver: This style of driver converts JDBC calls into the network protocol used directly by DBMSs, allowing a direct call from the client machine to the DBMS server and providing a practical solution for intranet access.

The DB2 for OS/390 SQLJ/JDBC driver is implemented as a type 2 driver. The type 2 driver translates JDBC calls into calls to a DB2 language interface module. The driver complies with the JDBC 1.2 specification and some of the JDBC 2.0 specification.

Several packages are included with the DB2 for OS/390 SQLJ/JDBC driver. These packages represent the DB2 for OS/390 implementation of the java.sql JDBC API. The driver packages include all of the JDBC classes, interfaces, and exceptions that comply with the JDBC specification.

The DB2 for OS/390 SQLJ/JDBC driver is available under two different Java class names . The preferred driver name is COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriver class. However, to maintain compatibility with existing DB2 for OS/390 JDBC applications, the following driver name is also supported: ibm.sql.DB2Driver. The ibm.sql.DB2Driver class will automatically forward all driver API calls to the COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriver.

DB2's JDBC Implementation

DB2 for OS/390 provides two implementations of JDBC.

  1. The first is an SQLJ/JDBC driver, which is fully compliant with SUN's JDBC 1.2 and SQLJ Part 0 specifications. To use this driver, DB2 for OS/390 requires an OS/390 Java Runtime Environment (JRE) version 1.1.6 or higher.

  2. The second is an SQLJ/JDBC driver, which is fully compliant SUN's JDBC 1.2 and SQLJ Part 0 specification and includes most of the functions of the JDBC 2.0 specification. To use this driver DB2 for OS/390 requires an OS/390 Java Runtime Environment (JRE) Version 1.3 or higher. You will need to use this JDBC driver if you use any of the following functions:

    - The JDBC 2.0 DataSource function

    - Global transactions that run under WebSphere Application Server Version 4.0 and above

You select a version of the JDBC driver by specifying the associated file name in your CLASSPATH environment variable.

NOTE

graphics/note_icon.jpg

You can download the specification for the JDBC drivers from the JDBC Web site: http://java.sun.com/products/jdbc.


NOTE

graphics/note_icon.jpg

The JDK includes a Java compiler, Java Virtual Machine (JVM), and Java Debugger. You can learn more about the JDK from the Java for OS/390 Web site: http://www.ibm.com/s390/java.


A Java application executes under the JVM. The Java application first loads the JDBC driver (by invoking the Class.forName()method), in this case the DB2 for OS/390 SQLJ/JDBC driver, and subsequently connects to the local DB2 subsystem or a remote DRDA application server (by invoking the DriverManager.getConnection method.

Target Data Source Identification

The Java application identifies the target data source it wants to connect to by passing a database Uniform Resource Locator (URL) to the DriverManager. The basic structure for the URL is

 jdbc:<subprotocol>:<subname> 

Specify either of the following URL values for a DB2 for OS/390 data source:

 jdbc:db2os390:<location-name>  jdbc:db2os390sqlj:<location-name> 

Each format results in the same behavior. Both subprotocols are provided for compatibility with existing DB2 for OS/390 JDBC applications. If location-name is not the local site, location-name must be defined in the SYSIBM.LOCATIONS catalog table. If location-name is the local site, location-name must have been specified in field DB2 LOCATION NAME of the Distributed Data Facility panel during DB2 installation.

In addition to the URL values shown above for a DB2 for OS/390 data source, there are two URL values that have special meaning for the DB2 for OS/390 SQLJ/JDBC driver.

  • If a URL value does not specify a location name, for example, jdbc:db2os390:, you will be connected to the local DB2 site. This format of the URL value is a DB2 for OS/390 defined extension. By using this URL value, a DB2 for OS/390 JDBC application does not need to know the location name of the local DB2 subsystem that the driver is using.

  • The SQLJ specification defines the following URL: jdbc:default:connection. When you use this URL value, your application is connected to the local DB2 site.

Connecting to a Data Source

There are two ways to connect to a data source. One method is available when you use the JDBC 1.2 driver or later. The other method is available only when you use the JDBC 2.0 driver or later.

When using the JDBC 1.2 driver the application attempts a connection to a data source, by requesting a java.sql.Connection implementation from the DriverManager (part of the java.sql package). The DriverManager searches all of the registered java.sql.Driver implementations for a driver that is capable of accepting the database URL. It then invokes the first JDBC driver that supports the subprotocol that is specified in the URL (and is registered with the DriverManager).

In this case, the DB2 for OS/390 SQLJ/JDBC driver (which is registered with the DriverManager) accepts the URL and returns a java.sql.Connection implementation that represents the database connection.

When using the JDBC 2.0 driver to connect to a data source, your application can reference a data source using a logical name, rather than an explicit driver class name and URL. In addition, you can define or modify the data source attributes without changing the JDBC application program.

JDBC 2.0 data source support is a complete replacement for the previous JDBC driver manager support. You can use both types of support in the same application, but it is recommended that you use data source support to obtain connections, regardless of whether you use connection pooling or distributed transactions.

The application uses JNDI to associate a logical name with a specific data source implementation. This data source object contains all of the information that is necessary to determine the correct JDBC driver and return a java.sql.Connection object to the specified data source.

Creating a DB2DataSource Instance

A DataSource object is the Java representation of a data source. The DataSource interface provides methods for connecting to the data source.

A DataSource object works with a JNDI naming service. A JNDI naming service provides a way for an application to access remote services over a network. Before you can use a DataSource object, the object needs to be created and registered with JNDI. In general, creating and managing DataSource objects is not done by the applications that use those objects.

If you use an application server, such as WebSphere, to write your JDBC applications, the application server creates and registers DataSource objects for you. This section contains information that you need if you create the DataSource objects yourself.

Two DB2 for OS/390 JDBC classes implement the javax.sql.DataSource interface:

  1. com.ibm.db2.jcc.DB2DataSource includes the methods that are necessary for a JDBC application to establish a connection to a data source using JDBC 2.0 support. This class also contains support for connection pooling.

  2. com.ibm.db2.jcc.DB2SimpleDataSource includes the same methods and properties as com.ibm.db2.jcc.DB2DataSource, but this class does not contain support for connection pooling. Because CICS does not support connection pooling, you need to use this class for CICS applications.

A DB2DataSource or DB2SimpleDataSource class provides a set of properties that define how the connection to a particular data source should be made. Those properties are usually set when a DataSource object is created and deployed. Those properties are:

databaseName

Specifies the location name to be used when establishing connections using the data source object. If the location name is not the local site the location name must be defined in SYSIBM.LOCATIONS. If the location name is the local site, the location name must have been specified in field DB2 LOCATION NAME of the DISTRIBUTED DATA FACILITY panel during the DB2 installation. If you do not set the database property, connections that are established using this data source object are to the local site. This property has data type String. The default value is null. The methods to get and set the value of this property are:

getDatabaseName

Returns the location name of the data source.

 java.lang.String getDatabaseName() 
setDatabaseName

Sets the location name of the data source.

 voidsetDatabaseName(java.lang.String dbname) 
description

Describes the data source object. This property has data type String. The default value is null. The methods to get and set the value of this property are:

getDescription

Returns a description of the data source.

 java.lang.String getDescription() 
setDescription

Sets the description of the data source.

 voidsetDescription(java.lang.String description) 
user

Specifies the OS/390 user ID to be used when using the DataSource object to establish a connection to the data source. DB2 validates the user ID and password. You can override this property by calling the DataSource.getConnection method with the user parameter. If you set the user property, or specify user parameter in the DataSource.getConnection method call, you must also set the password property, or specify the password parameter in the DataSource.getConnection method call. This property has data type String. The default value is null. The methods to get and set the value of this property are:

getUser

Returns the user ID to be used when connecting to the data source.

 java.lang.String getUser() 
setUser

Sets the user ID that is to be used for connecting to the data source.

 voidsetUser(java.lang.String user) 
password

Specifies a corresponding password for the user property. You can override this property by calling the DataSource.getConnection method with the password parameter. This property has data type String. The default value is null. The method to set the value of this property is:

setPassword

Sets the password that is to be used for connecting to the data source.

 void setPassword(java.lang.String password) 
planName

Specifies the name of the plan that DB2 allocates for connections that are established using the data source object. This property has data type String. The default value is DSNJDBC. The methods to get and set the value of this property are:

getPlanName

Gets the name of the plan that DB2 allocates for connections that are established using the data source object.

 java.lang.String getPlanName() 
setPlanName

Sets the name of the plan that DB2 allocates for connections that are established using the data source object.

 voidsetPlanName(java.lang.String planName) 
loginTimeout

Specifies the maximum time in seconds to wait for the DataSource object to connect to a data source. A value of 0 means that the timeout value is the default system timeout value, which is specified by the db2.connpool.connect.create.timeout property in the db2sqljjdbc.properties file. This property has a data type of int. The default value is 0. The methods to get and set the value of this property are:

getLoginTimeout

Gets the maximum time in seconds to wait for a connection.

 int getLoginTimeout() 
setLoginTimeout

Sets the maximum time in seconds to wait for a connection.

 voidsetLoginTimeout(int seconds) 

JDBC API

The JDBC API consists of the abstract Java interfaces that an application program uses to access databases, execute SQL statements, and process the results. Like ODBC, JDBC is a dynamic SQL interface. Writing a JDBC application is similar to writing a C application using ODBC to access a database. Four main interfaces perform these functions:

  • The DriverManager and DB2DataSource classes create database connections. DB2DataSource is available in JDCB 2.0 and above. DriverManager is available in JDBC 1.2 and above.

  • The Connection interface supports the connection to a specific database.

  • The Statement interface supports all SQL statement execution. This interface has two underlying interfaces:

    - The PreparedStatement interface supports any SQL statement containing input parameter markers.

    - The CallableStatement interface supports the invocation of a stored procedure and allows the application to retrieve output parameters.

  • The ResultSet interface provides access to the results that a query generates. The ResultSet interface is similar to the cursor that is used in SQL applications in other languages.

Running a JDBC Application

When you create a Java application that uses the JDBC interfaces, you import the java.sql package and invoke methods according to the JDBC specification. After coding your program, compile it as you would any other Java program. No precompile or bind steps are required to run a Java program.


Team-Fly    
Top


DB2 Universal Database for OS. 390 v7. 1 Application Certification Guide
DB2(R) Universal Database for OS/390 V7.1 Application Certification Guide (IBM DB2 Certification Guide Series)
ISBN: 0131007718
EAN: 2147483647
Year: 2002
Pages: 163
Authors: Susan Lawson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net