Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JdbcTemplate queryForInt/Long is deprecated in Spring 3.2.2. What should it be replaced by?

The queryforInt/queryforLong methods in JdbcTemplate are deprecated in Spring 3.2. I can't find out why or what is considered the best practice to replace existing code using these methods.

A typical method:

int rowCount = jscoreJdbcTemplate.queryForInt(     "SELECT count(*) FROM _player WHERE nameKey = ? AND teamClub = ?",     playerNameKey.toUpperCase(),     teamNameKey.toUpperCase() ); 

OK the above method needs to be re-written as follows:

Object[] params = new Object[] {     playerNameKey.toUpperCase(),     teamNameKey.toUpperCase() }; int rowCount = jscoreJdbcTemplate.queryForObject(     "SELECT count(*) FROM _player WHERE nameKey = ? AND teamClub = ?",     params, Integer.class); 

Obviously this deprecation makes the JdbcTemplate class simpler (or does it?). QueryForInt was always a convenience method (I guess) and has been around a long time. Why has it been removed. The code becomes more complicated as a result.

like image 268
Dan MacBean Avatar asked Mar 27 '13 14:03

Dan MacBean


People also ask

Is JdbcTemplate deprecated?

JdbcTemplate queryForInt() is Deprecated - Mkyong.com.

What is the difference between JdbcTemplate and NamedParameterJdbcTemplate?

Spring - NamedParameterJdbcTemplate Example Functionally, there's no difference between Spring's JdbcTemplate and it's variant, NamedParameterJdbcTemplate except for : NamedParameterJdbcTemplate provides a better approach for assigning sql dynamic parameters instead of using multiple '?' in the statement.

What is the difference between JdbcTemplate and SimpleJdbcTemplate?

It's not the only difference - SimpleJdbcTemplate supports named parameters like in NamedParameterJdbcTemplate , but JdbcTemplate only supports ? style parameters. No, spring HSBC doesn't provide any abstraction over SQL.

How do you use NamedParameterJdbcTemplate?

CLOB); String SQL = "update Student set description = :description where id = :id"; NamedParameterJdbcTemplate jdbcTemplateObject = new NamedParameterJdbcTemplate(dataSource); jdbcTemplateObject. update(SQL, in); Where, in − SqlParameterSource object to pass a parameter to update a query.


2 Answers

What I think is that somebody realized that the queryForInt/Long methods has confusing semantics, that is, from JdbcTemplate source code you can see its current implementation:

@Deprecated public int queryForInt(String sql, Object... args) throws DataAccessException {     Number number = queryForObject(sql, args, Integer.class);     return (number != null ? number.intValue() : 0); } 

which may lead you to think that if the result set is empty it will return 0, however it throws an exception:

org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0

so the the following implementation is essentially equivalent to the current one:

@Deprecated public int queryForInt(String sql, Object... args) throws DataAccessException {     return queryForObject(sql, args, Integer.class); } 

And then the non deprecated code now must be replaced with the ugly:

    queryForObject(sql, new Object { arg1, arg2, ...}, Integer.class); 

or this (nicer):

    queryForObject(sql, Integer.class, arg1, arg2, ...); 
like image 200
Gabriel Belingueres Avatar answered Oct 19 '22 23:10

Gabriel Belingueres


I agree with the original poster that deprecating the convenience method queryForLong(sql) is an inconvenience.

I had developed an app using Spring 3.1 and just updated to the latest Spring version (3.2.3) and noticed that it was deprecated.

Fortunately, it was a one line change for me:

return jdbcTemplate.queryForLong(sql);  // deprecated in Spring 3.2.x 

was changed to

return jdbcTemplate.queryForObject(sql, Long.class); 

And a couple of Unit Tests seem to indicate, the above change works.

like image 28
SGB Avatar answered Oct 20 '22 00:10

SGB