VOOZH about

URL: https://www.geeksforgeeks.org/springboot/spring-resultsetextractor/

⇱ Spring - ResultSetExtractor - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring - ResultSetExtractor

Last Updated : 8 Oct, 2025

ResultSetExtractor is an interface used to extract data from a ResultSet object returned by executing an SQL query. It is especially useful when mapping an entire ResultSet (multiple rows or nested data) into a single object or collection.

  • Used when a RowMapper is not sufficient (e.g., custom or hierarchical mapping).
  • Allows manual control over ResultSet iteration and transformation.
  • The JdbcTemplate.query() method accepts a ResultSetExtractor instance.

Method Syntax:

query() method syntax:

  • Java: 21 or later
  • Spring Boot: Version 3.2.0 or higher
  • Database: MySQL
  • Build Tool: Maven

Step-by-Step Implementation

Step 1: Define the Database Schema

Execute the following SQL statements to create and populate the Student table.

CREATE TABLE Student (

id INT PRIMARY KEY AUTO_INCREMENT,

name VARCHAR(45) NOT NULL,

department VARCHAR(45) NOT NULL

);


INSERT INTO Student (name, department)

VALUES ('geek', 'computer science');

Step 2: Add Dependencies

Include the following dependencies in your pom.xml file.

pom.xml:

Step 3: Create a Model Class

Define the model class representing the student entity.

Student.java:

Step 4: Create a DAO Interface

Create a StudentDao interface for database access operations.

Step 5: Implement the DAO using ResultSetExtractor

Create the implementation class that uses ResultSetExtractor to map query results.

StudentDaoImpl.java:

Step 6: Configure Database Connection

Add the configuration to application.yml.

application.yml:

spring:

datasource:

url: jdbc:mysql://localhost:3306/school_db

username: root

password: pass

driver-class-name: com.mysql.cj.jdbc.Driver

Step 7: Create the Main Application Class

Create a main class to bootstrap the Spring Boot application.

ResultSetExtractorApplication.java:

Step 8: Write the Test Class

Write a simple integration test to verify data fetching.

StudentDaoTest.java:

Step 9: Run the Application

Run the project using:

mvn spring-boot:run

Output

When executed, the console will print the student records retrieved from the database.

👁 Output
Output


Comment

Explore