Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Questions and answers begin here Logo Questions and answers begin here Logo
Sign InSign Up

Questions and answers begin here

Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Blog
  • Contact Us

SpringBoot: No qualifying bean of type javax.sql.DataSource.

Home/ Questions/Q 431
Next
Answered
SpringBoot: No qualifying bean of type javax.sql.DataSource.
kharlosofazeroth
kharlosofazeroth Begginer

I was trying to connect my Spring Boot application to a MySQL database using DataSource, but I kept getting a “No qualifying bean of type javax.sql.DataSource” error. I’ve followed various tutorials online, including this one: https://www.baeldung.com/spring-boot-checking-connection-to-database. However, I still can’t get my application to connect to the database. I have verified that the database is running and that the credentials are correct. Can you please tell me what I’m doing wrong?
Here’s what my application.properties file looks like:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.username=root
spring.datasource.password=password123

And here’s my DAO class:

@Repository
public class UserDaoImpl implements UserDao {

private final JdbcTemplate jdbcTemplate;
@Autowired
public UserDaoImpl(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List getAllUsers() {
String sql = "SELECT * FROM user";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
}
}

I’m really at a loss here, so any help you can provide is greatly appreciated. Thank you!

annotationsconfigurationdatasourcedependency-injectionjavajdbcspring-boot
  • 108
  • 0 Followers
  • 1
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

3 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    jokaduni Teacher
    2020-07-09T13:25:18+00:00Added an answer about 3 years ago

    Hi there! I understand that you are facing issues with the Spring Boot application that you are working on. From your question, it seems you are receiving an error stating “No qualifying bean of type javax.sql.DataSource found for dependency.”
    This error can occur when Spring is unable to find a bean of type javax.sql.DataSource in the application context. This usually happens when there is no configuration for the DataSource bean in the application. To resolve this issue, you can add the following code snippet to the configuration file:
    “`
    jaysbeanspam
    @ConfigurationProperties(prefix = “spring.datasource”)
    public DataSource dataSource() {
    return DataSourceBuilder.create().build();
    }
    “`
    This code will create the DataSource bean in the application context, and this bean can then be used across the application.
    Another possible solution to this problem is to verify that the necessary dependencies for the JDBC driver are added to the project. When working with Spring Boot, you can add these dependencies to the pom.xml file. For a MySQL database, you can add the following dependencies:
    “`

    mysql
    mysql-connector-java
    8.0.23

    “`
    If the above solutions do not work, you can also try to check if the application.properties file is properly configured. The configuration should include the database URL, username, and password. An example of a properly configured application.properties file could be:
    “`
    spring.datasource.url=jdbc:mysql://localhost:3306/dbname
    spring.datasource.username=username
    spring.datasource.password=password
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    “`
    Ensure that the property values are replaced with database-specific values.
    In conclusion, the error “No qualifying bean of type javax.sql.DataSource found for dependency” occurs when Spring is unable to find a bean of type javax.sql.DataSource in the application context. To resolve this issue, you can add the necessary code snippet to create the DataSource bean in the configuration file, check if the necessary dependencies are added, and ensure that the application.properties file is properly configured. I hope this helps you resolve your issue. If you have any further questions or concerns, feel free to ask.

    • 126
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. sayaizhan65 Begginer
    2020-07-11T08:11:36+00:00Added an answer about 3 years ago

    One possible way to solve the issue of a ‘No qualifying bean of type javax.sql.DataSource’ error in Spring Boot is to make sure that the necessary dependencies are included in the project. Check the pom.xml file for the following dependencies:
    “`

    org.springframework.boot
    spring-boot-starter-web


    org.springframework.boot
    spring-boot-starter-jdbc


    com.h2database
    h2
    runtime

    “`
    If they are not present, add them and run the project again. This should resolve the issue.
    Another possible solution is to add the @ComponentScan annotation to your main class, and specify the package where the DataSource class is located. For example:
    “`
    @SpringBootApplication
    @ComponentScan(basePackages = “com.example.project.datasource”)
    public class MyApplication {
    public static void main(String[] args) {
    SpringApplication.run(MyApplication.class, args);
    }
    }
    “`
    This will make sure that Spring scans for components in the specified package, and should locate your DataSource bean.
    I hope this helps resolve your issue. Let me know if you have any further questions or concerns.

    • 37
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  3. zaczang Teacher
    2020-07-13T20:57:07+00:00Added an answer about 3 years ago

    To solve the issue of “No qualifying bean of type ‘javax.sql.DataSource’ found for dependency”, you need to ensure that you have added the necessary dependencies in your pom.xml file.
    Firstly, check to see if you have added the necessary dependencies for the JDBC driver you are using. For instance, if you are using MySQL, you should ensure that you have added the MySQL JDBC driver dependency like this:

    “`xml

    mysql
    mysql-connector-java
    8.0.23

    “`

    Additionally, you should add the `spring-boot-starter-jdbc` dependency:

    “`xml

    org.springframework.boot
    spring-boot-starter-jdbc

    “`

    These dependencies will ensure that the DataSource bean is available to your application.

    If adding these dependencies doesn’t resolve your issue, you may want to check to see if there are any conflicts with other dependencies, or if there is an issue with your application configuration.

    In my own experience, I had encountered the same issue and it was due to a mismatch of dependencies, so resolving the conflicts fixed the issue.

    • 6
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.