I am new to Spring and Spring Boot. I am trying to connect to a Postgresql database in my Spring Boot app. I've tried to take inspiration from
http://spring.io/guides/gs/accessing-data-mysql/
and
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html
Basically I have an application.properties file that contains:
app.datasource.url=jdbc:postgresql://localhost:5432/db_example
app.datasource.username=myusername
app.datasource.password=mypassword
app.datasource.driver-class-name=org.postgresql.Driver
Using the psql tool I can normally connect to the db_example database on localhost with the 5432 port and those username and password.
Then I have a MyConfig.java which contains:
package hello;
import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
public class MyConfig {
@Bean
@Primary
@ConfigurationProperties(prefix = "app.datasource")
public DataSource dataSource() {
DataSourceBuilder dsb = DataSourceBuilder.create();
if (dsb == null) {
return null;
}
return dsb.build();
}
}
Then in MainController.java one of the methods that has a @GetMapping annotation, I call the dataSource() method. On the returned object I call getConnection() but that throws an SQLException with message The url cannot be null.
Also in the console the Spring Boot app writes several things:
Not loading a JDBC driver as driverClassName property is null.
and
Unable to create initial connections of pool
What am I missing? I guess some annotation in MyConfig.java?
EDIT: You might also want to see my pom.xml used to package the thing with mvn:
<?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>org.springframework</groupId>
<artifactId>gs-mysql-data</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>-->
<!-- Use MySQL Connector-J -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
driver-class-nameshould bedriverClassNamein your application.properties