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

<pre><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();
                }
            }
        }

    }

}
</code></pre>

<p>Am trying to connect DB2 database with the above code but it throws error.</p>

<pre><code>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.&lt;init&gt;(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.&lt;init&gt;(ib.java:644)
    at com.ibm.db2.jcc.t4.b.&lt;init&gt;(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
</code></pre>

<p>Hope Someone helps me to find out the solution. Thanks</p>