JDBC ResultSetExtractor Example
Spring provides a simplification in handling database access with the Spring JDBC Template. The org.springframework.jdbc.core.ResultSetExtractor interface is a callback interface used by JdbcTemplate’s query methods. Implementations of this interface perform the actual work of extracting results from an SQL ResultSet object.
In this article, we will try to show how the ResultSetExtractor mechanism can be applied to a Java application.
1. Introduction
In Spring JDBC development, developers can use JdbcTemplate and JdbcDaoSupport classes to simplify the overall database operation processes. Spring JdbcTemplate is a powerful mechanism to connect to the database and execute SQL queries. It internally uses JDBC API but eliminates a lot of problems of JDBC API.
The Spring JDBC Template has the following advantages compared with the standard JDBC API,
- The Spring JDBC template allows to clean-up the resources automatically, e.g. release the database connections.
- The Spring JDBC template converts the standard JDBC SQL Exceptions into
RuntimeExceptions. This allows the programmer to react more flexible to the errors. - The Spring JDBC template also converts the vendor specific error messages into better understandable error messages.
1.1 Problems of JDBC API
The problems of JDBC API are as follows:
- We need to write a lot of code before and after executing the query, such as creating
Connection,Statement, ClosingResultSet,Statement, andConnection. - We need to perform exception handling on the database logic.
- We need to handle transactions.
- Repetition of all these codes from one database logic to another is a time-consuming task.
Spring JdbcTemplate eliminates all the above-mentioned problems of JDBC API and provides methods to write the queries directly. Let’s take a look and understand the ResultSetExtractor interface.
1.2 ResultSetExtractor Interface
The org.springframework.jdbc.core.ResultSetExtractor interface can be used to fetch records from the database. It accepts a ResultSet as a method argument and returns the List. Implementation of this interface perform the actual work of extracting results from a ResultSet, but you don’t need to worry about exception handling.
SQL Exceptions will be caught and handled by the calling JdbcTemplate. This interface is mainly used within the JDBC framework itself. The org.springframework.jdbc.core.ResultSetExtractor interface defines only one method extractData that accepts ResultSet instance as a parameter. The syntax of the method is given below:
Method Signature
public List extractData(ResultSet rs) throws SQLException, DataAccessException {
// Database Logic
}
1.3 Download and Install MySQL
You can watch this video in order to download and install the MySQL database on your windows operations system.
Now, open up the Eclipse IDE and let’s start building the application!
2. JDBC ResultSetExtractor Example
2.1 Tools Used
We are using Eclipse Kepler SR2, JDK 8, MySQL database and Maven (to download the MySQL connector and Spring libraries). Having said that, we have tested the code against JDK 1.7 and it works well.
2.2 Project Structure
Firstly, let’s review the final project structure, in case you are confused about where you should create the corresponding files or folder later!

2.3 Project Creation
This section will demonstrate on how to create a Dynamic Web Java Maven project with Eclipse. In Eclipse IDE, go to File -> New -> Maven Project
In the New Maven Project window, it will ask you to select project location. By default, ‘Use default workspace location‘ will be selected. Select the ‘Create a simple project (skip archetype selection)‘ checkbox and just click on next button to proceed.
It will ask you to ‘Enter a group id for the artifact.’ We will input the details as shown in the below image. The version number will be by default 0.0.1-SNAPSHOT.
Click on Finish and now the creation of a maven project is completed. If you observe, it has downloaded the maven dependencies and a pom.xml file will be created. It will have the following code:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>JdbcResultSetExtractor</groupId> <artifactId>JdbcResultSetExtractor</artifactId> <version>0.0.1-SNAPSHOT</version> </project>
We can start adding the dependencies that developers want like MySQL, Spring Jars etc. Let’s start building the application!
3. Application Building
Below are the steps involved in developing this application:
3.1 Database & Table Creation
This tutorial uses a database called tutorialDb. The database is not included when you create the project in eclipse so you first need to create the database to follow this tutorial:
- Create a new database
tutorialDbas:
CREATE DATABASE tutorialDb;
- Use the created database
tutorialDbto create table as:
USE tutorialDb;
- Create the table
tech_editorsas shown below:
CREATE TABLE tech_editors (id int(11) NOT NULL AUTO_INCREMENT, name varchar(100) DEFAULT NULL, salary int(11) NOT NULL, PRIMARY KEY (id));
- Now we shall insert some values into the
tech_editorstable as:
INSERT INTO tech_editors (id, name, salary) VALUES (1, 'Java Code Geek', 10000); INSERT INTO tech_editors (id, name, salary) VALUES (2, 'Harry Potter', 5000); INSERT INTO tech_editors (id, name, salary) VALUES (3, 'Lucifer', 5500);
If everything goes well, the table will be shown as below in the MySQL workbench:

3.2 Maven Dependencies
In this example, we are using latest MySQL version i.e. mysql-connector-java-5.1.41 and Spring dependencies. The updated file will have the following code:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>JdbcResultSetExtractor</groupId>
<artifactId>JdbcResultSetExtractor</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
</build>
</project>
3.3 Java Class Creation
Let’s create the required java files. Right click on src/main/java folder, New -> Package.









