IBM Support

100 Tech Tips, #92 Sample Java program to test connection using IBM DB2 JCC Universal Type 4 driver.

Technical Blog Post


Abstract

100 Tech Tips, #92 Sample Java program to test connection using IBM DB2 JCC Universal Type 4 driver.

Body

 
Looking for a Sample Java program to test the connectivity using  IBM Data Server Driver for JDBC and SQLJ ( JCC Type 4 Universal driver) ?
Here is a sample program that uses JCC DataSource to connect to a DB2 database and run a simple query:
 
Sample code to test connectivity using Type 4 IBM DB2 JCC driver:


import java.io.*;
import java.sql.*;

class Jcctest {

  public static void main(String argv[]) {
   
    Connection          con = null;
    Statement           stmt = null;
    ResultSet           rs = null;
     
    try {
      System.out.println("\n Creating IBM DB2 JCC DataSource");
      javax.sql.DataSource ds=null;
      ds=new com.ibm.db2.jcc.DB2SimpleDataSource();

      ((com.ibm.db2.jcc.DB2BaseDataSource) ds).setServerName(argv[0]);
      ((com.ibm.db2.jcc.DB2BaseDataSource) ds).setPortNumber(Integer.parseInt(argv[1]));
      ((com.ibm.db2.jcc.DB2BaseDataSource) ds).setDatabaseName(argv[2]);
      ((com.ibm.db2.jcc.DB2BaseDataSource) ds).setDriverType(4);
                 
      System.out.println("\n Connecting to database using JDBC Universal type 4 driver....");
      con = ds.getConnection(argv[3],argv[4]);
      System.out.println("\n Connected to database successfully.");
          
     System.out.println("\n  Executing query....");
     stmt = con.createStatement();
     String query = "SELECT COUNT(*) FROM SYSCAT.TABLES";
     rs = stmt.executeQuery(query);
     while (rs.next())
     {
       System.out.println("\n " + query + " = " + rs.getInt(1));
     }

      rs.close();
      stmt.close();
     
      System.out.println("\n  Closing connection...");
      con.close();
      System.out.println("\n Connection closed.");
   
    } catch (Exception e) {

      System.out.println(e);
      e.printStackTrace();    }
  } // end main
} // end Jcctest



  1. Save the program as Jcctest.java  ( Note: the name should be the same as the class name and it is Case Sensitive)
  2. Make sure you have the  IBM Data Server Driver for JDBC and SQLJ class files -  db2jcc.jar or db2jcc4.jar in the classpath based on your requirement.
  3. To Compile :   javac Jcctest.java
  4. To Run :           java Jcctest <hostname> <portnumber> <database> <user> <pwd>
 Example:      java Jcctest myhost.lenexa.ibm.com 50000 Sample myuserID mypwd
 Sample output:  
 Creating IBM DB2 JCC DataSource
 Connecting to database using JDBC Universal type 4 driver...
 Connected to database successfully.
  Executing query....
 SELECT COUNT(*) FROM SYSCAT.TABLES = 418
  Closing connection...
 Connection closed.





 

[{"Business Unit":{"code":"BU029","label":"Data and AI"}, "Product":{"code":"SSEPGG","label":"DB2 for Linux- UNIX and Windows"},"Component":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"","Edition":""}]

UID

ibm11141756