Using JDBC with DB2


This document describes how to set up and run some example Java applications that talk to our DB2 server through JDBC.


Setting up example programs on rack40

Note: This step only needs to be performed once.

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

Copy example files and the DB2 JDBC driver to your home directory:

  cp -r ~cps216/db2java ~

Create an example table in DB2:

  cd ~/db2java
  db2 -tf DB2AppSetup.sql
The script above will create an example table named employee in your database.


Running example programs locally on rack40

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

Change your working directory to the one containing example programs:

  cd ~/db2java

Compile the example program:

  javac DB2AppLocal.java

Run the example program:

  java DB2AppLocal

Read DB2AppLocal.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.


Running example programs remotely

Log into the machine from which you want to run your JDBC program. Make sure you have on this machine a copy of the directory containing example files and the JDBC driver (from the directory ~cps216/db2java on rack40).

Change your working directory to the one containing example programs:

  cd ~/db2java

Compile the example program:

  javac DB2AppRemote.java

Make sure ~/db2java/db2java.zip (this is the DB2 JDBC driver) is in your CLASSPATH.

Run the example program:

  java DB2AppRemote <user> <password>
Here, <user> and <password> should be what you use to log into rack40.

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.