Dbutils Result Set Processor introduction

Source: Internet
Author: User
Tags connection pooling

Common-dbutils.jar is an open Source Tool class library provided by the Apache Organization for simple JDBC encapsulation, which simplifies the development of JDBC applications without compromising program performance.

1, Queryrunner class

①update Method:

    • int update (String sql,object...params)--Executable and DELETE statements
    • int update (Connection con,string sql,object...params)--requires the caller to provide Connection, which means that this method no longer manages Connection. Support transactions.

②query Method:

    • T query (String sql,resultsethandler rsh,object...params)--Executable query
      • It will get resultset first and then call Rsh's handle () to convert RS into the desired type.
    • T query (Connection con,string sql,resultsethandler rsh,object...params); Support transactions.

2. Resultsethandler Interface:

    • Beanhandler (single-line)--the constructor requires a class type parameter that converts a row of results to a JavaBean object of the specified type.
    • Beanlisthandler (multiline)--the constructor also requires a class-type parameter to convert a row of result sets to a javabean, so many rows are converted to a list object, a bunch of javabean
    • Maphandler (single line)--Convert a row of result sets to a Map object
      • One line of records:
      • Sid Sname Age Gender
      • 1001 ZS Male
      • A map:
      • {sid:1001, Sname:zs, age:99, Gender:male}
    • Maplisthandler (multiple lines)--Convert a row of records into a map, multiple lines are more than one map, that is, List<map>.
    • Scalarhandler (single row)-typically with the "Select COUNT (*) from T_stu;" statement, the result set is a single-row, and it returns an object.

3. Example:

1 ImportCn.itcast.jdbc.JdbcUtils; 2 ImportOrg.apache.commons.dbutils.QueryRunner; 3 Import org.apache.commons.dbutils.handlers.*; 4 ImportOrg.junit.Test; 5 ImportJava.sql.SQLException; 6 ImportJava.util.List; 7 ImportJava.util.Map; 8 9 public classDEMO3 {10@Test11 public void fun1 () throwsSQLException {queryrunner qr = newQueryrunner (Jdbcutils.getdatasource ()); String sql = "INSERT into T_stu VALUES (?,?,?,?)"; object[] params = {1002, "Lisi", "female"};15Qr.update (sql,params); 16}17@Test18 public void fun2 () throwsSQLException {19//create Queryrunner, provide database connection pool object Queryrunner qr = newQueryrunner (Jdbcutils.getdatasource ()); 21//gives the SQL template a String of sql = "SELECT * from T_stu WHERE sid=?"; 23//given parameter object[] params = {1002};25//execute the query () method, need to give the set processor, that is, the implementation of the Resultsethandler class object 26//We need to give the Beanhandler, it implements the RESULTSETHANDLER27//It requires a type, It then encapsulates the data in the RS into a JavaBean object of the specified type, and then returns the JavaBean object, Stu Stu = Qr.query (sql,new beanhandler<stu> (stu.class), params); 29System.out.println (Stu); 30}31//beanlisthandler Application, it is multi-line processor 32/Per line object one Stu object 33@Test34 public void Fun3 () throwsexception{35 queryrunner qr = newQueryrunner (Jdbcutils.getdatasource ()); String sql = "SELECT * FROM T_stu"; PNS list<stu> stulist = qr.query (sql,new beanlisthandler<stu> (stu.class)); 38System.out.println (stulist); 39}40//maphandler application, which is a single-line processor, converts a row into a map object 42@Test43 public void Fun4 () throwsexception{44 queryrunner qr = newQueryrunner (Jdbcutils.getdatasource ()); String sql = "SELECT * from T_stu WHERE sid=?"; object[] params = {1001};47 Map map = qr.query (sql,new  maphandler (), params), System.out.println }50 (map); Listhandler, which is a multi-line processor, converts each line into a Map, that is, list<map>52  @Test53 public void Fun5 () throws  exception{54 Queryrunner qr = new  Queryrunner (Jdbcutils.getdatasource ()), String sql = "SELECT * from T_stu" ; map<string,object>> maplist = qr.query (sql,new  Maplisthandler ());  System.out.println (maplist );//scalarhandler } @Test59, which is the most appropriate for single-row single-column use. public void Fun6 () throws  exception{61 queryrunner qr = new  Queryrunner (Jdbcutils.getdatasource ()); Stri ng sql = "Select COUNT (*) from T_stu" , number cnt = (number) qr.query (sql,new  Scalarhandler ()), and long C =< span> Cnt.longvalue ();  System.out.println (c); }67}       
1 import Com.mchange.v2.c3p0.ComboPooledDataSource; 2 import java.sql.Connection; 3 Import  Java.sql.SQLException; 4  5 public class Jdbcutils {6     //config file default configuration, required you must give from C3p0-config.xml 7     private static Combopooleddatasource  dataSource = new Combopooleddatasource (); 8     //Use connection pooling to return a Connection object 9 public     static Connection getconnection () throws SQLException {ten return }12//Return connection pool object public static  Combopooleddatasource Getdatasource () { }16}        

Dbutils Result Set Processor introduction

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.