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 }}}