Core JavaJava

Spring JDBC IncorrectResultSetColumnCountException: Incorrect column count Fix

1. Overview

When working with Spring JDBC, one common issue developers face is the spring.jdbc.IncorrectResultSetColumnCountException. This exception occurs when the number of columns returned by a query doesn’t match what the JdbcTemplate method expects. Even if the SQL works perfectly in a database tool, Spring can still throw this error if your method call and query structure are mismatched.

org.springframework.dao.IncorrectResultSetColumnCountException: Incorrect column count

At first glance, this message can be frustrating — the query works fine in your SQL client, yet Spring throws an exception when you run it in your application. This article will explain why the spring.jdbc.IncorrectResultSetColumnCountException occurs, how Spring interprets query results, and the steps you can take to resolve it.

We’ll specifically focus on cases involving queryForList() and custom object mapping, using a Student object as an example.

2. Introduction to the Problem

The IncorrectResultSetColumnCountException occurs when Spring expects a specific number of columns in your SQL result but receives a different count. In most cases, this happens because:

  • You are using a method designed to return a single column but your query returns multiple columns.
  • You are using a method that maps rows to a single value (like queryForObject with a type) instead of mapping them to an object or map.
  • Your SQL query’s SELECT statement changes over time (e.g., adding a column) but the Java code still expects the original structure.

Example scenario:

String sql = "SELECT id, name FROM students";
List<Integer> studentIds = jdbcTemplate.queryForList(sql, Integer.class);

Here, queryForList(sql, Integer.class) tells Spring to expect exactly one column of type Integer. But the query returns two columns (id and name). Spring detects this mismatch and throws an IncorrectResultSetColumnCountException.

3. The queryForList() Method

JdbcTemplate.queryForList() is a convenient method for retrieving multiple rows, but you must be careful with its overloaded forms. There are two common signatures:

1. Returning a list of maps (multiple columns):

List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
  • Each row is stored as a Map where the column names are keys and column values are the map’s values.
  • Useful when you need multiple columns and don’t want to bind them to a POJO immediately.

2. Returning a list of single-column values:

List<String> names = jdbcTemplate.queryForList("SELECT name FROM students", String.class);
  • The second parameter specifies the Java type of the column.
  • Spring expects exactly one column in the result set. If the SQL returns more than one column, IncorrectResultSetColumnCountException is thrown.

Key takeaway:

  • If your SQL returns multiple columns, do not use queryForList(sql, elementType). Use queryForList(sql) or map results with RowMapper instead.

4. Mapping Each Row to a Student Object

A cleaner, type-safe way to retrieve multiple columns is to map each row to a Java object using RowMapper.

For example, suppose we have a Student class:

public class Student {
    private int id;
    private String name;

    // getters and setters
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Correct Approach Using RowMapper

String sql = "SELECT id, name FROM students";

List<Student> students = jdbcTemplate.query(sql, new RowMapper<Student>() {
    @Override
    public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
        Student student = new Student();
        student.setId(rs.getInt("id"));
        student.setName(rs.getString("name"));
        return student;
    }
});

Why this works:

  • query() with a RowMapper doesn’t assume a fixed number of columns — you decide how to extract each value from the ResultSet.
  • No IncorrectResultSetColumnCountException occurs because Spring no longer assumes you’re fetching a single column.

Using Lambda for Simplicity (Java 8+)

List<Student> students = jdbcTemplate.query(sql, 
    (rs, rowNum) -> {
        Student s = new Student();
        s.setId(rs.getInt("id"));
        s.setName(rs.getString("name"));
        return s;
    });

5. Conclusion

The spring.jdbc.IncorrectResultSetColumnCountException almost always means your method call doesn’t match your SQL’s result structure. Specifically:

  • When fetching one column → Use queryForList(sql, type) but make sure your SELECT returns exactly one column.
  • When fetching multiple columns → Use queryForList(sql) to get a List<Map<String, Object>> or use query() with a RowMapper for type-safe mapping.

By ensuring the number of columns in your SQL matches the expectations of your method call, you can avoid this error entirely. Using RowMapper is often the safest and most maintainable approach, especially when working with domain objects like Student.

Final tip: Always verify both your SQL and your method signature when troubleshooting spring.jdbc.IncorrectResultSetColumnCountException. Matching them correctly will save you hours of debugging.

Ashraf Sarhan

With over 8 years of experience in the field, I have developed and maintained large-scale distributed applications for various domains, including library, audio books, and quant trading. I am passionate about OpenSource, CNCF/DevOps, Microservices, and BigData, and I constantly seek to learn new technologies and tools. I hold two Oracle certifications in Java programming and business component development.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button