Spring JDBC NamedParameterJdbcTemplate Example

By | June 23, 2021

In this post, We will learn about Spring JDBC NamedParameterJdbcTemplate Example using a Demo Project.

Spring Framework provides easy integration with JDBC API and provides org.springframework.jdbc.core.JdbcTemplate nad org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate utility classes 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.namedparam.NamedParameterJdbcTemplate class can help us in writing modular code with ease without worrying about resources are closed properly or not.

pom.xml

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

Employee.java

Employee model class to store employee data

EmployeeService.java

Employee Service Interface

EmployeeServiceImpl.java

Employee Service Implementation class

EmployeeDao.java

Employee DAO(Data Access Object) Interface

EmployeeDaoImpl.java

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

Spring org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate 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.

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

NamedParameterJdbcTemplate class with a basic set of JDBC CRUD operations, allows us to use named parameters rather than traditional ‘?’ placeholders.

This class mainly delegates to a wrapped JdbcTemplate after the substitution from named parameters to JDBC style ‘?’ placeholders is completed at execution time. It also allows us for expanding a List of values to the appropriate number of placeholders.

EmployeeRowMapper.java

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;

applicationContext.xml

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.namedparam.NamedParameterJdbcTemplate class of org.springframework.jdbc.datasource.DriverManagerDataSource type. So, we need to provide the reference of the DriverManagerDataSource object in the NamedParameterJdbcTemplate class for the data source property.

DB.sql

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

ClientTest.java

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 NamedParameterJdbcTemplate 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 with Jdbc java based configuration 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.