ResultSetHandler接口

org.apache.commons.dbutils.ResultSetHandler接口负责对ResultSet对象进行处理。

1 ResultSetHandler的语法

Customer customer = queryRunner.query(conn, "SELECT * FROM customer WHERE name=?", resultHandler, "张三");

2 ResultSetHandler的示例

2.1 编写Customer实体类

package com.yiidian.domain;

/**
 * 一点教程网 - http://www.yiidian.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 编写核心类

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 java.sql.*;
import java.util.Arrays;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class MainApp {
    // 驱动程序
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    // URL连接
    static final String DB_URL = "jdbc:mysql://localhost:3306/test";

    //数据库信息
    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<Object[]> handler = new ResultSetHandler<Object[]>() {
            public Object[] handle(ResultSet rs) throws SQLException {
                if (!rs.next()) {
                    return null;
                }
                ResultSetMetaData meta = rs.getMetaData();
                int cols = meta.getColumnCount();
                Object[] result = new Object[cols];

                for (int i = 0; i < cols; i++) {
                    result[i] = rs.getObject(i + 1);
                }
                return result;
            }
        };

        try {
            Object[] result  = queryRunner.query(conn, "SELECT * FROM customer WHERE name=?",
                    handler, "张三");
            System.out.print("结果是: " + Arrays.toString(result));
        } finally {
            DbUtils.close(conn);
        }
    }
}

2.3 运行测试

热门文章

优秀文章