PackageCom.itheima.dbutil;Importjava.util.List;ImportJava.util.Map;ImportOrg.apache.commons.dbutils.QueryRunner;ImportOrg.apache.commons.dbutils.handlers.ArrayHandler;ImportOrg.apache.commons.dbutils.handlers.ArrayListHandler;ImportOrg.apache.commons.dbutils.handlers.ColumnListHandler;ImportOrg.apache.commons.dbutils.handlers.KeyedHandler;ImportOrg.apache.commons.dbutils.handlers.MapHandler;ImportOrg.apache.commons.dbutils.handlers.MapListHandler;ImportOrg.apache.commons.dbutils.handlers.ScalarHandler;Importorg.junit.Test;ImportCom.itheima.util.DBCPUtil; Public classResultsethandlerdemo {PrivateQueryrunner QR =NewQueryrunner (Dbcputil.getdatasource ()); //Arrayhandler: Suitable for only one case of result. Encapsulates the value of each column of the first record into a object[] array@Test Public voidTest1 ()throwsexception{object[] Objs= Qr.query ("SELECT * FROM Student",NewArrayhandler ()); for(Object obj:objs) System.out.println (obj); } //Arraylisthandler: Suitable for multiple cases of results. Enclose the value of each column in the object[] array, and place the object[] in the list@Test Public voidTest2 ()throwsexception{List<Object[]> list = Qr.query ("SELECT * FROM Student",NewArraylisthandler ()); for(object[] objs:list) {System.out.println ("----------------"); for(Object obj:objs) {System.out.println (obj); } } } //Columnlisthandler: The value that is appropriate to take a column. Encapsulates a value into a list@Test Public voidTest3 ()throwsexception{List<Object> list = Qr.query ("SELECT * FROM Student",NewColumnlisthandler ("name")); for(Object obj:list) {System.out.println (obj); } } //keyedhandler: Query multiple records. Each record is encapsulated in a map, key: Field name, Value: Field values. Then put the map as value in another map, the map key is the specified column value as key. @Test Public voidTest4 ()throwsexception{Map<Object,Map<String,Object>> Bmap = Qr.query ("SELECT * FROM Student",NewKeyedhandler ("id")); for(map.entry<object,map<string,object>>Bme:bmap.entrySet ()) {System.out.println ("--------------------"); for(Map.entry<string, object>Lme:bme.getValue (). EntrySet ()) {System.out.println (Lme.getkey ()+"="+Lme.getvalue ()); } } } //Maphandler: Fits a result. Encapsulated into a map, key: Field name, Value: Field values@Test Public voidTEST5 ()throwsexception{Map<String,Object> map = Qr.query ("SELECT * FROM Student",NewMaphandler ()); for(Map.entry<string, object>Lme:map.entrySet ()) {System.out.println (Lme.getkey ()+"="+Lme.getvalue ()); } } //Maplisthandler: Suitable for multiple results. Enclose each piece in a map, key: Field name, Value: field values, and the map is encapsulated in the list@Test Public voidTest6 ()throwsexception{List<Map<String,Object>> list = Qr.query ("SELECT * FROM Student",NewMaplisthandler ()); for(map<string,object>map:list) {System.out.println ("--------------------"); for(Map.entry<string, object>Lme:map.entrySet ()) {System.out.println (Lme.getkey ()+"="+Lme.getvalue ()); } } } //Scalarhandler: Suitable for cases where results are only one row or column. @Test Public voidTest7 ()throwsexception{Object obj= Qr.query ("SELECT count (*) from student",NewScalarhandler (1));//System.out.println (Obj.getclass (). GetName ());Long L =(Long) obj; System.out.println (L.intvalue ()); System.out.println (obj); }}
Apache dbutils Framework Result processor