通过JDBC连接到远程db2数据库失败

时间:2015-12-30 16:12:55

标签: jdbc db2

我是db2的新手。我想问一下是否可以通过我的java应用程序连接到远程db2服务器上的db2数据库?仅仅拥有类型4的jdbc驱动程序?

我在构建路径上有db2jcc4驱动程序。

当我尝试这个简单的代码时

 public static void main(String[] args) {
        Class.forName("com.ibm.db2.jcc.DB2Driver");
        System.out.println("Driver loaded");

        try {
            Connection dbConn =     DriverManager.getConnection("jdbc:db2://192.168.0.103:50000/ABC", "****", "*****");
        } catch (SQLException e) {
            e.printStackTrace();
        }

}

连接因此异常而失败。我无法追踪到底发生了什么?

 com.ibm.db2.jcc.am.SqlException: [jcc][t4][10262][11223][4.19.26] Unexpected Throwable caught: null. ERRORCODE=-4228, SQLSTATE=null
at com.ibm.db2.jcc.am.kd.a(Unknown Source)
at com.ibm.db2.jcc.am.kd.a(Unknown Source)
at com.ibm.db2.jcc.am.kd.a(Unknown Source)
at com.ibm.db2.jcc.t4.b.b(Unknown Source)
at com.ibm.db2.jcc.t4.b.a(Unknown Source)
at com.ibm.db2.jcc.t4.b.a(Unknown Source)
at com.ibm.db2.jcc.t4.b.a(Unknown Source)
at com.ibm.db2.jcc.t4.b.<init>(Unknown Source)
at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(Unknown Source)
at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(Unknown Source)
at com.ibm.db2.jcc.DB2Driver.connect(Unknown Source)
at com.ibm.db2.jcc.DB2Driver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.connect.db2.ConnectToServer.main(ConnectToServer.java:22)


 Caused by: java.lang.NullPointerException
at com.ibm.db2.jcc.am.Connection.completeSqlca(Unknown Source)
at com.ibm.db2.jcc.t4.z.q(Unknown Source)
at com.ibm.db2.jcc.t4.z.p(Unknown Source)
at com.ibm.db2.jcc.t4.z.l(Unknown Source)
at com.ibm.db2.jcc.t4.z.d(Unknown Source)
at com.ibm.db2.jcc.t4.b.k(Unknown Source)
at com.ibm.db2.jcc.t4.b.b(Unknown Source)
at com.ibm.db2.jcc.t4.b.a(Unknown Source)
... 12 more

getconnection后的db2diag.log也是:

 PID     : 5110                 TID : 140245920114432 PROC : db2sysc 0
 INSTANCE: sachindb             NODE : 000            DB   : ABC
 APPHDL  : 0-46                 APPID: 192.168.0.101.64054.151230182945
 AUTHID  : SACHINDB             HOSTNAME: Sachin
 EDUID   : 22                   EDUNAME: db2agent (ABC) 0
 FUNCTION: DB2 UDB, base sys utilities, sqleIsDatabaseDirectoryConnectable, probe:6835
 MESSAGE : ZRC=0x8005006D=-2147155859=SQLE_CA_BUILT
      "SQLCA has been built and saved in component specific control   block."
 DATA #1 : SQLCA, PD_DB2_TYPE_SQLCA, 136 bytes
 sqlcaid : SQLCA     sqlcabc: 136   sqlcode: -1035   sqlerrml: 0
 sqlerrmc: 
 sqlerrp : SQLE534 
 sqlerrd : (1) 0x00000000      (2) 0x00000000      (3) 0x00000000
       (4) 0x00000000      (5) 0x00000000      (6) 0x00000000
 sqlwarn : (1)      (2)      (3)      (4)        (5)       (6)    
       (7)      (8)      (9)      (10)        (11)     
 sqlstate:      

1 个答案:

答案 0 :(得分:0)

您是否在Java 7中使用AES加密机制?如果是这样,我通过Googull在IBM.com

找到了这个解决方案

否则,请尝试直接创建DB2SimpleDataSource对象。

private DB2SimpleDataSource db2;
private void configureDataSource() {

     db2 = new DB2SimpleDataSource();
     db2.setServerName("192.168.0.103");
     db2.setPortNumber(50000);
     db2.setDatabaseName("ABC");
     db2.setUser("****");
     db2.setPassword("*****");
     db2.setReadOnly(true);          
}

然后按照此Connection c = db2.getConnection()或此Connection c = db2.getConnection("****", "*****")

进行连接

另外......见this similar question