Examples and summary of the query method using jdbcTemplate in java

Source: Internet
Author: User

Examples and summary of the query method using jdbcTemplate in java

You can use queryForXXX () or other methods to query using JdbcTemplate in java.

1. jdbcTemplate. queryForInt () and jdbcTemplate. queryForLong ()

// Query the number of data records. An int (with a small data range) or a Long (with a large data range) type is returned.

String todayCountTopicsSql = "SELECT count (*) FROM mcp_forum_post ";

Integer todayCount = jdbcTemplate. queryForInt (todayCountTopicsSql );

You can also: Long todayCount = jdbcTemplate. queryForLong (todayCountTopicsSql );


2. jdbcTemplate. queryForObject ()

Essentially the same as queryForInt (),Single Row Single ColumnA data, such as returning a String object:
String userAccountSql = "select account from scpn_user where user_id =" + userAccountId;
String userAccount = (String) jdbcTemplate. queryForObject (userAccountSql, java. lang. String. class );


3. jdbcTemplate. queryForMap ()

Query a row of data, that is, a record. A record contains multiple fields and uses the returned column as the key.

String userAccountSql = "select account, create_time from scpn_user where user_id =" + userAccountId; Map userAccountMap = (Map) jdbcTemplate. queryForMap (userAccountSql); String userAccount = (String) userAccountMap. get ("account"); // retrieve the char data in the database and convert it to StringString createTime = (String) userAccountMap. get ("create_time "). toString (); // extract the datetime type data in the database and convert it to String

4. jdbcTemplate. queryForList (): returns the List of Map sets (which contains multiple records) and uses column names as keys. Each map represents a database record. Each record needs to be output cyclically, if you want to add a field to the result set, you can also use the following put method.

String all = "SELECT * FROM mcp_forum_post"; List scpnPostList = jdbcTemplate. queryForList (all); if (scpnPostList! = Null) {for (int I = 0; I <scpnPostList. size (); I ++) {Long userAccountId = (Long) scpnPostList. get (I ). get ("user_id"); Long lastmodUser = (Long) scpnPostList. get (I ). get ("lastmod_user"); if (lastmodUser! = Null) {String lastmodUserSql = "select account from scpn_user where user_id =" + lastmodUser; String lastmodUserAccount = (String) jdbcTemplate. queryForObject (lastmodUserSql, java. lang. string. class); scpnPostList. get (I ). put ("lastmodUserAccount", lastmodUserAccount); // you can insert a field in the result set }}}




Related Article

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.