Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8771585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:49:35+00:00 2026-06-13T17:49:35+00:00

I’m writing a Java program that queries a PostgreSQL database. I’m following this example

  • 0

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"

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:49:37+00:00Added an answer on June 13, 2026 at 5:49 pm

    Most likely you have failed to register the JDBC driver with the DriverManager, so JDBC doesn’t know how to handle jdbc:postgresql:. Try Class.forName("org.postgresql.Driver"); before you try to use DriverManager.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 named postgrsql or Postgresql or 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:

    // Very bad code, never do this
    try {
        Class.forName("org.postgresql.Driver");
    } catch (ClasNotFoundException ex) {}
    

    Never do the above. Either wrap the exception in an unchecked runtime exception or just add throws ClasNotFoundException to your method definition.


    As per the PgJDBC documentation and FAQ, to use the driver you must:

    • Download the JDBC driver;
    • Ensure that the JDBC driver is on your CLASSPATH;
    • Load/register the driver by passing a JVM parameter or using Class.forName("org.postgresql.Driver"); so it’s registered with the DriverManager;
    • Connect to the database

    These are all links to the manual.

    For more on the CLASSPATH, see wikipedia

    The JDBC DriverManager is discussed in the JavaDoc and the JDBC tutorial.


    As for the JDBC URL format for PostgreSQL, that’s in the documentation too.

    With JDBC, a database is represented by a URL (Uniform Resource
    Locator). With PostgreSQL™, this takes one of the following forms:

    jdbc:postgresql:database
    jdbc:postgresql://host/database
    jdbc:postgresql://host:port/database

    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 getConnection arguments correct, the problem is that you didn’t register the driver first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.