Spring JDBC Integration Example

By | June 23, 2021

In this post, We will learn about Spring JDBC Integration 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.


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.


Employee row mapper class

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;


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


The org.springframework.jdbc.datasource.DriverManagerDataSource class is mainly used to contain information about the database such as driver class name, connection URL, username, and password.

There is a property Spring bean with the name as datasource in the org.springframework.jdbc.core.JdbcTemplate class of org.springframework.jdbc.datasource.DriverManagerDataSource type. So, we need to provide the reference of the DriverManagerDataSource object in the JdbcTemplate class for the data source property.

Here, we are using the JdbcTemplate object in the EmployeeDaoImpl class, so we are passing it by the setter injection but you can also use constructor injection.


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 JDBC Integration 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 Annotation Example
Spring with Jdbc java based configuration 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 *