304304 <para >Here is a simple query for getting the number of rows in a
305305 relation:</para >
306306
307- <programlisting language =" java" >int rowCount = this.jdbcTemplate.queryForInt ("select count(*) from t_actor");</programlisting >
307+ <programlisting language =" java" >int rowCount = this.jdbcTemplate.queryForObject ("select count(*) from t_actor", int.class );</programlisting >
308308
309309 <para >A simple query using a bind variable:</para >
310310
311- <programlisting language =" java" >int countOfActorsNamedJoe = this.jdbcTemplate.queryForInt (
312- "select count(*) from t_actor where first_name = ?", "Joe");</programlisting >
311+ <programlisting language =" java" >int countOfActorsNamedJoe = this.jdbcTemplate.queryForObject (
312+ "select count(*) from t_actor where first_name = ?", int.class, "Joe");</programlisting >
313313
314314 <para >Querying for a <classname >String</classname >:</para >
315315
@@ -567,7 +567,7 @@ public int countOfActorsByFirstName(String firstName) {
567567
568568 SqlParameterSource namedParameters = new MapSqlParameterSource("first_name", firstName);
569569
570- return namedParameterJdbcTemplate.queryForInt (sql, namedParameters);
570+ return this. namedParameterJdbcTemplate.queryForObject (sql, int.class , namedParameters);
571571}</programlisting >
572572
573573 <para >Notice the use of the named parameter notation in the value
@@ -598,9 +598,9 @@ public int countOfActorsByFirstName(String firstName) {
598598
599599 String sql = "select count(*) from T_ACTOR where first_name = :first_name";
600600
601- Map namedParameters = Collections.singletonMap("first_name", firstName);
601+ Map< String, String > namedParameters = Collections.singletonMap("first_name", firstName);
602602
603- return this.namedParameterJdbcTemplate.queryForInt (sql, namedParameters);
603+ return this.namedParameterJdbcTemplate.queryForObject (sql, int.class , namedParameters);
604604}</programlisting >
605605
606606 <para >One nice feature related to the
@@ -664,7 +664,7 @@ public int countOfActors(Actor exampleActor) {
664664
665665 SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(exampleActor);
666666
667- return this.namedParameterJdbcTemplate.queryForInt (sql, namedParameters);
667+ return this.namedParameterJdbcTemplate.queryForObject (sql, int.class , namedParameters);
668668}</programlisting >
669669
670670 <para >Remember that the
@@ -838,10 +838,8 @@ public class ExecuteAStatement {
838838 <section xml : id =" jdbc-statements-querying" >
839839 <title >Running queries</title >
840840
841- <para >Some query methods return a single value. To retrieve a count or a
842- specific value from one row, use
843- <methodname >queryForInt(..)</methodname >,
844- <methodname >queryForLong(..)</methodname > or
841+ <para >Some query methods return a single value. To retrieve a count or
842+ a specific value from one row, use
845843 <methodname >queryForObject(..)</methodname >. The latter converts the
846844 returned JDBC <classname >Type</classname > to the Java class that is
847845 passed in as an argument. If the type conversion is invalid, then an
@@ -862,11 +860,11 @@ public class RunAQuery {
862860 }
863861
864862 public int getCount() {
865- return this.jdbcTemplate.queryForInt ("select count(*) from mytable");
863+ return this.jdbcTemplate.queryForObject ("select count(*) from mytable", int.class );
866864 }
867865
868866 public String getName() {
869- return (String) this.jdbcTemplate.queryForObject("select name from mytable", String.class);
867+ return this.jdbcTemplate.queryForObject("select name from mytable", String.class);
870868 }
871869
872870 public void setDataSource(DataSource dataSource) {
0 commit comments