JDBC-postgres, connection refused

This is the first time I have used java to access databases, so I probably have a simple error, but when I go on to restore my connection from a remote database that I have access to, I get a connection failure.

Here is 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 url / password and database removed for privacy)

The problem cannot be the credentials or the URL itself, as I use it to automatically log in from the same field using psql. I think this is probably URL formatting, but I could not find examples of using psql on the remote address (they were all local hosts)

+5
3

http://www.petefreitag.com/articles/jdbc_urls/ URL-

jdbc:postgresql:database
jdbc:postgresql://host/database
jdbc:postgresql://host:port/database
jdbc:postgresql://host:port/database?user=userName&password=pass
jdbc:postgresql://host:port/database?charSet=LATIN1&compatible=7.2

// ?

+12

@Jonathan , jar PostgreSQL JDBC classpath: :

private static final String TABLE_NAME = "tablenamegoeshere";
private static final String DRIVER = "org.postgresql.Driver";
private static final String URL = "jdbc:postgresql://url.for.database/DatabaseName";
private static final String USERNAME = "yourusername";
private static final String PASSWORD = "yourpassword";


private static Connection getConnection() throws Exception {
  Class.forName(DRIVER);
  Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
  return conn;
}
+1

, PostgreSQL, :

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

, , PostgreSQL, :

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

JgBC PostgreSQL JDBC .

0

All Articles