English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

ResultSetExtractor example | Retrieve records using Spring JdbcTemplate

We can use JdbcTemplate The class query()The method easily retrieves records from the database, and we need to pass an instance of ResultSetExtractor.

Syntax of query methods using ResultSetExtractor

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

ResultSetExtractor interface

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

Methods of the ResultSetExtractor interface

It defines only one method that accepts a ResultResult instance as a parameter. The syntax of the method is as follows:

public T extractData(ResultSet rs) throws SQLException, DataAccessException

Example of ResultSetExtractor interface to display all records of the table

We assume that you have already installed Oracle10The following table was created in the g database.

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

Employee.java

This class includes3a class with constructors, setters, and getters. It defines an additional toString() method.

package com.w3codebox;
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;


EmployeeDao.java

It contains properties jdbcTemplate and a method getAllEmployees.

package com.w3codebox;
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>>(){
    @Override
     public List<Employee> extractData(ResultSet rs) throws SQLException,
            DataAccessException {
        List<Employee> list=new ArrayList<Employee>();
        while(rs.next()){
        Employee e=new Employee();
        e.setId(rs.getInt(1));
        e.setName(rs.getString(2));
        e.setSalary(rs.getInt(3));
        list.add(e);
        
        return list;
        
    });
  

applicationContext.xml

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

In the JdbcTemplate class of type DriverManagerDataSource, there is a named datasource The attribute. Therefore, we need to provide a reference to the DriverManagerDataSource object for the data source attribute in the JdbcTemplate class.

Here, we use the JdbcTemplate object in the EmployeeDao class, so we pass it through the setter method, but you can also use the constructor.

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
    xmlns:p="http://www.springframework.org/schema/p
    xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<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>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>
<bean id="edao" class="com.w3codebox.EmployeeDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>

Test.java

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

package com.w3codebox;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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)
        System.out.println(e);