无法使用java连接到DB2数据库?

时间:2014-04-11 10:59:45

标签: java db2

我正在尝试连接到位于服务器上的远程DB2数据库。我使用ODBC进行了必要的DSN连接,测试成功。所以在下面的代码中我使用的是我的DSN名称IMRM2,这是正确的吗?

但我的主要问题是,即使将db2jcc.jar,db2jcc_license_cu.jar添加到我的eclipse库后,我也会收到以下错误。 ?

错误:

java.lang.ClassNotFoundException: com.ibm.db2.jcc.DB2Driver
    at java.net.URLClassLoader$1.run(Unknown Source)
    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 ConnectToDb2.makeConnection(CreateChart.java:19)
    at CreateChart.main(CreateChart.java:63)
Caused by: java.util.zip.ZipException: invalid LOC header (bad signature)
    at java.util.zip.ZipFile.read(Native Method)
    at java.util.zip.ZipFile.access$1400(Unknown Source)
    at java.util.zip.ZipFile$ZipFileInputStream.read(Unknown Source)
    at java.util.zip.ZipFile$ZipFileInflaterInputStream.fill(Unknown Source)
    at java.util.zip.InflaterInputStream.read(Unknown Source)
    at sun.misc.Resource.getBytes(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    ... 11 more

Java文件。

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

class ConnectToDb2{

    String jdbcClassName="com.ibm.db2.jcc.DB2Driver";
    String url="jdbc:db2:IMRM2";
    String user="db2inst1";
    String password="DB2";
    Connection connection = null;


        public void makeConnection()
        {   

            try 
            {   
                Class.forName(jdbcClassName);
                connection = DriverManager.getConnection(url, user, password);
            } 

            catch (ClassNotFoundException e) 
            {
                e.printStackTrace();
            } 

            catch (SQLException e) 
            {
                e.printStackTrace();
            }


            finally
            {
                if(connection!=null)
                {
                    System.out.println("Connected successfully.");
                    try 
                    {
                        connection.close();
                    }

                    catch (SQLException e) 
                    {
                        e.printStackTrace();
                    }
                }
            }//End of finally 

        } // End of method makeConnection

}


public class CreateChart {


    public static void main(String[] args) {

        ConnectToDb2 obj_connectdb2 = new ConnectToDb2();

        obj_connectdb2.makeConnection();

    }

}

1 个答案:

答案 0 :(得分:0)

根本问题在此行中显示:

Caused by: java.util.zip.ZipException: invalid LOC header (bad signature)

您的驱动程序文件可能已损坏。如果您使用zip程序打开它,您可能会看到错误。

解决方案:重新下载您的驱动程序。

类似的问题,问题和解决方案: tomcat7: Could not load JDBC driver class [com.mysql.jdbc.Driver]