Overview of the sample Java applications

This topic describes the DB2eAppl.java and the DB2eJavaCLP.java sample applications.

Sample 1: DB2eAppl.java

DB2eAppl.java demonstrates how to code a JDBC application for DB2 Everyplace.

The major steps of the DB2eAppl.java application are:

Step 1:
Import the java.sql package.

Step 2:
Load the DB2 Everyplace JDBC driver com.ibm.db2e.jdbc.DB2eDriver.

Step 3:
Connect to the database in the current directory, the directory that the DB2eAppl.java application will be run in.

Step 4:
Create a Statement object.

Step 5:
Set up the very simple sample database, consisting of an EMPLOYEE table that contains two records. This is done using the executeUpdate(String sql) method of the java.sql.Statement interface.

Step 6:
Select all records from the EMPLOYEE table, and retrieve the rows by using the next() method of the java.sql.ResultSet interface.

Step 7:
Drop the EMPLOYEE table from the database and release database and JDBC resources.

The DB2eAppl.java source code below contains comments that show where the steps explained above are being used.

import java.sql.*; //Step 1
 
public class DB2eAppl
{
    public static void main(String[] args) {
 
        String driver = "com.ibm.db2e.jdbc.DB2eDriver";
        String url    = "jdbc:db2e:mysample";
 
        try {
            Class.forName(driver); //Step 2
            Connection con = DriverManager.getConnection(url); //Step 3
            Statement st = con.createStatement(); //Step 4
 
            //Create table: employee  //Step 5
            st.executeUpdate("CREATE TABLE employee (EMPNO CHAR(6), FIRSTNAME VARCHAR(12))");
            System.out.println("*** Created table: employee");
 
            //Add records to employee
            st.executeUpdate("INSERT INTO employee VALUES ('112233','John')");
            st.executeUpdate("INSERT INTO employee VALUES ('445566','Mary')");
            System.out.println("*** Inserted two records");
 
            //Query and display results //Step 6
            ResultSet rs = st.executeQuery("SELECT * FROM employee");
            System.out.println("*** Query results:");
            while (rs.next()) {
                System.out.print("EMPNO=" + rs.getString(1) + ", ");
                System.out.println("FIRSTNAME=" + rs.getString(2));
            }
 
            //Delete table: employee  //Step 7
            st.executeUpdate("Drop table employee");
            System.out.println("*** Deleted table: employee");
 
            rs.close();
            st.close();
            con.close();
 
        } catch (SQLException sqlEx) {
            while(sqlEx != null)
            {
                System.out.println("[SQLException] " +
                                   "SQLState: " + sqlEx.getSQLState() + 
                                   ", Message: " + sqlEx.getMessage() +
                                   ", Vendor: " + sqlEx.getErrorCode() );
                sqlEx = sqlEx.getNextException();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Sample 2: DB2eJavaCLP.java

DB2eJavaCLP.java is a Java command line processor for DB2 Everyplace.

Restriction:
On Palm OS, the DB2eJavaCLP.java sample application is not supported.

Related tasks

Related concepts