DBUtils for database operations

Keywords: Java Apache SQL JDBC

Summary

DBUtils is a practical tool for database operation in Java programming. It is compact, simple and practical.

DBUtils encapsulates the operation of JDBC, simplifies the operation of JDBC, and can write less code.

Introduction to Three Core Functions of DBUtils

  • API to provide operations on sql statements in Query Runner
  • ResultSetHandler interface, which defines how to encapsulate the result set after a select operation
  • The DBUtils class, which is a tool class, defines ways to close resources and transaction processing

QueryRunner Core Class

  • Query Runner (Data Sources), providing data sources (connection pools), DbUtils underlying automatic maintenance connection
  • Update (String sql, Obj...params), execute update data
  • query (String sql, ResultSetHandler < T > rsh, Object... Panrams), execute queries

ResultSetHandler result set processing class

ArrayHandler: Suitable for taking a record and converting the first row of data in the result set into an array of objects.

ArrayListHandler: Suitable for multiple records, converting each row of data in the result set into an array of objects and storing them in the List.

 

BeanHandler: Encapsulate the first row of data in the result set into a corresponding JavaBean instance (encapsulate each record as an object, suitable for taking one record)

BeanListHandler: Encapsulate each row of data in the result set into a corresponding JavaBean instance and store it in the List. / / key

MapHandler: Encapsulate the first row of data in the result set into a Map. The key is the column name and the value is the corresponding value. / / key

MapListHandler: Encapsulate each row of data in the result set into a Map and store it in List

ColumnListHandler: Store data from a column in the result set in a List.

KeyedHandler(name): Encapsulate each row of data in the result set into a Map (List < Map >), and then store these maps in a map with the key of the specified column.

ScalarHandler: Put a column in the first row of the result set into an object. / / key

    package com.oterman.DButils;
 
    import java.sql.SQLException;
    import java.util.List;
    import java.util.Map;
 
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.ArrayHandler;
    import org.apache.commons.dbutils.handlers.ArrayListHandler;
    import org.apache.commons.dbutils.handlers.BeanHandler;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    import org.apache.commons.dbutils.handlers.MapHandler;
    import org.apache.commons.dbutils.handlers.MapListHandler;
    import org.apache.commons.dbutils.handlers.ScalarHandler;
    import org.junit.Test;
 
    import com.mchange.v2.c3p0.ComboPooledDataSource;
 
    /**
     * This program introduces various implementation classes of ResultSetHandler.
     * @author Mushroom
     *
     */
    public class RSHandlerDemo {
 
        //--7.ScalarHandler: Put a column of the first row of the query result into an object; locate a value accurately;
        @Test
        public void query7() throws SQLException{
            
            QueryRunner runner=new QueryRunner(new ComboPooledDataSource());
            String name=(String)runner.query("select * from account",new ScalarHandler(2));
            System.out.println(name);
        }
        
        //--6.MapListHandler: Save each row of the query results into one map The key is the column name and the value is the column value; then the key is the column name and the value is the column value. map Deposit in list Medium;
        @Test
        public void query6() throws SQLException{
            
            QueryRunner runner=new QueryRunner(new ComboPooledDataSource());
            List<Map<String,Object>> map=runner.query("select * from account",new MapListHandler());
            System.out.println(map);
        }
        
        //--5.MapHandler: Save the first line of the query results into one map The key is the column name and the value is the column value.
        @Test
        public void query5() throws SQLException{
            
            QueryRunner runner=new QueryRunner(new ComboPooledDataSource());
            Map<String,Object> map=runner.query("select * from account",new MapHandler());
            System.out.println(map);
        }
        
        //--4.BeanListHandler: Encapsulate each row of the query results into one javabean Objects are then stored in them list Medium;
        @Test
        public void query4() throws SQLException{
            
            QueryRunner runner=new QueryRunner(new ComboPooledDataSource());
            List<Account> list=runner.query("select * from account",new BeanListHandler<Account>(Account.class));
            System.out.println(list);
        }
        
        
        //--3.BeanHandler: Encapsulate the first line of the query results into one javabean In the object;
        @Test
        public void query3() throws SQLException{
            
            QueryRunner runner=new QueryRunner(new ComboPooledDataSource());
            Account account=runner.query("select * from account",new BeanHandler<Account>(Account.class));
            System.out.println(account);
        }
        
        
        //--2.ArrayListHandler: Put each row of the query result into an array, and then put the array into the collection.
        @Test
        public void query2() throws SQLException{
            
            QueryRunner runner=new QueryRunner(new ComboPooledDataSource());
            List<Object[]> list=runner.query("select * from account",new ArrayListHandler());
            System.out.println(list);
        }
        
        //--1.ArrayHandler: Place the first line of the query result in an array
        @Test
        public void query1() throws SQLException{
            
            QueryRunner runner=new QueryRunner(new ComboPooledDataSource());
            Object[] array=runner.query("select * from account",new ArrayHandler());
            System.out.println(array);
        }
    }

2018-10-15

Posted by Brentley_11 on Sat, 02 Feb 2019 14:12:16 -0800