Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No suitable driver for postgres, even though Class.forName works?

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...

like image 902
corsiKa Avatar asked Oct 26 '15 16:10

corsiKa


1 Answers

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

like image 104
Andy Gaskell Avatar answered Oct 15 '22 17:10

Andy Gaskell