0

I'm trying to get some Java code to connect to Postgres. Just an attempt to get it to connect to a database at all, in the first instance:

import java.sql.DriverManager;
import java.sql.SQLException;

public class Main {
  private static final String url = "jdbc:postgresql://localhost/dvdrental";
  private static final String user = "postgres";
  private static final String password = "...";

  public static void main(String[] args) throws SQLException {
    var conn = DriverManager.getConnection(url, user, password);
    System.out.println("Connected to the PostgreSQL server successfully.");
  }
}

This gets a runtime error:

Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost/dvdrental
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:708)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:230)

Seems straightforward enough, I need to add a suitable driver as a dependency, so I added the following to pom.xml:

<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>42.6.0</version>
</dependency>

... and it makes no difference at all; mvn package followed by running the program, still gets the same runtime error.

Is there something else I need to do, to tell Java to actually go ahead and use that driver?

rwallace
  • 31,405
  • 40
  • 123
  • 242

0 Answers0