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 JDBCDB2 JDBC offers a number of advantages for accessing DB2 data:
DB2 for OS/390 SQLJ/JDBC DriverThe 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 ImplementationDB2 for OS/390 provides two implementations of JDBC.
You select a version of the JDBC driver by specifying the associated file name in your CLASSPATH environment variable. NOTE
NOTE
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 IdentificationThe 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.
Connecting to a Data SourceThere 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 InstanceA 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:
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: databaseNameSpecifies 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: getDatabaseNameReturns the location name of the data source. java.lang.String getDatabaseName() setDatabaseNameSets the location name of the data source. voidsetDatabaseName(java.lang.String dbname) descriptionDescribes 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: getDescriptionReturns a description of the data source. java.lang.String getDescription() setDescriptionSets the description of the data source. voidsetDescription(java.lang.String description) userSpecifies 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: getUserReturns the user ID to be used when connecting to the data source. java.lang.String getUser() setUserSets the user ID that is to be used for connecting to the data source. voidsetUser(java.lang.String user) passwordSpecifies 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: setPasswordSets the password that is to be used for connecting to the data source. void setPassword(java.lang.String password) planNameSpecifies 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: getPlanNameGets the name of the plan that DB2 allocates for connections that are established using the data source object. java.lang.String getPlanName() setPlanNameSets the name of the plan that DB2 allocates for connections that are established using the data source object. voidsetPlanName(java.lang.String planName) loginTimeoutSpecifies 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: getLoginTimeoutGets the maximum time in seconds to wait for a connection. int getLoginTimeout() setLoginTimeoutSets the maximum time in seconds to wait for a connection. voidsetLoginTimeout(int seconds) JDBC APIThe 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:
Running a JDBC ApplicationWhen 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 |