javajdbcdb2database-server

Connect to db2 server in windows


I am connecting to a db2 database server using this piece of java code.

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DatabaseConnect {

    Connection co=null;
        Statement st=null;
        PreparedStatement pstmt;
        ResultSet rs;

        int status;

        void makeConnection() throws ClassNotFoundException, SQLException
        {
            Class.forName("com.ibm.db2.jcc.DB2Driver");
            co=DriverManager.getConnection("jdbc:db2://localhost:50000/databasename","username","password");
            //st=co.createStatement();
        }
        Connection getConnection()
        {
            return co;
        }
        void endConnection() throws SQLException
        {
            co.close();
        }

        public static void main(String args[]) throws ClassNotFoundException, SQLException
        {
            DatabaseConnect db=new DatabaseConnect();
            db.makeConnection();

            new MaintainHash().createHashMap(db.getConnection());

            new Login(db.getConnection()).setVisible(true);

        }

}

It works good if hostname is localhost but when i provide hostname of remote server, the program hangs and shows this error.

    Exception in thread "main" com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][3.68.61] Exception java.net.ConnectException: Error opening socket to server /192.168.80.1 on port 50,000 with message: Connection timed out: connect. ERRORCODE=-4499, SQLSTATE=08001
    at com.ibm.db2.jcc.am.gd.a(gd.java:329)
    at com.ibm.db2.jcc.am.gd.a(gd.java:410)
    at com.ibm.db2.jcc.t4.ac.a(ac.java:439)
    at com.ibm.db2.jcc.t4.ac.<init>(ac.java:96)
    at com.ibm.db2.jcc.t4.a.b(a.java:358)
    at com.ibm.db2.jcc.t4.b.newAgent_(b.java:2066)
    at com.ibm.db2.jcc.am.Connection.initConnection(Connection.java:780)
    at com.ibm.db2.jcc.am.Connection.<init>(Connection.java:725)
    at com.ibm.db2.jcc.t4.b.<init>(b.java:333)
    at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:233)
    at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:199)
    at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:474)
    at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:115)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at DatabaseConnect.makeConnection(DatabaseConnect.java:31)
    at DatabaseConnect.main(DatabaseConnect.java:58)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at com.ibm.db2.jcc.t4.w.run(w.java:49)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.ibm.db2.jcc.t4.ac.a(ac.java:425)
    ... 14 more
Java Result: 1
BUILD SUCCESSFUL (total time: 25 seconds)

I am able to telnet and ping to the host ip address.


Solution

  • Got it. The solution is actually quite simple as well as a bit off topic. Thanks Ian for pointing it out. I had to add port number of db2 to the inbound and outbound rules of firewall. If anyone is having the same problem, you can follow these steps: