15

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)

9 Answers 9

14

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...

Sign up to request clarification or add additional context in comments.

4 Comments

Mine uses com.ibm.db2.jcc.DB2Driver ...not sure what the difference is
@ESP Added my answer for your question.
could someone please add a link to download and install the driver?
the 'jcc" driver will bypass the jdbc protocol if the database is local.
7

Please do try using

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

This link might help: PUBLIB

4 Comments

my funny question, do you know something about AS400, because I can't found driver for outdated DB2 version 6.1
Lol, my BAD, never heard of AS400 before, just that much that it's associated with IBM i guess not sure though, but have a look at this LINK and this JT400admin. Glad you found it :-)
I have to connect db2, then I starting to search on this forum
The link is (effectively) broken: It doesn't redirect, but the result is the same as a redirect to a generic page (search page): It includes "1022 results" (a page with 1022 links...)
7

The driver name is depends on the driver we are using.

  • Use COM.ibm.db2.jdbc.app.DB2Drive when db2java.zip is in your path.

  • Use com.ibm.db2.jcc.DB2Driver when db2jcc.jar and db2jcc_license_cu.jar are in your classpath.

Also follow the below tutorials:

Comments

6

I think you need to put db2jcc.jar in your classpath.

2 Comments

hi Could you please help with how to set the class path in eclipse
@ramya, I think AVD's answer has got your covered there.
3

Neither of the examples in the previous answers worked for me, but this did:

Class.forName("com.ibm.as400.access.AS400JDBCDriver");

Comments

1

These two drivers are loaded from different JARs. The latter is loaded from jt400.

1 Comment

What is "jt400"?
1

Your URL is a type 2 driver connectivity URL ("jdbc:db2:sabarish").

The driver class you are using is from the legacy DB2 JDBC driver which is out of support, but it is still available inside the DB2 server installation, e.g, C:\Program Files\IBM\SQLLIB\java\db2java.zip.

In Eclipse, right click on the Java project → select Properties. In the properties window, go to Java build Path. Select the libraries tab. Click the Add External Jars button and add the db2java.zip file from the above DB2 installation location.

Recommendation:

If you want to stay with latest DB2 server and drivers, download the driver JAR files from the IBM fix central.

You need to register for the first time to create an IBM ID. In the bundle, you will find db2jcc.jar. It’s based on the JDBC3 specification. In the bundle, you will find db2jcc4.jar. It’s based on the JDBC4 specification.

Add any one of the JAR files in your project as mentioned above. Load the driver class as below.

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

This supports both type 2 and type 4 connectivity.

Comments

0

Well, you first need to have the DB2 Driver in your classpath; namely the db2jcc4.jar file. A syntax mistake that I noticed is:-

You have the line as follows

conn = DriverManager.getConnection("jdbc:db2:sabarish","db2admin","Murugasaranam"); _______________________________________^^^_________________________________

You should add two forward slash characters(/) after db2: and before sabarish like this

conn = DriverManager.getConnection("jdbc:db2://sabarish","db2admin","Murugasaranam")

Comments

0

For the DB2 old 8.x version, you need to add this driver:

com.ibm.db2.jcc.DB2Driver

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.