AnandKumar Selvamani
AnandKumar Selvamani

Reputation: 399

How to connect DB2 database connectivity using java ?

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

Upvotes: 3

Views: 7924

Answers (2)

AnandKumar Selvamani
AnandKumar Selvamani

Reputation: 399

Actually the port 50000 is not opened that is the reason I got the error after change that port to 51020 it works fine also it connects with database.

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

Thanks

Upvotes: 1

Nicolas Filotto
Nicolas Filotto

Reputation: 45005

Cause

A possible cause for this problem is that TCP/IP is not properly enabled on your DB2 database server.

Resolving the problem

Use the db2set DB2COMM command from the DB2 command window to start the TCP/IP connection:

db2set DB2COMM=protocol_names

For example, to set the database manager to start connection managers for the TCP/IP communication protocols, enter the following commands:

db2set DB2COMM=tcpip
db2stop
db2start

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

Upvotes: 0

Related Questions