Using Java to access CLSD

Using Java to access CLSD

CLSD can be accessed by JAVA clients that use JDBC to send SQL queries and/or commands to CLSD. Such clients may collect requests from a command-line, GUI, or Web-based interface.

Such clients may also themselves serve other programs that collect SQL requests and therefore become "relays" to CLSD.

This page will provide examples of both approaches. It will start with a discussion of Java Database Connectivity (JDBC), and discuss various issues related to JDBC drivers leading to a presentation of the source code for a Java servlet that uses JDBC to query CLSD.

This page will then discuss the demonstration WebServices relay that can be used to access CLSD.

JDBC

JDBC is an application program interface (API) specification for accessing databases from Java. IBM provides JDBC drivers for use with DB2, and the example below shows how to use the JDBC Connection class to make an initial connection to CLSD.
import java.sql.*;

public class MyDB2Connection
{
   public MyDB2Connection()
   {
   }

   public Connection getConnection( String accountName, 
                                   String accountPassword )
   {
      try
      {
          Class.forName( "com.ibm.db2.jcc.DB2Driver" );
          Connection con = DriverManager.getConnection(
            "jdbc:db2://libra45.uits.iu.edu:50000/clsd2", 
	    accountName, accountPassword );
      } 
      catch ( Exception e )
      {
          System.out.println( "Can't connect to CLSD." );
          System.out.println( e.toString() );
      }
      return null;
   }
}
After creating the connection, standard JDBC commands are used to fetch individual rows of data as well as table metadata. IBM supplies a short tutorial on writing JDBC applications to interact with DB2.

To run a program like this, you must have a DB2 driver JAR file in your classpath.

There are several JDBC drivers for DB2 available via free download from IBM, or as part of a client distribution.

The DB2 Universal driver, also known as the Java Common Client (JCC) driver, is itself written entirely in Java and can therefore run on any system hosting a JVM. You can, for example, download it to any Java-enabled desktop to support Java scripts that run on that desktop.

You may download the DB2 Universal driver, here.

The DB2 Universal driver JAR files are already installed on Libra45 in ~db2inst2/sqllib, and you can put their pathnames in your CLASSPATH or "source" the DB2 profile file, db2profile, as in:

      source ~db2inst2/sqllib/db2profile

You can find the source code for a Java servlet that accesses CLSD here for use as a reference.

(Note that JDBC converts data among Java, DB2, and JDBC datatypes according to this datatype mapping scheme.)

Accessing CLSD via the demonstration WebServices relay

WebServices allow functions on one computer to call functions running on another computer. The most popular WS framework is JAX-RPC, and SDS provides a demonstration service, CLSDservice, for those wishing to construct clients that can access CLSD via Java WITHOUT JDBC.

The following components are available for inspection:

Note that WS clients must be run with appropriate JAR files in their classpaths, and both requires many of the AXIS JAR files.

Note also that WS clients are usually constructed using a different approach that allows direct reference to the invoked remote functions. However, such clients also require the use of WSDL2Java, or equivalent, to construct client stubs, and correct installation of those stubs on the system runnning the client software.