Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jdbctemplate count queryForInt and pass multiple parameters

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.

like image 458
Shijin Bhaskar Avatar asked Dec 11 '15 04:12

Shijin Bhaskar


People also ask

How do you find the count of records using JdbcTemplate?

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

Is JdbcTemplate deprecated?

JdbcTemplate queryForInt() is Deprecated - Mkyong.com.

How does JdbcTemplate select multiple columns?

List<Map<String, Object>> rows = jdbcTemplate. queryForList("SELECT name, middle, family FROM table"); Every Map in this List represents a row in the returned query, the key represents the column name, and the value is the value of that column for that row.


1 Answers

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, new Object[] { parameter1,parameter2 }, Integer.class);

As per spring docs

int queryForInt(String sql, Map 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.

like image 147
Naruto Avatar answered Sep 17 '22 18:09

Naruto