UFO_Rider
UFO_Rider

Reputation: 121

Connection problem while connecting to PostgreSQL database through JDBC

I have an IP address 192.168.218.18 I've tried a lot of ways connecting to that server every time I'm getting a message as connection attempt failed. For security reasons I've hidden the username and password.

Code:

public static void main(String[] args) {

String url = "jdbc:postgresql://192.168.218.18:5432/manikanta?user=*****&password=*****&ssl=true";
        try {

            Connection conn = DriverManager.getConnection(url);

            System.out.println("connection established");
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }
}

Exceptions i got

org.postgresql.util.PSQLException: The connection attempt failed. at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292) at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) at org.postgresql.jdbc.PgConnection.(PgConnection.java:211) at org.postgresql.Driver.makeConnection(Driver.java:458) at org.postgresql.Driver.connect(Driver.java:260) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at com.inno.demo.ConnectionJDBC.main(ConnectionJDBC.java:17) Caused by: java.net.SocketTimeoutException: connect timed out at java.net.DualStackPlainSocketImpl.waitForConnect(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 org.postgresql.core.PGStream.(PGStream.java:75) at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91) at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192) ... 7 more

Upvotes: 0

Views: 4627

Answers (2)

thepoetdj
thepoetdj

Reputation: 773

Since pinging to the IP 192.168.218.18 (I'll refer to this as target system) has failed, verify and confirm one of the following:

  • Your system is connected to the same network as the target system (i.e. Private network)
  • If you have access to the target system, try pinging your local machine's IP from that system and check if pinging is successful from there or not
  • Make sure that your/target system's firewall is configured to allow connections to each other

If you confirm these, I am sure you will at least have a basic idea of what and where it is going wrong. And with that your connection issue will get resolved.

Upvotes: 0

user11809641
user11809641

Reputation: 895

You should be passing the user and password separately, not as part of the URL:

public static void main(String[] args) {

    String url = "jdbc:postgresql://192.168.218.18:5432/v";
    String user = "****";
    String password = "*****";

    try (Connection con = DriverManager.getConnection(url, user, password);

        System.out.println("connection established");

    } catch (SQLException e) {

        System.out.println(e.getMessage());
    }
}

See: http://zetcode.com/java/postgresql/

Upvotes: 1

Related Questions