EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot DataSource
 

Spring Boot DataSource

Updated April 1, 2023

Spring Boot DataSource

 

 

Introduction to Spring Boot DataSource

In Spring boot, we have a datasource which helps us to connect where the data is kept. Datasource helps us to identify the database; in short, it is an identifier we can say which helps us to identify the source from where the data will come and in the context of any application or programming language, data always come from a database, so datasource help us to register and identify the source of data for our application.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

In spring boot, we do not need to make extra configurations for datasource because that can be done in either two ways, one by properties file and another one using the explicit configuration with a custom class. Here we will see how we can register a datasource by using both approaches for better understanding and implementation.

Syntax of Spring Boot DataSource

As we already know that we can register a data source object using an application property file; let’s look at the syntax of how we can do this in spring boot.

spring.datasource.driverClassName = your driver class name
spring.datasource.url = url for your database
spring.datasource.username = your username
spring.datasource.password = your password

As you can see in the above line of syntax, we are doing some configuration which needs to be go inside the application.properties file. Also, we have to mention the correct parameter here, which are specific to our database.

Here we will see how we can do configuration using properties file and custom class in java to make it work like normal.

How Spring Boot DataSource Works?

As we already know that spring boot we are not supposed to do a lot of configuration when it comes to datasource because the spring boot framework autoconfigures it. We only have to make a few changes to the application property file to make this work.

We will first see how we can do this using the application file; then, we will see a custom class to configure a datasource.

By using application file: In this approach, we do not have to make many configurations because it will manage by the spring boot frame work, so what we have to do is only mention the required property of the database, which will be needed in order to connect.

We will first see the different properties and discuss each of them in order to write the data source for our application.

a. application.properties file: Inside this file, we will make configurations regarding the name, pass, url and other things which will help the spring boot application to identify our data source using this property. We have to mention everything correctly; otherwise, we will get a runtime error; the database username, password, and other things need to be accurate; otherwise, we will end up with a runtime exception.

Properties:

1. spring.datasource.driverClassName

Using this property, we can specify the name of the driverClass, which will help spring boot identify which database we are using for our current project. You can use any of the available databases we have. But remember, this driverClassName should be correct, and the spring boot is able to find it; otherwise, an exception will be thrown.

Output Error:

Spring Boot Datasource 1

2. spring.datasource.url

This property contains the information about the database, like what is the name of the database, on which por it is running etc.

If the database does not exist, then this error:

Output:

.url

3. spring.datasource.username

This property allows access to the spring boot application to access our database with the mentioned username. So this user should be already created and exist on the database side. If the spring boot is not able to find it, then again, we will have the runtime exception.

If the username is wrong, then the following error will appear.

Output:

.username

4. spring.datasource.por

This property also allows access for spring boot application to a database if we have any password set for the specific database; also, if we do not provide this property, then we will have so many exceptions at runtime.

Below error of the password is incorrect entered:

Output:

Spring Boot Datasource 4

For both the property that we have mentioned above, username and password, there is not always the possibility that we have authorization on the database side. Sometimes it may happen that we have not protected our database with any credential. so we do not require to mention this property inside the file; it should be blank; we cannot directly remove the whole.

To make this work, we have to add some more things to our project else it will give us classnotfound or other exceptions at runtime; let’s take a look at the required dependency we need to add in order to properly configure the data source using the application file in paring boot project.

1. If we are using MYSQL, then we have to add the dependency to make spring boot able to find the classes at runtime.

For this, we have MySQL dependency as below:

Example:

Code:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

2. If you are using the h2 database, you may require this dependency below in our build file.

Example:

Code:

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.4.1</version>
<scope>runtime</scope>
</dependency>

3. For different databases, we have different driver classes available, which should be present in the library; else, it will not work just by mentioning the above properties inside the file; at compile-time, everything will be fine, but at runtime, it will throw an exception.

Conclusion

By the use of data source, we can connect to our database and fetch the data we need in our application; also, this is a very important part of the project because we need to save the data of the user somewhere, and that is a database only, which can only be done using the data source in spring boot.

Recommended Article

This is a guide to Spring Boot DataSource. Here we discuss the introduction and how spring boot DataSource works? respectively. You may also have a look at the following articles to learn more –

  1. Maven Repository Spring
  2. Spring Boot DevTools
  3. Spring AOP
  4. Spring Cloud Components

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW