Using JDBC with DB2


Index


Getting started

Log into rack40 and set up DB2 environment (see Getting Started with DB2 for detailed instructions).

To set the sample database used by the example code, type db2 -tf ~cps216/examples/jdbc/ExampleSetup.sql. The script will create an example table named employee in your database. Next, copy the JDBC driver and example code to your home directory: cp -r ~cps216/examples/jdbc/ ~/.


Connecting to a local database

Make sure your DB2 environment is set up correctly on rack40. Go into ~/jdbc/ and compile the example: javac DB2AppLocal.java. To run the example, type java DB2AppLocal.

DB2AppLocal.java connects to a local DB2 database running on the same machine. Read the code to get an idea about how JDBC works. Pay special attention to the driver class name and connection URL string, as they are different for local and remote connections.


Connecting to a remote database

If you are on a remote machine, make sure that you have on this machine a copy of DB2AppRemote.java and db2java.zip. To compile the example, type javac DB2AppRemote.java. To run the example, type java -classpath db2java.zip:. DB2AppRemote user password. Here, user and password should be what you use to log into rack40. If you set your CLASSPATH environment variable to include the full path to your copy of db2java.zip, then you do not need the -classpath option.

Read DB2AppRemote.java to get an idea about how JDBC works. Pay attention to the driver class name and connection URL string, as they are different for local and remote connections.


JDBC basics

You can find the JDBC documentation, together with the rest of the Java 2 Platform API's online.