When querying data using JdbcTemplate, you can use methods such as queryforxxx. Let's look at the following one by one:
1, Jdbctemplate.queryforint () and Jdbctemplate.queryforlong () --use queryForInt to return the number of records in the user table, queryForInt with such a SQL can calculate the total number of records when paging
Jdbctemplate.queryforint ("SELECT count (*) from user");
2, Jdbctemplate.queryforobject ()- -essentially the same as queryForInt, but can return different objects, such as returning a string object
String name = (string) jdbctemplate.queryforobject ( --3 arguments, 1, SQL 2, parameter array to be passed 3, returned object class ") Select name from USER WHERE id =? ",
New object[] {ID},
Java.lang.String.class);
3, Jdbctemplate.queryforlist (???) --Return a list with map, each map is a record, the key in the map is the field name
List rows = jdbctemplate.queryforlist ("SELECT * from user"); --Get a list with map For (int i=0;i<rows. Size (); i++) { --traversal
Map usermap=rows.get (i);
System. out.println (Usermap.get ("id"));
System. out.println (Usermap.get ("name"));
System. out.println (Usermap.get ("age"));
}
4, Jdbctemplate.queryformap (SQL) -This query can only be a query to query a record, return a Map,key value is the value of column
Map map = Jdbctemplate.queryformap ("SELECT COUNT (*) as Keyval from user"); map.get ("Keyval")
5, Jdbctemplate.queryforrowset (???) --Returns a rowset and then calls. GetString or Getint to fetch the value
6, Jdbc1.query (SQL, New RowCallbackHandler () --Returns a ResultSet object, processrow a mechanism for automatic looping, which automatically executes the statements in Processrow until the size of the--rs is done. We can use the list to complete the transfer of the object, except that list is going to use final to modify jdbc1.query (SQL, new RowCallbackHandler () {
Editing
Public void Processrow (ResultSet rs) throws SQLException {
Videosearch vs = new Videosearch ();
Vs.setrecordingfilename (rs.getstring ("Recordingfilename"));
Vs.setcallid (rs.getstring ("Callid"));
List. Add (VS);
} }
Description: How to use JdbcTemplate:
Define a JdbcTemplate node in the Applicationcontext.xml,
Use Pojo injection to get injected after you can perform operations that do not need to inherit what base class
<bean id="JdbcTemplate" class="Org.springframework.jdbc.core.JdbcTemplate" >
<property name="DataSource" ref="DataSource"/> </bean>
SqlRowSet rs = jdbctemplate.queryforrowset (sql, params);
JdbcTemplate has a number of ORM callback operations that return results to the list of objects,
But there are times when you need to return to resultset,
Spring has provided a similar resultset,
Implement the spring SqlRowSet of the JDBC3.0 rowset interface
Note JdbcTemplate try to perform only query operations, do not update, otherwise it will destroy Hibernate's Level two cache system
Spring's JdbcTemplate use