验证安装 IBM Data Server Driver for JDBC and SQLJ

要验证安装是否成功, IBM Data Server Driver for JDBC and SQLJ ,请编译并运行任何简单的 JDBC 应用程序。

例如,您可以编译并运行以下程序来验证您的安装:
/**
 * File: TestJDBCSelect.java
 *
 * Purpose: Verify IBM Data Server Driver for JDBC and SQLJ installation.  
 *          This program uses IBM Data Server Driver for JDBC and SQLJ
 *          type 2 connectivity on Db2 for z/OS.
 *
 * Authorization: This program requires SELECT authority on
 *                DB2 catalog table SYSIBM.SYSTABLES.
 *
 * Flow:
 *   - Load the IBM Data Server Driver for JDBC and SQLJ.
 *   - Get the driver version and display it.
 *   - Establish a connection to the local Db2 for z/OS server.
 *   - Get the DB2 version and display it.
 *   - Execute a query against SYSIBM.SYSTABLES.
 *   - Clean up by closing all open objects.
 */

import java.sql.*;

public class TestJDBCSelect
{
  public static void main(String[] args)
  {
    try
    {
      // Load the driver and get the version
      System.out.println("\nLoading IBM Data Server Driver for JDBC and SQLJ");
      Class.forName("com.ibm.db2.jcc.DB2Driver");
      System.out.println("  Successful load. Driver version: " +
        com.ibm.db2.jcc.DB2Version.getVersion());

      // Connect to the local Db2 for z/OS server
      System.out.println("\nEstablishing connection to local server");
      Connection conn = DriverManager.getConnection("jdbc:db2:");
      System.out.println("  Successful connect");
      conn.setAutoCommit(false);

      // Use DatabaseMetaData to determine the DB2 version
      System.out.println("\nAcquiring DatabaseMetaData");
      DatabaseMetaData dbmd = conn.getMetaData();
      System.out.println("  DB2 version: " +
                           dbmd.getDatabaseProductVersion());

      // Create a Statement object for executing a query
      System.out.println("\nCreating Statement");
      Statement stmt = conn.createStatement();
      System.out.println("  successful creation of Statement");
      // Execute the query and retrieve the ResultSet object
      String sqlText = 
          "SELECT CREATOR, "       +
                 "NAME "           +
          "FROM SYSIBM.SYSTABLES " +
          "ORDER BY CREATOR, NAME";
      System.out.println("\nPreparing to execute SELECT");
      ResultSet results = stmt.executeQuery(sqlText);
      System.out.println("  Successful execution of SELECT");
  
      // Retrieve and display the rows from the ResultSet
      System.out.println("\nPreparing to fetch from ResultSet");
      int recCnt = 0;
      while(results.next())
      {
        String creator = results.getString("CREATOR");
        String name    = results.getString("NAME");
        System.out.println("CREATOR: <" + creator + "> NAME: <" + name + ">");
        
        recCnt++;
        if(recCnt == 10) break;
      }
      System.out.println("  Successful processing of ResultSet");

      // Close the ResultSet, Statement, and Connection objects
      System.out.println("\nPreparing to close ResultSet");
      results.close();
      System.out.println("  Successful close of ResultSet");

      System.out.println("\nPreparing to close Statement");
      stmt.close();
      System.out.println("  Successful close of Statement");

      System.out.println("\nPreparing to rollback Connection");
      conn.rollback();
      System.out.println("  Successful rollback");

      System.out.println("\nPreparing to close Connection");
      conn.close();
      System.out.println("  Successful close of Connection");
    }
    // Handle errors  
    catch(ClassNotFoundException e)
    {
      System.err.println("Unable to load IBM Data Server Driver " +
        "for JDBC and SQLJ, " + e);
    }
    catch(SQLException e)
    {
      System.out.println("SQLException: " + e);
      e.printStackTrace();
    }
  }
}