DBUtils使用数据源

之前的示例中,我们使用QueryRunner时传入了Connection对象。其实我们也可以DataSource数据源对象。下面的示例将演示如何在QueryRunner中使用数据源来查询记录。我们将从Customer表中读取一条记录。

1 使用数据源的语法

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

2 使用数据源的示例

2.1 下载dbcp数据源包

下载dbcp相关包

2.2 编写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.3 编写CustomDatasource类

CustomDataSource:

package com.yiidian.dbutils;

import org.apache.commons.dbcp.BasicDataSource;

import javax.sql.DataSource;

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class CustomDataSource {

    // 驱动程序
    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";

    private static DataSource datasource;
    private static final BasicDataSource basicDataSource;

    static {
        basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName(JDBC_DRIVER);
        basicDataSource.setUsername(USER);
        basicDataSource.setPassword(PASS);
        basicDataSource.setUrl(DB_URL);
    }

    public static DataSource getInstance() {
        return basicDataSource;
    }
}

2.4 编写核心类

MainApp:

package com.yiidian.dbutils;

import com.yiidian.domain.Customer;
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.*;

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class MainApp {

    public static void main(String[] args) throws SQLException {
        DbUtils.loadDriver(CustomDataSource.JDBC_DRIVER);

        QueryRunner queryRunner = new QueryRunner(CustomDataSource.getInstance());

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

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

        System.out.print("编号: " + customer.getId());
        System.out.print(", 用户名: " + customer.getName());
        System.out.print(", 性别: " + customer.getGender());
        System.out.print(", 联系电话: " + customer.getTelephone());
        System.out.println(", 住址: " + customer.getAddress());
    }
}

2.5 运行测试

热门文章

优秀文章