Book Image

SPRING COOKBOOK

Book Image

SPRING COOKBOOK

Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Finding the number of results for an SQL query


In this recipe, we will add a DAO method to quickly get the number of results for an SQL query without actually loading the rows in the memory.

How to do it…

Use an SQL count(*) function and get the value directly using the queryForObject() method with a second argument specifying Long as the returned type:

public long countMinorUsers() {
  String sql = "select count(*) from age < 18";
  return jdbcTemplate.queryForObject(sql, Long.class);
}