Question

I want to connect to a PostgreSQL database using the JDBC 4.1 driver. I declared the following dependency in the pom.xml:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.3-1101-jdbc41</version>
</dependency>

This puts postgresql-9.3-1101-jdbc41.jar into my classpath. I have read that it not necessary anymore to load the driver using Class.forName() if the driver is specified at META-INF/services/java.sql.Driver. The driver-jar has this file but I still get the following error:

No suitable driver found for jdbc:postgresql://localhost:5432

I just make a call to in a test and then run the tests using mvn test:

DriverManager.getConnection("jdbc:postgresql://localhost:5432");

Even if I call Class.forName() before I get the error. How do I get the driver loaded properly?

Was it helpful?

Solution

Your connection URL is invalid. You must specify a database, otherwise the driver will not accept the URL.

The correct URL would be:

jdbc:postgresql://localhost:5432/database_name

Or as you are using the default port:

jdbc:postgresql://localhost/database_name

The message "No suitable driver found" is a message generated by the DriverManager which asks all registered drivers if they accept the given URL. The first driver to say "yes" will then be used to open the connection. The Postgres driver doesn't accept the URL because the database is missing and as no other drivers are around to ask, the DriverManager "gives up" and presents you with that error message.

If the class wasn't available you'd get a ClassNotFoundException not a "message" from the DriverManager.

OTHER TIPS

This looks like some issue with Maven setting up the classpath correctly. Take a look at this , sounds like a similar issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top