【问题标题】:Springboot postgres Failed to determine a suitable driver classSpringboot postgres 无法确定合适的驱动类
【发布时间】:2018-12-26 11:46:02
【问题描述】:

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

这里是代码的 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

【问题讨论】:

    标签: postgresql gradle jdbc


    【解决方案1】:

    遇到了同样的问题。 我的解决方案是将 application.properties 文件扩展名更改为 application.yml

    【讨论】:

      【解决方案2】:

      对我来说,问题在于 postgresSql 的拼写错误

      只有一个,

      替换

      1. spring.datasource.url=jdbc:postgres://localhost:5432/databaseName
      2. spring.datasource.url=jdbc:postgressql://localhost:5432/databaseName


      spring.datasource.url=jdbc:postgresql://localhost:5432/databaseName

      在hibernate方言上也检查同样的事情,

      替换PostgresSQLDialect PostgreSQLDialect

      【讨论】:

        【解决方案3】:

        对我来说,错误是

        未能配置数据源:未指定“url”属性并且无法配置嵌入式数据源。 原因:无法确定合适的驱动类

        行动: 考虑以下: 如果您想要一个嵌入式数据库(H2、HSQL 或 Derby),请输入 它在类路径上。 如果您有要从特定配置文件加载的数据库设置,您可能需要激活它(当前没有激活的配置文件)。

        问题是缺少配置文件 所以我在类路径中添加了以下内容并且它起作用了

        spring.profiles.active=dev

        【讨论】:

          【解决方案4】:

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

          【讨论】:

          • 我们应该在哪里创建这些函数?
          • 你需要在源码包里面创建DataSource.java,供Springboot扫描和挑选
          猜你喜欢
          • 2023-02-23
          • 2019-03-18
          • 2020-10-23
          • 2019-05-27
          • 2022-01-19
          • 2021-11-22
          • 2021-09-12
          • 2019-12-06
          • 2019-05-10
          相关资源
          最近更新 更多