I’m writing a Java program that queries a PostgreSQL database. I’m following this example and have trouble here:
connection = DriverManager.getConnection(
"jdbc:postgresql://127.0.0.1:5432/testdb", "mkyong",
"123456");
According to the JavaDoc for DriverManager the first string is “a database url of the form jdbc:subprotocol:subname. When I connect to the server I type in psql -h dataserv.abc.company.com -d app -U emp24 and give the password qwe123 (for example sake). What should the first argument of getConnection be?
I’ve tried
connection = DriverManager.getConnection(
"jdbc:postgresql://dataserv.abc.company.com", "emp24",
"qwe123");
and get the run time error: no suitable driver found.
I’ve download JDBC4 Postgresql Driver, Version 9.2-1000.
After I fixed my program to load the driver with Class.forName("org.postgresql.Driver"); it recognises the JDBC URL but it still doesn’t connect. I now have a new error.
When I run the program there is an error and here is the output with the stack trace:
-------- PostgreSQL JDBC Connection Testing ------------
PostgreSQL JDBC Driver Registered!
Connection Failed! Check output consoleorg.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:207)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:140)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)
at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:23)
at org.postgresql.Driver.makeConnection(Driver.java:393)
at org.postgresql.Driver.connect(Driver.java:267)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at DatabaseConnect.main(DatabaseConnect.java:32)
Caused by: java.net.ConnectException: Connection timed out: 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 java.net.Socket.connect(Unknown Source)
at org.postgresql.core.PGStream.<init>(PGStream.java:60)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:101)
... 11 more
I format the URL for the getConnection like this:
"jdbc:postgresql://dataserv.abc.company.com:5432/app"
Most likely you have failed to register the JDBC driver with the DriverManager, so JDBC doesn’t know how to handle
jdbc:postgresql:. TryClass.forName("org.postgresql.Driver");before you try to useDriverManager.getConnection. That is shown in the example code you linked to (it’s the very first line), is explained in the preamble of the DriverManager documentation, and is also explained in detail in the PgJDBC documentation linked below.Alternately, maybe you’ve typo’d
jdbc:postgresql:so the DriverManager is looking for a driver namedpostgrsqlorPostgresqlor something, which won’t be registered.Finally, you could be swallowing a class loading exception so the driver load fails and you don’t see it, like this (extremely bad) code:
Never do the above. Either wrap the exception in an unchecked runtime exception or just add
throws ClasNotFoundExceptionto your method definition.As per the PgJDBC documentation and FAQ, to use the driver you must:
CLASSPATH;Class.forName("org.postgresql.Driver");so it’s registered with the DriverManager;These are all links to the manual.
For more on the
CLASSPATH, see wikipediaThe JDBC
DriverManageris discussed in the JavaDoc and the JDBC tutorial.As for the JDBC URL format for PostgreSQL, that’s in the documentation too.
The docs go on to explain what each parameter means and the optional connection parameters.
From this you can see, in answer to your comment on John Woo’s answer, that you do not have to specify the port if your server is listening on the default PostgreSQL port, which it is if you don’t have to specify the port when connecting with
psql.That makes your
getConnectionarguments correct, the problem is that you didn’t register the driver first.