5
votes

I'm attempting to use the following Driver to connect to my postgresql database:

<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>9.4-1204-jdbc41</version>
</dependency>

I'm using the following code:

Class.forName("org.postgresql.Driver");
System.out.println("Driver version: " + org.postgresql.Driver.getVersion());

String jdbcUrl = "jdbc:postgresql://localhost:5432";
String user = "postgres";
String pass = "password"; // super secure

Connection c = DriverManager.getConnection(jdbcUrl, user, pass);

And I get the following output

Driver version: PostgreSQL 9.4 JDBC4.1 (build 1204)
java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432
       at java.sql.DriverManager.getConnection(DriverManager.java:689)
       at java.sql.DriverManager.getConnection(DriverManager.java:247)
       at [ the line Connection c = DriverManager... ]

So how is it that this 9.4 driver is not suitable? I know I can log in via my psql command line, but I can't seem to get it to work through Java.

I've done apps like this many times, but can't seem to see what I'm missing here...

1
Try appending a databasename to the url: jdbc:postgresql://localhost:5432/dbname. Do you have other JDBC drivers on the classpath? Some actually intercept the way the DriverManager searches for the correct driver.a_horse_with_no_name
wow... I would have expected it to simply use the default database... Apparently I must be explicit... If you would like to create an answer, I'll be happy to give you credit for that.corsiKa
Well, according to the manual, the database name is optional: jdbc.postgresql.org/documentation/94/connect.htmla_horse_with_no_name
Any chance you are trying running your app with Java 1.6?Gyro Gearless
Could you try jdbc:postgresql://localhost:5432/ (note the trailing /), instead of jdbc:postgresql://localhost:5432?Mark Rotteveel

1 Answers

8
votes

All credit to a_horse_with_no_name's comment, but this question needs an answer.

Either specify a database in the connection string or add a trailing slash.

Bad url:

jdbc:postgresql://localhost:5432

Good:

jdbc:postgresql://localhost:5432/

or

jdbc:postgresql://localhost:5432/thebestdatabase