Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connect DB2 with Java

Tags:

java

db2

How can I connect DB2 with Java in Eclipse? What are some step-by-step instructions?

How can I add the classpath in Eclipse?

Code snippet:

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

public class connection {
    public static void main(String[] argv) {
        try {
            Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
        }
        catch (ClassNotFoundException e) {
            System.out.println("Please include Classpath  Where your DB2 Driver is located");
            e.printStackTrace();
            return;
        }
        System.out.println("DB2 driver is loaded successfully");
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rset = null;
        boolean found = false;
        try {
            conn = DriverManager.getConnection("jdbc:db2:sabarish", "db2admin", "Murugasaranam");
            if (conn != null)
            {
                System.out.println("DB2 Database Connected");
            }
            else
            {
                System.out.println("Db2 connection Failed ");
            }
            pstmt = conn.prepareStatement("Select * from bo");
            rset = pstmt.executeQuery();
            if(rset != null)
            {
                while(rset.next())
                {
                    found = true;
                    System.out.println("Class Code: " + rset.getString("clcode"));
                    System.out.println("Name: " + rset.getString("name"));
                }
            }
            if (found == false)
            {
                System.out.println("No Information Found");
            }
        } catch (SQLException e) {
            System.out.println("DB2 Database connection Failed");
            e.printStackTrace();
            return;
        }
    }

}

On running the code, I got the following exceptions:

java.lang.ClassNotFoundException: COM.ibm.db2.jdbc.app.DB2Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at connection.main(connection.java:11)
like image 443
ramya Avatar asked Dec 16 '11 06:12

ramya


2 Answers

You need to correct the package name.

Class.forName("com.ibm.db2.jdbc.app.DB2Driver");

To add .jar in your project, use menu ProjectPropertiesJava Build Path → tab Select "Libraries"Add External Jars...

like image 160
KV Prajapati Avatar answered Nov 18 '22 04:11

KV Prajapati


Please do try using

Class.forName("com.ibm.db2.jcc.DB2Driver");

This link might help: PUBLIB

like image 7
nIcE cOw Avatar answered Nov 18 '22 05:11

nIcE cOw