YuFeng

JDBC DButils的使用
dbutils工具包:本地下载:Apache dbutils.zipDButils封装了JDBC的操作,来简化对数...
扫描右侧二维码阅读全文
24
2018/11

JDBC DButils的使用

dbutils工具包:
本地下载:Apache dbutils.zip
DButils封装了JDBC的操作,来简化对数据库操作的代码量,核心类是:QueryRunner
使用DButils需要导入相应的工具包commons-dbutils-1.4.jar
DButils的代码展示`java
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
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.junit.jupiter.api.Test;

import cn.yufeng.datasource.domain.Product;
import cn.yufeng.datasource.utils.C3P0Utils;
import cn.yufeng.datasource.utils.DBCPUtils;

public class DButilsConnection {



//@Test
//测试javaBean   BeanListHandler    查询
public void testBeanListHandler() {
    try {
        QueryRunner qr = new QueryRunner(DBCPUtils.getDataSource());
        String sql = "select * from product";
        List<Product> list = qr.query(sql, new BeanListHandler<Product>(Product.class));
        for(Product c : list) {
            System.out.println(c.getName());
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

//@Test
//测试javaBean   BeanHandler    查询
public void testBeanHandler() {
    try {
        QueryRunner qr = new QueryRunner(DBCPUtils.getDataSource());
        String sql = "select * from product";
        Product p = qr.query(sql, new BeanHandler<Product>(Product.class));
        System.out.println(p.getName());
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

//@Test
//测试javaBean testMapListHandler    查询
public void testMapListHandler() {
    try {
        QueryRunner qr = new QueryRunner(DBCPUtils.getDataSource());
        String sql = "select * from product";
        List<Map<String, Object>> query = qr.query(sql,new MapListHandler());
        for(Map<String, Object> p : query) {
            System.out.println(p.get("name"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

//@Test
//测试javaBean MapHandler    查询
public void testMapHandler() {
    try {
        QueryRunner qr = new QueryRunner(DBCPUtils.getDataSource());
        String sql = "select * from product";
        Map<String, Object> p = qr.query(sql, new MapHandler());
        System.out.println(p);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}


//@Test
//测试添加数据
public void testInsertData() {
    try {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "insert into product values (?,?,?)";
        Object[] information = {1,"荣耀",2399};
        int row = qr.update(sql, information);
        if(row > 0) {
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

@Test
//测试更新数据
public void testUpdate() {
    try {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "update product set name = ? where price = ?";
        Object[] information = {"华为2",2399};
        int row = qr.update(sql, information);
        if(row > 0) {
            System.out.println("更新成功");
        }else {
            System.out.println("更新失败");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

}



    QueryRunner 相关得其他操作
    模糊查询:(查询指定队列)
    List<Object> query = runner.query(sql, new ColumnListHandler("pname"), "%"+word+"%");
    用于查询数据库内得相应数量
    //用于获取查询得数量
    QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
            String sql = "select count(*) from product";
            Long query = (Long) runner.query(sql, new ScalarHandler());
            return query.intValue();
Last modification:June 25th, 2019 at 09:21 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment