postgresqlgradlejdbc

Springboot postgres Failed to determine a suitable driver class


I am trying to develop web application using SpringBoot and Postgres Database. However, on connecting to the application, I am getting error "Failed to determine a suitable driver class" As per advise in older posts, I have tried using driver of different version of jdbc and also tried creating bean for NamedParameterJdbcTemplate manually. I also validated that libraries are present and is accessible from Java code and those are present in classpath. But its still giving the same issue. I am using gradle to import all jars into build path.

Here is the git repository for the code: https://github.com/ashubisht/sample-sbs.git

Gradle dependency code:

apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-websocket")
    compile("org.springframework.boot:spring-boot-starter-jdbc")
    //compile("org.postgresql:postgresql")
    compile("org.postgresql:postgresql:9.4-1206-jdbc42")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

Code for building Bean

@Configuration
@PropertySource("classpath:application.properties")
public class Datasource {

    @Value("${db.driverClassName}")
    private String driverClass;
    @Value("${db.url}")
    private String url;
    @Value("${db.username}")
    private String username;
    @Value("${db.password}")
    private String password;

    @Bean
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate() throws Exception{
        System.out.println(driverClass+" "+ url+" "+username+" "+password);
        DriverManagerDataSource source = new DriverManagerDataSource();
        source.setDriverClassName(driverClass);
        source.setUrl(url);
        source.setUsername(username);
        source.setPassword(password);
        NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(source);
        return namedParameterJdbcTemplate;
    }
}

Here is application.properties

server.port=8086

#spring.datasource.driverClassName=org.postgresql.Driver
#spring.datasource.url= jdbc:postgresql://localhost:5432/testdb
#spring.datasource.username=postgres
#spring.datasource.password=password
#spring.datasource.platform=postgresql
#spring.jpa.hibernate.ddl-auto=create-drop

db.driverClassName=org.postgresql.Driver
db.url=jdbc:postgresql://localhost:5432/testdb
db.username=postgres
db.password=password

Solution

  • The issue is resolved by creating two beans. Separate bean is created for DataSource and NamedParameterJdbcTemplate.

        @Bean
        public DataSource dataSource(){
            System.out.println(driverClass+" "+ url+" "+username+" "+password);
            DriverManagerDataSource source = new DriverManagerDataSource();
            source.setDriverClassName(driverClass);
            source.setUrl(url);
            source.setUsername(username);
            source.setPassword(password);
            return source;
        }
    
        @Bean
        public NamedParameterJdbcTemplate namedParameterJdbcTemplate(){
            NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(this.dataSource());
            return namedParameterJdbcTemplate;
        }