postgresql - Spring Boot postgres 无法确定合适的驱动程序类

标签 postgresql gradle jdbc

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

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

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'
}

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

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

最佳答案

通过创建两个 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;
    }

关于postgresql - Spring Boot postgres 无法确定合适的驱动程序类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51403991/

相关文章:

sql - 在 SQL 查询中将用户相互关联

android - Android Studio Gradle下载,无进度更新

android - 'build' 中的 'main' 不能应用于 '(groovy.lang.closure'

java - 使用jdbc向数据库中插入数据

java - 找不到适用于 jdbc :mysql://localhost:3306/egov 的合适驱动程序

postgresql - PostgreSQL 存储对 char 和 varchar 使用 2^n-1

php - PDO 语句错误 : 42601 syntax error near "$6"

具有特定键的 Django ManyToMany 反向查询

android - 缺少对测试类中测试库的引用

java - 无法连接到 heroku PostgreSQL 数据库