【问题标题】:How to set driver for postgres? [duplicate]如何为 postgres 设置驱动程序? [复制]
【发布时间】:2020-05-19 00:30:27
【问题描述】:

我有 Spring Boot 应用程序,我想在其中测试批量插入到不同的数据库。我有 pom 文件:

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.8</version>
    </dependency>

控制器中的代码:

@RequestMapping(value = "setdb/{url}/{schema}/{login}/{password}")
public String setDB(@PathVariable(name = "url") String url,
                    @PathVariable(name = "schema") String schema,
                    @PathVariable(name = "login") String login,
                    @PathVariable(name = "password") String password) throws SQLException, ClassNotFoundException {
    url = "jdbc:postgresql://"+url+"?currentSchema="+schema;
    connection = DriverManager.getConnection(url, login, password);
    return String.format("url: %s\nlogin: %s\npassword: %s", url, login, password);
}

当我尝试这样做时,我遇到了错误

{
    "timestamp": "2020-02-03T09:06:46.800+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "No suitable driver found for jdbc:postgresql://127.0.0.1:5432?currentSchema=app",
    "path": "/setdb/127.0.0.1:5432/app/postgres/qwerty"
}

我该如何解决?以编程方式设置驱动程序?

【问题讨论】:

  • 尝试先加载 postgress 驱动程序? Class.forName("org.postgresql.Driver");
  • @Worthless 多年来没有必要。
  • 很可能由于某种原因未包含驱动程序 jar。生成战争并检查 jar 是否包含在库中。
  • app 是模式还是数据库?如果它真的是一个模式,那你为什么不在 URL 中提供一个数据库名称呢?
  • 你不应该在 Spring Boot 中使用DriverManager.getConnection,而应该使用数据源。见How-to Guide: Data Access

标签: java spring postgresql spring-boot jdbc


【解决方案1】:

如果您确定驱动程序在您的类路径中,则错误也可能源于错误的连接 URL。 检查您的 URL 语法,它应该是

  • jdbc:postgresql:数据库
  • jdbc:postgresql://host/database
  • jdbc:postgresql://host:port/database

所以如果 app 是你应该做的数据库:

jdbc:postgresql://127.0.0.1:5432/app

如果应用程序是数据库中的架构,您仍然需要在 URL 中指定您的数据库

jdbc:postgresql://127.0.0.1:5432/DBNAME?currentSchema=app

【讨论】:

    【解决方案2】:

    我所做的测试是创建了一个空的 mvn 项目

    spring init --name=postgres-demo --dependencies=web,jpa,postgresql 
    postgres-test
    

    并且在资源下的应用程序属性中添加了这个&它对我有用:

    ## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
    
    spring.datasource.url=jdbc:postgresql://localhost:5432/postgres_test
    spring.datasource.username= rajtest
    spring.datasource.password=
    
    # dialect makes Hibernate generate better SQL for the database
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
    
    # Hibernate ddl auto (create, create-drop, validate, update)
    spring.jpa.hibernate.ddl-auto = update
    
    

    【讨论】:

    • OP 已经将 JDBC 驱动程序作为依赖项,并且给出的代码没有使用数据源。
    • 所以基本上不是在控制器中执行它,而是在应用程序属性中添加它并执行它。
    • 你错过了我的意思,OP目前不是DriverManager.getConnection,所以你展示的所有这些配置都不能解决他的问题,除非你也解释他需要在他的代码中改变什么使用该配置。
    • @MarkRotteveel 知道了
    【解决方案3】:

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

    在您的 URL 中,您需要添加主机和数据库名称

    url = "jdbc:postgresql://"+url+"?currentSchema="+schema;

    url = "jdbc:postgresql://"+url+"/"+schema使用这个url

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 2021-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多