Spring JdbcTemplate example

In this post, we are going to see Spring JdbcTemplate example.
Spring jdbcTemplate is used to convenient way to connect to database and execute queries. It internally use JDBC code only, but provides you APIs , so you don’t have to write boiler plate code. You don’t have write much code before and after executing queries for creating connection , creating statement , closing connections etc.

Lets understand with the help of simple example:

Lets say you want to save country object to database.
If you want to write it using normal JDBC api, you have to use below code:

If you use Spring JdbcTemplate to do same operation, you need to use below code:

If you observe above code, you can easily see that you need to write very less code with Spring JdbcTemplate and you also don’t need to do exception handling

Spring example using Normal JDBC API:

Create Country table in mysql database with following code:

We will you use Country table for querying and updating values in database.

Lets first create our bean class Country.java

Create a DAO classed CountryDAO.java which will have all methods for database operations.

Create DAO implementation of above interface using normal JDBC APIs.

Create applicationcontext.xml as below

Configure datasource basaed on your connection details, datasource bean will be [autowired](https://java2blog.com/autowired-annotation-in-spring/ “autowired”) in CountryDAOImpl.
Create Main class named SpringApplicationMain.java as below

When you run above program, you will get below output:

As you can see in CountryDAOImpl, you need write lot of code to handle connections, exceptions etc.

Spring JdbcTemplate example:

Now Replaced above CountryDAOImpl.java with below Spring JdbcTemplate example.

As you can see, you need to write very less code with SpringJdbcTemplate.
jdbcTemplate.update(query, args) : This method is used to add or update in database. Object[] args is arguments array corresponds to ? in the query. jdbcTemplate.queryForObject(query, new Object[] {id}, new RowMapper() : We have used RowMapper and overriden maprows method to set values from resultset to country object. When you run above SpringApplicationMain again , you will get similar ouput.

Lets compare lines of code for normal JDBC and Spring JdbcTemplate

Total lines of code for same operations
Spring JdbcTemplate

Was this post helpful?


You may also like:

Related Posts

  • 29 August

    Spring NamedParameterJdbcTemplate example

    In this post , we are going to see about Spring NamedParameterJdbcTemplate. In previous post, we have already seen Spring JdbcTemplate example. NamedParameterJdbcTemplate is used to pass named parameter instead of ? in case of JdbcTemplate. It makes code more readable and maintainable. Lets say you have 10 parameters and you have to use 10 ? […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.