【发布时间】: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