postgresql Springboot postgres 无法确定合适的驱动程序类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51403991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:29:10  来源:igfitidea点击:

Springboot postgres Failed to determine a suitable driver class

postgresqlgradlejdbc

提问by ashu

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.

我正在尝试使用 SpringBoot 和 Postgres 数据库开发 Web 应用程序。但是,在连接到应用程序时,我收到错误消息“无法确定合适的驱动程序类” 根据旧帖子中的建议,我尝试使用不同版本的 jdbc 驱动程序,并尝试手动为 NamedParameterJdbcTemplate 创建 bean。我还验证了库存在并且可以从 Java 代码访问,而那些存在于类路径中。但它仍然给出同样的问题。我正在使用 gradle 将所有 jars 导入构建路径。

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

这是代码的 git 存储库:https: //github.com/ashubisht/sample-sbs.git

Gradle dependency code:

Gradle依赖代码:

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

构建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

这是 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

采纳答案by ashu

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

该问题已通过创建两个 bean 解决。为 DataSource 和 NamedParameterJdbcTemplate 创建单独的 bean。

    @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;
    }

回答by Caleb Santos

After taking a time, I realized that I've created the Datasource class outside the application package. Check this may help.

花点时间后,我意识到我已经在应用程序包之外创建了 Datasource 类。检查这可能会有所帮助。