1

I'm trying to connect a PostgreSQL database to my Spring application but I keep getting errors that are linked with my app not being able to find the org.postgresql.Driver class.

Here's the pom.xml as I proof I'm getting the jar from the dependency:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>slupov</groupId>
    <artifactId>slupov-personal</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <build>

        <finalName>slupov-personal</finalName>

        <plugins>
           ...
        </plugins>
    </build>

<!--    Spring Boot dependency-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
    </parent>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/postgresql/postgresql -->
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901-1.jdbc4</version>
        </dependency>

     ...

    </dependencies>

</project>

I can confirm the downloaded jar contains the org.postgresql.Driver.

Now when I try to check whether the class is in my classpath I get an exception showing it is not there:

try {
    Class.forName("org.postgresql.Driver");
    //on classpath
} catch(ClassNotFoundException e) {
    // breaks here -> not on classpath
    System.out.println("Not");
}

Here's my application.properties for the Hibernate connection:

spring.datasource.driver-class-name=org.postgresql.Driver

spring.datasource.connection.url=jdbc:postgresql://localhost:5432/SlupovPersonal?useSSL=false
spring.datasource.connection.username=postgres
spring.datasource.connection.password=Sigma255!
spring.datasource.dialect=org.hibernate.dialect.PostgreSQLDialect9.2
spring.datasource.show_sql=true
spring.datasource.current_session_context_class=thread
spring.datasource.hbm2ddl.auto=create

spring.datasource.hibernate.dbcp.initialSize=5
spring.datasource.hibernate.dbcp.maxTotal=20
spring.datasource.hibernate.dbcp.maxIdle=10
spring.datasource.hibernate.dbcp.minIdle=5
spring.datasource.hibernate.dbcp.maxWaitMillis=-1

The How do I fix this so my app uses the database through Hibernate?

3
  • I used spring.datasource.driverClassName=org.postgresql.Driver and that worked. Can you please try with that? Commented Oct 30, 2019 at 20:50
  • @zain does not work for me Commented Oct 30, 2019 at 21:30
  • remove the version of jar <version>9.1-901-1.jdbc4</version>, let spring boot manage the version, also I suspect the version you mentioned, exists here jdbc.postgresql.org/download.html#current Commented Oct 31, 2019 at 4:30

1 Answer 1

2

I modified your application.properties to work

spring.datasource.driver-class-name=org.postgresql.Driver

spring.datasource.url=jdbc:postgresql://localhost:5432/SlupovPersonal?useSSL=false
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.properties.show_sql=true
spring.jpa.properties.current_session_context_class=thread
spring.jpa.properties.hbm2ddl.auto=create

spring.datasource.dbcp2.initial-size=5
spring.datasource.dbcp2.maxTotal=20
spring.datasource.dbcp2.maxIdle=10
spring.datasource.dbcp2.minIdle=5
spring.datasource.dbcp2.maxWaitMillis=-1

Also we need to change the version of postgressql to a later version as suggested in the second comment or you can remove the version so that it takes the spring boot managed version.

    <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
    </dependency>
Sign up to request clarification or add additional context in comments.

3 Comments

I believe that combined with some run command alterations fixed the issue although i've got a new error now. It's not a part of the question so i mark this as an answer. The exception is java.sql.SQLFeatureNotSupportedException: Method org.postgresql.jdbc4.Jdbc4Connection.isValid(int) is not yet implemented. if anyone is wondering or decides to help out.
Sorry but did you change the version of postgresql driver? I think the mentioned error comes with old version which you had been using.
yes, I did. I'm using the newest from the maven repository now. Still no idea why this would happen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.