4

I'm trying to following this guide to create a bean that gives me a DataSource Object, but when I try to access the Datasource, for example this way:

Connection connection  = datasource.getConnection();
            Statement stmt=connection.createStatement();  
            ResultSet rs=stmt.executeQuery("select * from products");   

I get this error:

 HikariPool-1 - dataSource or dataSourceClassName or jdbcUrl is required.

I edited my code many times, since I read various examples, that are always slightly different.

This is the last version of my code

@Configuration
@ComponentScan("com.packagename.webstore")
public class RootApplicationContextConfig {

    @Bean 
    @ConfigurationProperties(prefix = "spring.datasource")
    public HikariDataSource  dataSource() {     
        return DataSourceBuilder.create().type(HikariDataSource.class).build();

    } 
}

This is the application.properties file inside src/main/resources folder:

spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=password
spring.datasource.driverClassName=com.mysql.jdbc.Driver

This are my dependencies:

enter image description here

Does anybody understand what is my error?? Thank you

MDP
  • 4,177
  • 21
  • 63
  • 119
  • i think it shud be like this `spring.datasource.driver-class-name` – pvpkiran Aug 29 '18 at 13:53
  • I already tried in a previous attempt, but I got the same error :( – MDP Aug 29 '18 at 13:59
  • `spring.datasource.url` instead of `spring.datasource.jdbc-url`. You don't need the `spring.datasource.driverClassName`. – M. Deinum Aug 29 '18 at 14:00
  • Also you are using Spring Boot so why configure the datasource manually? You don't need the datasource bean as Spring Boot already configures one for you. The same applies to the `@ComponentScan`. So basically you should be able to remove your `RootApplicationContextConfig` and have the same (or better result). – M. Deinum Aug 29 '18 at 14:07
  • I think I'm making a lot of confusion. Actually I'm using a Spring mvc since I'm Building a web application. That's why I have RootApplicationContextConfig . But I dont' understand how to create bean that gives me a DataSource object – MDP Aug 29 '18 at 14:13
  • See https://stackoverflow.com/questions/49088847/after-spring-boot-2-0-migration-jdbcurl-is-required-with-driverclassname – Ori Marko Aug 30 '18 at 05:55

1 Answers1

3

This should work for you.

# DataSource settings: Database configurations
spring.datasource.url = jdbc:mysql://localhost:3306/db_example
spring.datasource.username = springuser
spring.datasource.password = password

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

Have you try the Spring Data Repositories? If you use the Spring Data Repositories, you don't have to specify a Datasource object. If you want to implement repositories, you can follow this example:

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductRepository extends CrudRepository<Product, Long> {

}

To call the service on a MVC @Service.

@Service
public class ProductService {

  @Autowired
  ProductRepository productRepository;

  public List<Product> findAll() {
    List<Product> results = null;

    results = productRepository.findAll();

    return results;

  }
}