0

I am trying to connect to an existing table in an oracle database. I have set up a oracle connection inside application.properties

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

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

}

When i run it I have the error java.lang.IllegalStateException: Cannot load configuration class: oraclejpamaven.oraclejpamaven.Db caused by java.lang.IllegalArgumentException: No visible constructors in class oraclejpamaven.oraclejpamaven.Db

MrSir
  • 576
  • 2
  • 11
  • 29

1 Answers1

1

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

Change this private Db() by this public Db() {

Hope this helps!

Ele
  • 33,468
  • 7
  • 37
  • 75
  • yes, I know i should make another question but i now have **java.sql.SQLException: Invalid Oracle URL specified: OracleDataSource.makeURL** when i call `dataSource.getConnection();` , the url is correct, i checked [this](https://stackoverflow.com/questions/1332869/invalid-oracle-url-specified-oracledatasource-makeurl) but it didn't help. – MrSir Dec 26 '17 at 17:55
  • You're not setting the URL – Ele Dec 26 '17 at 18:00