Spring with Jdbc java based configuration example

By | June 23, 2021

In this post, We will learn about Spring with Jdbc java based configuration example using a Demo Project.

Spring Framework provides easy integration with JDBC API and provides org.springframework.jdbc.core.JdbcTemplate utility class that we can use to avoid writing boiler-plate code from our database operations logic such as Opening/Closing Connection, ResultSet, PreparedStatement, etc.

Let’s have a look at a Spring JDBC example application and we will understand how the org.springframework.jdbc.core.JdbcTemplate class can help us in writing modular code with ease without worrying about resources are closed properly or not.

Now Let’s try to understand the Spring with Jdbc java-based configuration example using the below demo Project.


We need to add Spring and MySQL dependencies in Maven pom.xml file.


Employee model class to store employee data


Employee Service Interface


Employee Service Implementation class


Employee DAO(Data Access Object) Interface


Employee DAO(Data Access Object) Interface implementation class to perform CRUD operations with MySQL database.

Spring org.springframework.jdbc.core.JdbcTemplate is the central class in the Spring JDBC core package and it provides many methods to execute SQL queries and It automatically parse ResultSet to get the Object or list of Objects.


RowMapper interface allows us to map a row of the relations with the instance of the user-defined class. It usually iterates the ResultSet internally and adds it into the collection. So we don’t need to write a lot of boilerplate code to fetch the records as ResultSetExtractor.

This interface has only one method mapRow that accepts ResultSet instance and int as rowNum the number of the current row

T mapRow(ResultSet rs, int rowNum) throws SQLException;


This class is annotated with @Configuration annotation that means it holds bean configuration details like applicationContext.xml config file(This Java class is the replacement of Spring XML config file ). so all the Spring beans configuration moved from Spring XML config file to Java-based configuration.


Here we are going to use the below database table in the MySQL database.


This ClientTest class gets the bean from the applicationContext.xml file and calls the different methods of  EmployeeService to perform CRUD operation with MySQL database.

If you run ClientTest.java as Java Application then it will give the below output: 

MySQL Database Before and After running above Client Program:

Initial MySQL Database Screen Shot After Creating Table

MySQL Database Screen Shot After running ClientTest.java:

That’s all about Spring with Jdbc java based configuration example

You May Also Like:

Spring BeanFactory Container Example
Spring ApplicationContext Container Example
Annotation-based Configuration in Spring Framework Example
Spring Java-based Configuration Example
Spring Setter Dependency Injection Example
Spring @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring @Autowired Annotation With Constructor Injection Example
Spring Autowiring byName & byType Example
getBean() overloaded methods in Spring Framework
Spring Inner bean example
Spring Dependency Injection with Factory Method
Spring Framework @Qualifier example
Injecting Collections in Spring Framework Example
Spring Bean Definition Inheritance Example
Spring bean scopes with example
Spring JSR-250 Annotations with Example
Spring BeanPostProcessor Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *