springboot-how to solve 'Failed to configure a DataSource' in SpringBoot apps

Problem

When we develop spring boot jdbc applications , sometimes, we got this problem:


> Task :app3:bootRun FAILED

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.2.RELEASE)

2020-11-21 16:14:00.386  INFO 62255 --- [           main] com.bswen.app3.Main                      : Starting Main on MBP-bswen with PID 62255 (/Users/bswen/bswen-springboot23/app3/build/classes/java/main started by bswen in /Users/bswen/bswen-springboot23/app3)
2020-11-21 16:14:00.389  INFO 62255 --- [           main] com.bswen.app3.Main                      : No active profile set, falling back to default profiles: default
2020-11-21 16:14:00.869  INFO 62255 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode.
2020-11-21 16:14:00.888  INFO 62255 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 14ms. Found 0 JDBC repository interfaces.
2020-11-21 16:14:01.077  WARN 62255 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2020-11-21 16:14:01.083  INFO 62255 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-11-21 16:14:01.086 ERROR 62255 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).


Execution failed for task ':app3:bootRun'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

Environment

  • mysql 5.1
  • spring boot 2.3

Reason

You must specify the jdbc url/username/password for the spring boot application to connect.

Solution

Create a file named application.properties in src/main/resources directory. Add the following lines:

spring.datasource.url=jdbc:mysql://your_mysql_ip:3306/bswen1
spring.datasource.username=bswen
spring.datasource.password=123456

You should change the ip address / username / password to match your settings.

All the code is upload to github, you can download the example code here.