The use of BeanListHandler interface in DBUtils

Keywords: Programming Apache Java MySQL JDBC

Original link: http://www.yiidian.com/dbutils/dbutils-beanlisthandler.html

org.apache.commons.dbutils.BeanListHandler is the implementation of the ResultSetHandler interface, and is responsible for converting all records at the ResultSet result level to the List collection of JavaBean s. This class is thread safe.

1 syntax for beanlisthandler

List<Customer> custList= queryRunner.query(conn, "SELECT * FROM customer", resultHandler); 

2 example of beanlisthandler

2.1 write Customer entity class

package com.yiidian.domain;

/**
 * A little tutorial website - http://www.yidian.com
 */
public class Customer {
    private Integer id;
    private String name;
    private String gender;
    private String telephone;
    private String address;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

2.2 writing core classes

MainApp:

package com.yiidian.dbutils;

import com.yiidian.domain.Customer;
import org.apache.commons.dbutils.AsyncQueryRunner;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.*;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * A little tutorial website - http://www.yidian.com
 */
public class MainApp {
    // Driver program
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    // URL connection
    static final String DB_URL = "jdbc:mysql://localhost:3306/test";

    //database information 
    static final String USER = "root";
    static final String PASS = "root";

    public static void main(String[] args) throws SQLException {
        Connection conn = null;

        QueryRunner queryRunner = new QueryRunner();

        DbUtils.loadDriver(JDBC_DRIVER);

        conn = DriverManager.getConnection(DB_URL, USER, PASS);

        ResultSetHandler<List<Customer>> resultHandler = new BeanListHandler<Customer>(Customer.class);

        try {
            List<Customer> custList = queryRunner.query(conn, "SELECT * FROM customer", resultHandler);
            for(Customer customer: custList ) {

                System.out.print("number: " + customer.getId());
                System.out.print(", User name: " + customer.getName());
                System.out.print(", Gender: " + customer.getGender());
                System.out.print(", Contact number: " + customer.getTelephone());
                System.out.println(", address: " + customer.getAddress());
            }
        } finally {
            DbUtils.close(conn);
        }
    }
}

2.3 operation test

Welcome to my official account: a little tutorial. Get exclusive learning resources and daily dry goods push. If you are interested in my series of tutorials, you can also follow my website: yiidian.com

Posted by codygoodman on Sat, 28 Mar 2020 07:55:12 -0700