我需要在一个Jhipster项目中做一个新的数据库连接,我正在做下一个
在application-dev.yml中添加:
datasource:
driver-class-name: org.postgresql.ds.PGSimpleDataSource
url: jdbc:postgresql://localhost:5432/analytics
name: analytics
username: elser
password: ******
datasources:
elser:
driver-class-name: org.hibernate.dialect.PostgreSQLDialect
url: jdbc:postgresql://localhost:5432/elser
name: elser
username: elser
password: ******
在 DatabaseConfiguration.java 中:
@Bean
@ConfigurationProperties(prefix="spring.datasources.elser")
public DataSource dataSourceElser() {
return DataSourceBuilder.create().build();
}
我添加了一个新类来测试这个:
@Inject
@Qualifier("dataSourceElser")
private DataSource dataSourceElser;
private JdbcTemplate jdbcTemplate;
@PostConstruct
public void init() {
jdbcTemplate = new JdbcTemplate(dataSourceElser);
int rowCount = this.jdbcTemplate.queryForObject("select count(*) from commons.usuario", Integer.class);
System.out.println(rowCount);
}
但它给了我下一个错误:
java.sql.SQLException: org.hibernate.dialect.PostgreSQLDialect cannot be cast to java.sql.Driver
最佳答案
您当前指定的是 hibernate 方言,而不是 JDBC 驱动程序名称。您需要使用:
driver-class-name: org.postgresql.Driver
而不是driver-class-name: org.postgresql.ds.PGSimpleDataSource
和 driver-class-name: org.hibernate.dialect.PostgreSQLDialect
关于java - 在 Jhipster 中添加到数据库的新连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35334041/