Tutorial on Connecting Java application to IBM DB2 Express-C edition (Windows)

Software Prerequisites

·         IBM DB2 Express-C
·         Java SE JDK

To begin, we will need to write a Java App. An example is provided below, this app
simply shows how to list all the lastname from Employee table in Java app


import java.sql.*;
public class testing{
 public static void main(String args[])
 {


       try
      {
             Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
             Connection con=DriverManager.getConnection("jdbc:db2:sample");
             Statement stmt = con.createStatement();
             ResultSet resultSet=stmt.executeQuery("SELECT Lastname from Employee");
             System.out.println("Got Results!");
             while(resultSet.next()){
                     String data=resultSet.getString("Lastname");
                     System.out.println(data);
             }
             stmt.close();
      }
      
      catch(Exception a){
             System.out.println(a.getMessage());
      }  
 }
}

Next, save the file as testing.java 
Now, we will need to compile the program,
In command prompt, execute the following statement
          
     javac testing.java

To use the JDBC driver provided in the DB2 Express-C, we will need to set the driver    
classpath; to do this, execute the following command in command prompt

            set CLASSPATH=.; <Your DB2 directory>\SQLLIB\java\db2java.zip

Finally, execute the following statement to run the program
            
            java testing


For other variant of DB2 jdbc driver,visit www.razorsql.com/docs/help_db2.html   
for more details

Comments

Popular posts from this blog

Simplex implementation in Java with sample code

Tutorial on porting OpenCV(revision:5578) to android in Windows Environment