Examples and summaries of query methods using JdbcTemplate in Java

Source: Internet
Author: User

When querying using JdbcTemplate in Java, you can use methods such as QUERYFORXXX ()

1, Jdbctemplate.queryforint () and Jdbctemplate.queryforlong ()

Query the number of data records, return an int (smaller data range) or a long (large data range) type

String todaycounttopicssql= "SELECT COUNT (*) from Mcp_forum_post";

Integer Todaycount=jdbctemplate.queryforint (Todaycounttopicssql);

can also:Long todaycount=jdbctemplate.queryforlong (todaycounttopicssql);


2,jdbctemplate.queryforobject()

Essentially the same as queryForInt (), the return is a single- line single row of 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, a record, a record containing multiple fields, and use the returned columns for 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");    Remove the data from the char type in the database into Stringstring createtime= (String) useraccountmap.get ("Create_time"). ToString ();  

4, jdbctemplate.queryforlist (): Returns the collection of map list (it contains multiple records), with the column name of the key, each map represents a database record, you need to use a loop to output each record, if you want to add a field in 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  }}   }




Examples and summaries of query methods using JdbcTemplate in Java

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.