Jose
Jose

Reputation: 1829

Add new conection to database in Jhipster

I need to do a new database connection in a Jhipster project, I am doing the next

Add in 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: ******

And in DatabaseConfiguration.java:

@Bean
@ConfigurationProperties(prefix="spring.datasources.elser")
public DataSource dataSourceElser() {
    return DataSourceBuilder.create().build();
}

I added a new class to test this:

@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);
}

But its giving me the next error:

 java.sql.SQLException: org.hibernate.dialect.PostgreSQLDialect cannot be cast to java.sql.Driver

Upvotes: 3

Views: 2002

Answers (1)

Mark Rotteveel
Mark Rotteveel

Reputation: 109265

You are currently specifying a hibernate dialect, not the JDBC driver name. You need to use:

driver-class-name: org.postgresql.Driver

Instead of both driver-class-name: org.postgresql.ds.PGSimpleDataSource and driver-class-name: org.hibernate.dialect.PostgreSQLDialect

Upvotes: 1

Related Questions