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?
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.