JDBC tutorial

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

The example 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 the example 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 server. From a command prompt, enter the following commands:
         ftp <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 server as long as you transfer it into the integrated file system.
  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/jdk15/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) PARM(My_Program_Name.java)
         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 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.