Adsense

Tuesday, October 21, 2014

Db2 Jars and Class Path set to connect db2 database using java code with JDBC driver.

If your are using DB2 database in your java application, it is always required to write code for connecting DB2 database. Db2 Database can be connected by either using connection pool or  using JDBC. The following example java code is for connecting and accessing DB2 database using JDBC. It loads the DB2 JDBC Driver using Class.forName  and connects to the DB2 database Employee and prints the records of the table Department. For running the program you have to do the following. Assume that Employee database, qual table already been created in DB2.
1. Download DB2 JDBC driver for your db2 version from http://www-01.ibm.com/support/docview.wss?uid=swg21363866.

2. Include db2 driver path in the CLASSPATH
Steps to include to db2 drivers in the class path:

Copy the below db2 driver files  to any folder for example d:\jars\db2drivers.
 1. db2jcc.jar
 2. db2jcc_license_cisuz.jar
 3. db2jcc_license_cu.jar
4,  db2java.zip

              Now set the classpath to include the driver files as follows.
d:\db2test>set CLASSPATH=.;d:\jars\db2drivers\db2jcc.jar;d:\jars\db2drivers\db2jcc_license_cisuz.jar;d:\jars\db2drivers\db2jcc_license_cu.jar;d:\jars\db2drivers\db2java.zip;.
OR permanantly set by Right click on MyComputer ->Advanced Tab - > Environment variables - > select CLASSPATH and edit , set the variable value to above path.

 For connecting db2 in Servlet / DAO in struts, include the above driver files in the libraries (WebContent\WEB-INF\lib). If you use any development tool (IDE) like WSAD or RAD or Myeclipe, then you can include drivers for your application by accessing
                      Project->Properties->  Add External Jar from Project -> Java Build Path -> Libraries Tab -> Add External Jars in the IDE. 
package com.javaonline;

 import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

 public class Db2ConnectJava {
  public static void main(String[] argv) {
  try {
   
 // Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
  Class.forName("com.ibm.db2.jcc.DB2Driver");
  
  } 
  catch (ClassNotFoundException e) 
  {
   System.out.println("Class not found. Please include the right db2 jars in the Classpath");
   e.printStackTrace();
   return;
  }
  
  System.out.println("Great. DB2 driver is loaded successfully");
  
  Connection conn = null;
  PreparedStatement pstmt = null;
  ResultSet rs=null;
  try {
   conn = DriverManager.getConnection("jdbc:db2:employee");
   
  if (conn != null)  System.out.println("Connection Sucessful");
  else System.out.println("Connection Failed ");
  
  pstmt=conn.prepareStatement("Select * from EmpList");
  rs=pstmt.executeQuery();
  
  System.out.println("S.No          Emp.Code                        Name                     Pay Scale ");
  System.out.println("----------------------------------------------------------------------------------");
  
  int sno=0;
  if(rs!=null)
  {
   while(rs.next())
   {
    sno++;
    System.out.println(sno + ".           "+rs.getString("empcode") +"                          "+ rs.getString("name") +"               "+ rs.getString("payscale"));
       }
  }
  
  if (sno<=0) 
  {
   System.out.println("No Employees Found");
  }
  
  } catch (SQLException e) {
   System.out.println("Some Error Occurs...");
   e.printStackTrace();
   return;
  }
  }
  }
 
Running the above program, you will get the below output.



1 comment:

  1. i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful. best apps for doordash drivers

    ReplyDelete