connect DB2 with Java

痞子三分冷 提交于 2020-07-31 08:00:16

问题


I need to connect DB2 with Java. Kindly help me to connect DB2 with java in ECLIPSE. It would be of great help if you could guide me step by step Please let me know how to add 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)

回答1:


You need to correct the package name.

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

To add .jar in your project => Project + Properties + Java Build Path + Select "Libraries" tab + Add External Jars...




回答2:


Please do try using

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

Hopefully this link might help you a bit more. PUBLIB

Regards




回答3:


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 & db2jcc_license_cu.jar are in your classpath.

Also follow the below tutorial
Razorsql Help
IBM JDBC help




回答4:


Think you need to put db2jcc.jar on your classpath.




回答5:


Neither of the examples above worked for me, but this did:

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



回答6:


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




回答7:


Your URL is a T2 connectivity url.( "jdbc:db2:sabarish") The driver class you are using is from the legacy db2 jdbc driver which is out of support but still available inside db2 server installation e.g (C:\Program Files\IBM\SQLLIB\java\db2java.zip) in eclipse , right click on the java project-> select Properties. In properties window go to Java build Path. select libraries tab. click Add External Jars button and add the db2java.zip from the above db2 installation location.

Recommendation: If you want to stay with latest db2 server and drivers, download the driver jars from the IBM fix central. http://www-01.ibm.com/support/docview.wss?uid=swg21363866 You need to register for first time to create an IBM id. In the bundle you will find db2jcc.jar its based on JDBC3 specification. In the bundle you will find db2jcc4.jar its based on JDBC4 specification. Add any one of the jar file in your project as mentioned above. load the driver class as below. Class.forName("com.ibm.db2.jcc.DB2Driver"); This supports both T2 and T4 connectivity.




回答8:


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")




回答9:


for db2 old 8.x version you need to add this driver com.ibm.db2.jcc.DB2Driver



来源:https://stackoverflow.com/questions/8530711/connect-db2-with-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!