Encapsulate the query results into List & lt; Map & gt; and Use callback functions to dynamically encapsulate data (44). map callback function

Source: Internet
Author: User

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 ();

}

}

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.