Some details of the "Spring" queryforxxx () method used

Source: Internet
Author: User
about using Spring's jdbctemplate to simplify JDBC development: After Spring 3.2.2, the queryForInt in JdbcTemplate has been canceled.
Use of the Queryforxxx () method:
queryForObject instead of the queryForInt method.
queryForObject method, view source code, queryForObject (String sql, object[] args, class<t> requiredtype) {}

Example: the queryForInt () method before spring 3.2.2
JdbcTemplate jdbctemplate = new JdbcTemplate ();
String sql = "SELECT COUNT (*) from user";
Call the queryForInt () method to get the returned int type directly.
int count = jdbctemplate.queryforint (SQL);

Example: queryForObject () method
JdbcTemplate jdbctemplate = new JdbcTemplate ();
String sql = "SELECT COUNT (*) from user";
queryForObject () method, if you need to return an int type, write Integer.class, and you need to return a long type to write Long.class.
int count = Jdbctemplate.queryforobject (Sql,interger.class);

Note:When it is necessary to return what type, then the third parameter specifies what type, for example: need to return int type, write integer.class, need to return a long type to write Long.class.
This differs from the queryForInt () method in version JdbcTemplate prior to spring 3.2.2, but the essence is the same, and the difference is that we need to specify the type of parameter returned.

Summary:queryForObject () replaces the queryForInt () method with a type that requires the developer to specify an additional return.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.