views:

397

answers:

2

This is my first time using java to access databases, so I probably have a simple mistake here, but when I go to retrieve my connection from a remote database I have access to, I get a connection refused.

Here's the code in question:

String url = "jdbc:postgresql:url.isformatted.like.this/database";

try {
    conn = DriverManager.getConnection(url, "username", "password");
} catch (SQLException e) {
    e.printStackTrace();
    System.exit(1);
}

(user/pass and database url removed for privacy sake)

The problem couldn't be credentials or the URL itself, as I use it to manually log in from the same box successfully using psql. I'm thinking it's probably the formatting of the URL, but I couldn't find any examples of psql being used on a remote address (they were all local host kinda things)

+2  A: 
atk
nope, I didnt! (I saw the // in the localhost kinda examples, but I thought it applied only to local databases) THANKYOU!
RyanCacophony
A: 

I just looked ad my code which connects to a PostgreSQL database, and it looks like this:

DriverManager.getConnection(String.format("jdbc:postgresql://%s/%s", server, dbName), userName, password);

Also, make sure you're loading the PostgreSQL database driver before trying to connect:

Class.forName("org.postgresql.Driver");

and that the PostgreSQL JDBC Jar library is on your classpath.

Jonathan