eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Course – Summer Sale 2026 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

Course – Summer Sale 2026 – NPI (cat=Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

1. Overview

In this tutorial, we’ll discuss the deprecated queryForObject() and query() methods of JdbcTemplate and their preferred counterparts.

The deprecated methods accept parameters passed within an array of objects, whereas the new methods utilize Varargs for passing parameters. This approach is more intuitive because Varargs are designed specifically for efficiently passing a variable number of arguments to a method.

Throughout our tutorial, we’ll explain all the methods by running queries on a student table in the in-memory H2 database.

2. Database Setup

Before we look at the methods in question, let’s first examine the student table, which we’ll use in all our examples:

CREATE TABLE student (
    student_id INT AUTO_INCREMENT PRIMARY KEY,
    student_name VARCHAR(255) NOT NULL,
    age INT,
    grade INT NOT NULL,
    gender VARCHAR(10) NOT NULL,
    state VARCHAR(100) NOT NULL
);
-- Student 1
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('John Smith', 18, 3, 'Male', 'California');

-- Student 2
INSERT INTO student (student_name, age, grade, gender, state) VALUES ('Emily Johnson', 17, 2, 'Female', 'New York');

--More insert statements...

3. Deprecated Method query()

In JdbcTemplate, there are three query() method variations, each using a different functional interface to collect or process the rows from the query result. These interfaces are ResultSetExtractor, RowCallbackHandler, and RowMapper.

Let’s see each of them and their suggested replacements.

3.1. query() Method with RowMapper

The query() method in the JdbcTemplate class returns a list representing rows returned by the DB query. Let’s begin with an example where we’ll query the student table to get students of a certain age and gender:

public List<Student> getStudentsOfAgeAndGender(Integer age, String gender) {
    String sql = "select student_id, student_name, age, gender, grade from student where age= ? and gender = ?";
    Object[] args = {age, gender};
    return jdbcTemplate.query(sql, args, new StudentRowMapper());
}

In the above method, the variable args of type Object[] stores the query parameters. Even if it is a single query parameter, it needs to be added to an array, which is inconvenient.

Suppose we need additional filter criteria to get students of a certain grade. Then, we have to write a new method for it:

public List<Student> getStudentsOfAgeGenderAndGrade(Integer age, String gender, Integer grade) {
    String sql = "select student_id, student_name, age, gender, grade from student where age= ? and gender = ? and grade = ?";
    Object[] args = {age, gender, grade};
    return jdbcTemplate.query(sql, args, new StudentRowMapper());
}

So, to implement this, we modified sql and args variables. But, can we eliminate the boiler-plate code involving the args variable? Let’s explore this further in the following method:

public List<Student> getStudentsOfAgeGenderAndGrade(Integer age, String gender, String grade) {
    String sql = "select student_id, student_name, age, gender, grade from student where age= ? and gender = ? and grade = ?";
    return jdbcTemplate.query(sql, new StudentRowMapper(), age, gender, grade);
}

The varargs variation of the jdbcTemplate.query did exactly what we wanted.

Let’s see the deprecated and the replacement methods in action:

@Test
public void givenDeprecatedMethodQuery_whenArgsAgeAndGender_thenReturnStudents() {
    List<Student> students = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentsOfAgeGenderAndGrade(4, "Female", 2);
    for (Student student: students) {
        logger.info("Student Name: " + student.getStudentName() + " Student gender: " + student.getStudentGender());
    }
    assertEquals(5, students.size());
}
@Test
public void givenPreferredMethodQuery_whenArgsAgeAndGender_thenReturnStudents() {
    List<Student> students = studentDaoWithPreferredJdbcTemplateMethods.getStudentsOfAgeGenderAndGrade(4, "Female", 2);

    for (Student student: students) {
        logger.info("Student Name: " + student.getStudentName() + " Student gender: " + student.getStudentGender());
    }
    assertEquals(5, students.size());
}

As we can see above, the two test methods invoke getStudentsOfAgeAndGender() of the Dao classes, StudentDaoWithDeprecatedJdbcTemplateMethods and StudentDaoWithPreferredJdbcTemplateMethods. Following is the output of both methods:

Student Name: Olivia Garcia Student gender: Female Student grade: 2
Student Name: Ava Davis Student gender: Female Student grade: 2
Student Name: Olivia Johnson Student gender: Female Student grade: 2
Student Name: Isabella Davis Student gender: Female Student grade: 2
Student Name: Sophia Hernandez Student gender: Female Student grade: 2

No doubt, the varargs version serves its intended purpose.

In the upcoming sections, we’ll mostly discuss the usage of the deprecated and preferred methods. The benefits of the varargs version would be the same as discussed in this section. Hence, we won’t be repeating that.

3.2. query() Method with ResultSetExtractor

Now, let’s move on to the next deprecated version that uses the ResultSetExtractor interface. The method extractData() in the interface gets invoked once so that all the rows can be returned in a data structure after processing. For this example, we have created a StudentResultExtractor:

public class StudentResultExtractor implements ResultSetExtractor<List<Student>> {
    @Override
    public List<Student> extractData(ResultSet rs) throws SQLException {
        List<Student> students = new ArrayList<Student>();
        while(rs.next()) {
            Student student = new Student();
            student.setStudentId(rs.getInt("student_id"));
            student.setStudentName(rs.getString("student_name"));
            student.setAge(rs.getInt("age"));
            student.setStudentGender(rs.getString("gender"));
            student.setGrade(rs.getInt("grade"));
            student.setState(rs.getString("state"));
            students.add(student);
        }
        return students;
    }
}

It returns a List of Student.

Let’s take a look at the query method using the array of objects to pass query parameters, and StudentResultExtractor mentioned above:

public List<Student> getStudentsOfGradeAndState(Integer grade, String state) {
    String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?";
    Object[] args = {grade, state};
    return jdbcTemplate.query(sql, args, new StudentResultExtractor());
}

The above method queries the student table to retrieve students studying in a specific grade and belonging to a particular state.

Similar to the earlier section, we’ll implement the same with the varargs version of the query method:

public List<Student> getStudentsOfGradeAndState(Integer grade, String state) {
    String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?";
    return jdbcTemplate.query(sql, new StudentResultExtractor(), grade, state);
}

Finally, we invoke the above two methods in separate test functions:

@Test
public void givenDeprecatedMethodQuery_whenArgsGradeAndState_thenReturnStudents() {
    List<Student> students = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentsOfGradeAndState(1, "New York");
    for (Student student: students) {
        logger.info("Student Name: " + student.getStudentName()
          + " Student grade: " + student.getStudentGender()
          + " Student State: " + student.getState());
    }
    assertEquals(6, students.size());
}
@Test
public void givenPreferredMethodQuery_whenArgsGradeAndState_thenReturnStudents() {
    List<Student> students = studentDaoWithPreferredJdbcTemplateMethods.getStudentsOfGradeAndState(1, "New York");
    for (Student student: students) {
        logger.info("Student Name: " + student.getStudentName()
          + " Student grade: " + student.getStudentGender()
          + " Student State: " + student.getState());
    }
    assertEquals(6, students.size());
}

As expected, both the tests give the same output:

Student Name: Ethan Rodriguez Student grade: Male Student State: New York
Student Name: Benjamin Brown Student grade: Male Student State: New York
Student Name: Matthew Martinez Student grade: Male Student State: New York
Student Name: Christopher Lee Student grade: Male Student State: New York
Student Name: Liam Johnson Student grade: Male Student State: New York
Student Name: Mason Smith Student grade: Male Student State: New York

3.3. query() Method with RowCallbackHandler

Lastly, in this section, we’ll discuss the version using the interface RowCallbackHandler. For this example, we would use RowCountCallbackHandler, which is a subclass of RowCallbackHandler.

Let’s start by taking a look at the deprecated version:

public Integer getCountOfStudentsInAGradeFromAState(String grade, String state) {
    String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?";
    Object[] args = {grade, state};
    RowCountCallbackHandler countCallbackHandler = new RowCountCallbackHandler();
    jdbcTemplate.query(sql, args, countCallbackHandler);
    return countCallbackHandler.getRowCount();
}

The method, true to its name, gets the total number of students studying in a given grade and belonging to a particular state.

Similarly, here is the method using the preferred version of the query method:

public Integer getCountOfStudentsInAGradeFromAState(String grade, String state) {
    String sql = "select student_id, student_name, age, gender, grade, state from student where grade = ? and state = ?";

    RowCountCallbackHandler countCallbackHandler = new RowCountCallbackHandler();
    jdbcTemplate.query(sql, countCallbackHandler, grade, state);
    return countCallbackHandler.getRowCount();
}

Let’s see how these methods can be invoked:

@Test
public void givenDeprecatedMethodQuery_whenArgsGradeAndState_thenReturnCount() {
    Integer count = studentDaoWithDeprecatedJdbcTemplateMethods.getCountOfStudentsInAGradeFromAState(1, "New York");
    logger.info("Total students of grade 1 from New York:" + count);
    assertEquals(6, count);
}

@Test
public void givenPreferredMethodQuery_whenArgsGradeAndState_thenReturnCount() {
    Integer count = studentDaoWithPreferredJdbcTemplateMethods.getCountOfStudentsInAGradeFromAState(1, "New York");
    logger.info("Total students of grade 1 from New York:" + count);
    assertEquals(6, count);
}

As shown below, unsurprisingly, they yield identical results:

Total students of grade 1 from New York: 6

4. Deprecated Method queryForObject()

The method queryForObject() also has two deprecated variants that use the Object[] type for passing the query parameters. These are queryForObject(String sql, Object[] args, Class<T> requiredType) and queryForObject(String sql, Object[] args, RowMapper<T> rowMapper).

Like before, we’ll explore their suggested replacements using Varargs in the upcoming sections.

4.1. queryForObject() Method with RowMapper

Unlike the query() method, queryForObject() is meant for handling a DB query result with a single row. Along the same lines, let’s first check out the following method that uses the deprecated version of queryForObject():

public Student getStudentOfStudentIDAndGrade(Integer studentID, Integer grade) {
    String sql = "select student_id, student_name, age, gender, grade from student where student_id = ? and grade = ?";
    Object[] args = {studentID, grade};

    return jdbcTemplate.queryForObject(sql, args, new StudentRowMapper());
}

The above method returns the student from a grade having a specific student ID. But just like the deprecated version of the query() method, it also needs to declare a variable of type Object[] for the query parameters.

Since the above promotes boiler-plate code, let’s see a cleaner approach:

public Student getStudentOfStudentIDAndGrade(Integer studentID, Integer grade) {
    String sql = "select student_id, student_name, age, gender, grade from student where student_id = ? and grade = ?";

    return jdbcTemplate.queryForObject(sql, new StudentRowMapper(), studentID, grade);
}

However, here, there is no need to declare a variable of type Object[]. The varargs variant of queryForObject() directly takes the studentID and grade arguments as the last parameters.

Now, let’s look at how the methods are used:https://www.baeldung.com/wp-admin/post.php?post=165771&action=edit

@Test
public void givenDeprecatedMethodQueryForObject_whenArgsStudentIDAndGrade_thenReturnStudent() {
    Student student = studentDaoWithDeprecatedJdbcTemplateMethods.getStudentOfStudentIDAndGrade(4, 1);
    assertEquals(1, student.getGrade());
    assertEquals(4, student.getStudentId());
    logger.info("Student ID: " + student.getStudentId()
      + " Student Name: " + student.getStudentName() + " Student grade: " + student.getGrade());
}

@Test
public void givenPreferredMethodQueryForObject_whenArgsStudentIDAndGrade_thenReturnStudent() {
    Student student = studentDaoWithPreferredJdbcTemplateMethods.getStudentOfStudentIDAndGrade(4, 1);
    assertEquals(1, student.getGrade());
    assertEquals(4, student.getStudentId());
    logger.info("Student ID: " + student.getStudentId()
      + " Student Name: " + student.getStudentName() + " Student grade: " + student.getGrade());
}

As expected, both methods give the same output:

Student ID: 4 Student Name: Sophia Martinez Student grade: 1

4.2. queryForObject() With Class<T>

Let’s begin by taking a look at the example using the deprecated method queryForObject(String sql, Object[] args, Class<T> requiredType):

public Integer getCountOfGenderInAGrade(String gender, Integer grade) {
    String sql = "select count(1) as total from student where gender = ? and grade = ?";
    Object[] args = {gender, grade};

    return jdbcTemplate.queryForObject(sql, args, Integer.class);
}

The above method returns the total number of students of a certain gender studying in a given grade.

Let’s see the approach with the preferred method queryForObject(String sql, Class<T> requiredType, Object… args):

public Integer getCountOfGenderInAGrade(String gender, Integer grade) {
    String sql = "select count(1) as total from student where gender = ? and grade = ?";

    return jdbcTemplate.queryForObject(sql, Integer.class, gender, grade);
}

As usual, in the above method, we’re able to get rid of the variable args of type Object[].

Now, let’s go through the following methods showing getCountOfGenderInAGrade() in action:

@Test
public void givenPreferredMethodQueryForObject_whenArgsGenderAndGrade_thenReturnCount() {
    Integer count = studentDaoWithPreferredJdbcTemplateMethods.getCountOfGenderInAGrade("Female", 2);
    assertEquals(6, count);
    logger.info("Total number of Female Students: " + count);
}

@Test
public void givenDeprecatedMethodQueryForObject_whenArgsGenderAndGrade_thenReturnCount() {
    Integer count = studentDaoWithPreferredJdbcTemplateMethods.getCountOfGenderInAGrade("Female", 2);
    assertEquals(6, count);
    logger.info("Total number of Female Students: " + count);
}

Finally, as shown below, the replacement method successfully manages to get a similar result:

Total number of Female Students: 6

5. Conclusion

In this tutorial, we explored the preferred replacements of the deprecated variants of query() and queryForObject() methods of the JdbcTemplate class. With examples, we explained the new methods that use Varargs to take the query parameters as arguments. We also saw how it helps eliminate the boilerplate code for getting the parameters in an array of objects.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Summer Sale 2026 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

Course – Summer Sale 2026 – NPI (All)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

eBook Jackson – NPI EA – 3 (cat = Jackson)