Apache dbutils Framework Result processor

Source: Internet
Author: User

 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

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.