26

How can I pass multiple parameters in jdbcTemplate queryForInt to get the count. I have tried this,

Integer count = this.jdbcTemplate
    .queryForInt("select count(name) from table_name where parameter1 = ? and parameter2 = ?", new Object[]{parameter1,parameter2});

But its showing queryForInt as strikes.

moffeltje
  • 4,521
  • 4
  • 33
  • 57
Shijin Bhaskar
  • 381
  • 1
  • 4
  • 9

2 Answers2

48

Both queryForInt() and queryForLong() are deprecated since version 3.2.2 (correct me if mistake). To fix it, replace the code with queryForObject(String, Class).

 this.jdbcTemplate.queryForObject(
                    sql, Integer.class, 
                    parameter1, parameter2);

As per spring docs

int queryForInt(String sql, Map<String,?> args)

Deprecated. Query for an int passing in a SQL query using the named parameter support provided by the NamedParameterJdbcTemplate and a map containing the arguments.

int queryForInt(String sql, Object... args) Deprecated.

Query for an int passing in a SQL query using the standard '?' placeholders for parameters and a variable number of arguments. int queryForInt(String sql, SqlParameterSource args) Deprecated. Query for an int passing in a SQL query using the named parameter support provided by the NamedParameterJdbcTemplate and a SqlParameterSource containing the arguments.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
Naruto
  • 4,221
  • 1
  • 21
  • 32
  • 1
    Please mark the answer as ACCEPTED if it helped you to solve your problem. In this way it will help others looking for similar problem. – Naruto Dec 11 '15 at 09:51
  • Edited this to include the current version with the whack-a-mole deprecation carousel. The current version is actually better as it uses a varargs syntax and moves the return type to the second argument. – Shai Almog Apr 29 '23 at 17:36
6

As per the current version of Spring Boot,

 this.jdbcTemplate.queryForObject(
                sql, new Object[] { parameter1,parameter2 }, Integer.class);

is deprecated. So try the below given code:

 this.jdbcTemplate.queryForObject(
                sql, Integer.class, new Object[] { parameter1,parameter2 });

Refer this Spring document.

chirayu joshi
  • 191
  • 3
  • 6