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