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