Share
Go down
avatar
Admin
Posts : 155
Join date : 2017-11-11
Age : 27
View user profilehttp://net-media.yoo7.com

Cannot load configuration class in Spring with Oracle db connection

on Tue Dec 26, 2017 8:19 pm
I am trying to connect to an existing table in an oracle database. I have set up a oracle connection inside application.properties
Code:
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect

spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
spring.datasource.username=talon
spring.datasource.password=talon

spring.datasource.tomcat.test-while-idle=true
spring.datasource.dbcp2.validation-query=SELECT 1

spring.jpa.show-sql=true
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
I have a configuration class
Code:
@Configuration
public class Db {

    @NotNull
    private String username;
    @NotNull
    private String password;
    @NotNull
    private String url;

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    private Db() {
        OracleDataSource dataSource = null;
        try {
            dataSource = new OracleDataSource();
            dataSource.setUser(username);
            dataSource.setUser(password);
            dataSource.setUser(url);
            dataSource.setImplicitCachingEnabled(true);
            dataSource.setFastConnectionFailoverEnabled(true);
            Connection connection = dataSource.getConnection();
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("select * from BOOK");
            rs.next();
            System.out.println(rs.getString(1));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
Answer
Spring is trying to instantiate your configuration class Db using the default constructor.

Change this private Db() by this public Db() {
Back to top
Permissions in this forum:
You can reply to topics in this forum