How to connect DB2 database connectivity using java ?

时间:2016-05-11 11:31:45

标签: java db2

Am trying to connect DB2 database in java but it throwing error, I can't find what issue was that. I added db2jcc.jar and here I show my complete database connectivity code.

public class ConnectionExample {

    public static void main(String[] args) {
        String jdbcClassName="com.ibm.db2.jcc.DB2Driver";
        String url="jdbc:db2://localhost:50000/TestDb";
        String user="user";
        String password="pass@123";

        Connection connection = null;
        try {
            //Load class into memory
            Class.forName(jdbcClassName);
            //Establish connection
            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();
                }
            }
        }

    }

}

Am trying to connect DB2 database with the above code but it throws error.

com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][3.63.123] Exception java.net.ConnectException: Error opening socket to server localhost/127.0.0.1 on port 50,000 with message: Connection refused: connect. ERRORCODE=-4499, SQLSTATE=08001
    at com.ibm.db2.jcc.am.fd.a(fd.java:321)
    at com.ibm.db2.jcc.am.fd.a(fd.java:340)
    at com.ibm.db2.jcc.t4.xb.a(xb.java:433)
    at com.ibm.db2.jcc.t4.xb.<init>(xb.java:90)
    at com.ibm.db2.jcc.t4.a.z(a.java:347)
    at com.ibm.db2.jcc.t4.b.a(b.java:1974)
    at com.ibm.db2.jcc.am.ib.a(ib.java:691)
    at com.ibm.db2.jcc.am.ib.<init>(ib.java:644)
    at com.ibm.db2.jcc.t4.b.<init>(b.java:330)
    at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:231)
    at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:197)
    at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:472)
    at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:113)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at ConnectionExample.main(ConnectionExample.java:18)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.ibm.db2.jcc.t4.x.run(x.java:38)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.ibm.db2.jcc.t4.xb.a(xb.java:419)
    ... 13 more

Hope Someone helps me to find out the solution. Thanks

2 个答案:

答案 0 :(得分:1)

实际上端口50000没有打开,这是我在将端口更改为51020之后出错的原因,它也可以正常连接数据库。

String url="jdbc:db2://localhost:51020/TestDb";

由于

答案 1 :(得分:0)

<强>原因

此问题的可能原因是DB2数据库服务器上未正确启用TCP / IP。

解决问题

使用DB2命令窗口中的db2set DB2COMM命令启动TCP / IP连接:

db2set DB2COMM=protocol_names

例如,要设置数据库管理器以启动TCP / IP通信协议的连接管理器,请输入以下命令:

db2set DB2COMM=tcpip
db2stop
db2start

来源:https://www-304.ibm.com/support/docview.wss?uid=swg21403644