Shahid Ghafoor
Shahid Ghafoor

Reputation: 3103

jdbcTemplate query with multiple queries - spring boot

When I execute the following query, its work fine

jdbcTemplate.query("select * from foo Limit 50");

But when I want to try to execute the following query , it giving me syntax error even successfully running in mysql

jdbcTemplate.query("select SQL_CALC_FOUND_ROWS * from foo Limit 10; SELECT FOUND_ROWS()");

any update ?

Upvotes: 1

Views: 7263

Answers (1)

NiVeR
NiVeR

Reputation: 9786

You can't execute two queries on a single query execution from the template. It expects (correctly) that a single execution of its corresponding method will execute a single query, and hence return a single result. The key point is the ; character. I believe that's the cause of the syntax error, exactly because of what you are trying to do.

There are two ways to go around this:

  • Perform 2 different queries, one for the limit and one for the total count
  • Implement a stored procedure that will give you both results, and this time you can get them with just a single query

If you choose the first approach and you want to synchronize you can check this link.

Upvotes: 3

Related Questions