:::: MENU ::::
  • Jul 06 / 2014
  • 1

Spring MVC With JDBC Template

In this particular blog we will discuss about Spring Jdbc template and its features. We will cover all crud operations here with a Spring MVC application.

Why JDBC Template

Before we start coding lets take a brief introduction to why do we need to adopt spring jdbc template instead of traditional JDBC programming or ORM like hibernate ?. The answer to this question is that to deal with traditional jdbc programming is a pain for developers as we need to code more and manage a number of headache regarding connection management and writing same code again and again. On the other hand ORM tools are good but we needs to deal with a heavy part of configuration, we also need to add a number of external jars to code in orms. JDBC Template on the other hand provides a new and efficient mechanism to deal with database, we dont need to bother about any heavy xml configurations like orm’s do or adding same connection code again and again as in traditional jdbc programming. In Jdbc template we put the connection thing in one place and dont even need to add external jar files. Instead of this JDBC template cleans up the resources automatically by releasing the db connection. Jdbc template also provides a better error detection mechanism by splitting the JDBC SQLExceptions into RuntimeExceptions. So here is a sample application to deal with it, atleast give it a chance to make your work easier and more efficient.

Project Structure

Before we start lets take a look at overall project structure for today’s discussion.

Spring MVC With JDBC Template Structure

Libraries Used

Here is the complete list of jar files needed to start working with JDBC template in a Spring MVC application.

Spring MVC With JDBC Template jars


This is the sample database that we have used to get a connection and create a table, execute the code below in your MySql query editor.

Spring JDBC Template

To start with spring jdbc template all you need to do is injecting ther datasource bean to dao class and everything will be done to use on the fly.




This class represents actual coding to deal with jdbc template, we have autowired datasource bean here and passes it to get an object of JdbcTemplate class on. Now we will call appropriate methods on this JdbcTemplate object to manuplate data in DB. jdbcTemplate.query(sql, new UserRowMapper()) method will use the String SQL to query the database and will loop through each “row” in the ResultSet.


RowMapper in Spring JDBC Template

RowMapper is a useful interface provided by spring JDBC, it can be used to map rows in a resultset on per row basis. RowMapper with ResultSetExtractor can be used as a powerful combination to map relational database rows to domain object.


ResultSetExtractor in Spring JDBC Template

ResultSetExtractor is an useful interface provided by Spring JDBC, it accept a resultset as input parameter and returns list implementation of extracted data from database.





To locate the static resources in our jsp files. Though we are not using any of them now.






This is all about Spring JDBC template and its usage with a Spring MVC application, now try to check it in your browser. Hope you like this post. Please let me know in the comment section if you face any issue or any suggestion that you have in your mind.

Thanks for your time.

Koushik Ghosh

Koushik Ghosh

Co-founder of CodeStrive. I am a developer in Java. I love coffee, music, coding, open source technology and playing Counter Strike. Feel free to connect me ..
Koushik Ghosh

Latest posts by Koushik Ghosh (see all)

Spring JdbcTemplate
Pride that quarells at self-breath