JDBC tutorial

The following is a tutorial on writing a Java™ Database Connectivity (JDBC) program and having it run on the an iSeries™ server with the native JDBC driver. It is designed to show you the basic steps required for your program to run JDBC.

Example: JDBC creates a table and populates it with some data. The program processes a query to get that data out of the database and to display it on the screen.

Run the example program

To run the example program, perform the following steps:

  1. Copy the program to your workstation.
    1. Copy Example: JDBC and paste it into a file on your workstation.
    2. Save the file with the same name as the public class provided and with the .java extension. In this case, you must name the file BasicJDBC.java on your local workstation.
  2. Transfer the file from your workstation to your iSeries server. From a command prompt, enter the following commands:
         ftp <iSeries server name>
         <Enter your user ID>
         <Enter your password>
         cd /home/cujo
         put BasicJDBC.java
         quit

    For these commands to work, you must have a directory in which to put the file. In the example, /home/cujo is the location, but you can use any location you want.

    Note: It is possible that the FTP commands mentioned previously may be different for you based on how your server is set up, but they should be similar. It does not matter how you transfer the file to your iSeries server as long as you transfer it into the integrated file system. Tools such as VisualAge® for Java can fully automate this process for you.
  3. Make sure you set your classpath to the directory where you put the file in so that your Java commands find the file when you run them. From a CL command line, you can use WRKENVVAR to see what environment variables are set for your user profile.
    • If you see an environment variable named CLASSPATH, you must ensure that the location where you put the .java file in is in the string of directories listed there or add it if the location has not been specified.
    • If there is no CLASSPATH environment variable, you must add one. This can be accomplished with the following command:
           ADDENVVAR ENVVAR(CLASSPATH)
           VALUE('/home/cujo:/QIBM/ProdData/Java400/jdk13/lib/tools.jar')
    Note: To compile Java code from the CL command, you must include the tools.jar file. This JAR file includes the javac command.
  4. Compile the Java file into a class file. Enter the following command from the CL command line:
         java class(com.sun.tools.javac.Main) prop(BasicJDBC)
         java BasicJDBC

    You can also compile the Java file from QSH:

         cd /home/cujo
         javac BasicJDBC.java

    QSH automatically ensures that the tools.jar file can be found. As a result, you do not have to add it to your classpath. The current directory is also in the classpath. By issuing the change directory (cd) command, the BasicJDBC.java file is also found.

    Note: You can also compile the file on your workstation and use FTP to send the class file to your iSeries server in binary mode. This is an example of Java's ability to run on any platform.

    Run the program by using the following command from either the CL command line or from QSH:

         java BasicJDBC

    The output is as follows:

         ----------------------
         | 1 | Frank Johnson  |
         |                    |
         | 2 | Neil Schwartz  |
         |                    |
         | 3 | Ben Rodman     |
         |                    |
         | 4 | Dan Gloore     |
         ----------------------
         There were 4 rows returned.
         Output is complete.
         Java program completed.

For more information on Java and JDBC, consult the following resources:

Related concepts
Types of JDBC drivers
JDBC requirements
Related tasks
Use JNDI for the examples