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)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ramya
  • 928
  • 5
  • 14
  • 30

9 Answers9

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
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:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kanagavelu Sugumar
  • 18,766
  • 20
  • 94
  • 101
7

Please do try using

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

This link might help: PUBLIB

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • my funny question, do you know something about AS400, because I can't found driver for outdated DB2 version 6.1 – mKorbel May 23 '12 at 13:56
  • 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](http://www-03.ibm.com/systems/i/software/toolbox/downloads.html) and this [JT400admin](http://sourceforge.net/projects/jt400/). Glad you found it :-) – nIcE cOw May 23 '12 at 14:01
  • I have to connect db2, then I starting to search on this forum – mKorbel May 23 '12 at 14:05
  • 1
    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...) – Peter Mortensen Sep 27 '22 at 14:32
6

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
buruzaemon
  • 3,847
  • 1
  • 23
  • 44
3

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

Class.forName("com.ibm.as400.access.AS400JDBCDriver");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mike.tihonchik
  • 6,855
  • 3
  • 32
  • 47
1

These two drivers are loaded from different JARs. The latter is loaded from 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
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")

0

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

com.ibm.db2.jcc.DB2Driver

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
venkatesh A
  • 113
  • 2
  • 7