Encapsulate query results into List <Map> and Use callback functions to dynamically encapsulate data (44). map callback function
Start the QueryRunner class manually. Data encapsulation:
MapListHandler
MapHandler
BeanListHandler
BeanHandler
Step 1: Basic encapsulation Test
Write a class, QueryRunner, and implement a method query (SQL)-List <map>
package cn.itcast.dbutils;import java.sql.Connection;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.sql.DataSource;
Public class QueryRunner {// receives a ds private DataSource ds; public QueryRunner () {} public QueryRunner (DataSource ds) {this. ds = ds;}/*** is encapsulated into a List <Map> */public List <Map <String, Object> query (String SQL) {// encapsulate data using List <Map <String, Object> list = new ArrayList <Map <String, Object> (); // declare the returned Object Connection con = null; try {con = ds. getConnection (); // execute the query Statement st = con. createStatement (); ResultSet rs = st.exe cuteQuery (SQL); // analysis result set ResultSetMetaData rsmd = rs. getMetaData (); // get the number of columns int cols = rsmd. getColumnCount (); // traverses data while (rs. next () {// Map a row of Data <String, Object> mm = new HashMap <String, Object> (); // traverse the column for (int I = 0; I <cols; I ++) {// obtain the column name String colName = rsmd. getColumnName (I + 1); // get data Object val = rs. getObject (I + 1); // encapsulate it in map mm. put (colName, val);} // put this map to list. add (mm) ;}} catch (Exception e) {throw new RuntimeException (e);} finally {try {con. close ();} catch (SQLException e) {e. printStackTrace () ;}} return list ;}}
Dynamic Data encapsulation using callback Functions
A callback is implemented by a third-party class during execution.
Callback is generally composed of two parts:
1: call class-QueryRunner. Instance type
2: callback specification-ResultSetHandler. It is generally an interface.
3: The callback Specification defines the callback method and this method is called by the call class.
PackageCn. itcast. demo;
ImportJava. util. ArrayList;
ImportJava. util. List;
ImportOrg. junit. Test;
Public ClassThreadDemo {
/**
* Testing generics
*@ ThrowsException
*/
@ Test
Public VoidAa ()ThrowsException {
// Generic, with no location at runtime.
List <String> list =NewArrayList <String> ();
List. add ("ddd ");
List list2 = list;
List2.add( 777 );
List. getClass (). getMethod ("add", Object.Class). Invoke (list, 909 );
For(Object o: list ){
System.Err. Println (o + "," + o. getClass ());
}
}
/**
* Test thread
*@ ParamArgs
*/
Public Static VoidMain (String [] args ){
NewThread (
NewRunnable (){
Public VoidRun (){
System.Err. Println ("B This is runnable. run .... ");
}
}
){
Public VoidRun (){
Super. Run (); // call the method of the parent class to execute runnalble. run
System.Err. Println ("A This is thread. run ....");
};
}. Start ();
}
}