ResultSetExtractor Example

In this page We can easily fetch the all records from the database using query() method of JdbcTemplate class.

Syntax of query method using ResultSetExtractor

public T query(String sql,ResultSetExtractor<T< rse)

ResultSetExtractor Interface

ResultSetExtractor interface can be used to fetch records from the database. It accepts a ResultSet and returns the list.

Method of ResultSetExtractor interface

It defines only one method extractData that accepts ResultSet instance as a parameter. Syntax of the method is given below: public T extractData(ResultSet rs)throws SQLException,DataAccessException


Example of ResultSetExtractor Interface to show all the records of the table

We are assuming that you have created the following table inside the Oracle10g database.

create table employee(
id number(10),
name varchar2(100),
salary number(10)

This class contains 3 properties with constructors and setter and getters. It defines one extra method toString().

package com.javatpoint;

public class Employee {
private int id;
private String name;
private float salary;
//no-arg and parameterized constructors
//getters and setters

public String toString(){
	return id+" "+name+" "+salary;

It contains on property jdbcTemplate and one method getAllEmployees.

package com.javatpoint;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;

public class EmployeeDao {
private JdbcTemplate template;

public void setTemplate(JdbcTemplate template) {
	this.template = template;

public List<Employee> getAllEmployees(){
 return template.query("select * from employee",new ResultSetExtractor<List<Employee>>(){
	 public List<Employee> extractData(ResultSet rs) throws SQLException,
			DataAccessException {
		List<Employee> list=new ArrayList<Employee>();
		Employee e=new Employee();
		return list;


The DriverManagerDataSource is used to contain the information about the database such as driver class name, connnection URL, username and password.

There are a property named datasource in the JdbcTemplate class of DriverManagerDataSource type. So, we need to provide the reference of DriverManagerDataSource object in the JdbcTemplate class for the datasource property.

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

<?xml version="1.0" encoding="UTF-8"?>

<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="system" />
<property name="password" value="oracle" />

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>

<bean id="edao" class="com.javatpoint.EmployeeDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>


This class gets the bean from the applicationContext.xml file and calls the getAllEmployees() method of EmployeeDao class.

package com.javatpoint;

import java.util.List;

import org.springframework.context.ApplicationContext;
public class Test {

public static void main(String[] args) {
	ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
	EmployeeDao dao=(EmployeeDao)ctx.getBean("edao");
	List<Employee> list=dao.getAllEmployees();
	for(Employee e:list)