QueryRunner的添加与查询操作

Apache-DBUtils实现CRUD操作,commmons-dbutils是Apache组织提供的开源JDBC工具类,

封装了针对于数据库的增删改查操作,Class QueryRunner 

TestQueryRunner

package com.aff.util;
import java.sql.Connection;
import java.sql.Date;
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.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;
import com.aff.bean.Customer;
//commmons-dbutils是Apache组织提供的一个开源的jdbc工具类库,
//封装了针对于数据库的增删改查操作
public class TestQueryRunner { // 测试插入 @Test public void testInsert() { Connection conn = null; try { QueryRunner runner = new QueryRunner(); conn = JDBCUtilsDruid.getConnection(); String sql = "insert into customers (name,email,birth)values(?,?,?)"; runner.update(conn, sql, "蔡徐坤", "cxk@126.com", "1997-2-24"); System.out.println("添加成功"); } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtilsDruid.closeResource(conn, null, null); } } // 测试查询 // ResultSetHandler 是个接口 // BeanHander是ResultSetHandler接口的实现类,用于封装表中的一条记录 @Test public void testQuery1() { Connection conn = null; try { QueryRunner runner = new QueryRunner(); conn = JDBCUtilsDBCP.getConnection(); String sql = "select id,name,birth from customers where id =?"; BeanHandler<Customer> handler = new BeanHandler<>(Customer.class); Customer customer = runner.query(conn, sql, handler, 20); System.out.println(customer); } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtilsDBCP.closeResource(conn, null, null); } } // BeanListHander是ResultSetHandler接口的实现类,用于封装表中的多条记录构成的集合 @Test public void testQuery2() throws Exception { Connection conn = null; try { QueryRunner runner = new QueryRunner(); conn = JDBCUtilsC3P0.getConnection(); String sql = "select id,name,birth from customers where id <?"; BeanListHandler<Customer> handler = new BeanListHandler<>(Customer.class); List<Customer> list = runner.query(conn, sql, handler, 20); list.forEach(System.out::println); } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtilsC3P0.closeResource(conn, null, null); } } // MapHander是ResultSetHandler接口的实现类,对应表中的一条记录 // 将字段及相应字段的值作为map中的key和value // 键--值 键--值 体现 key-value // {name=何苗苗, birth=1996-02-03, id=20} @Test public void testQuery3() throws Exception { Connection conn = null; try { QueryRunner runner = new QueryRunner(); conn = JDBCUtilsC3P0.getConnection(); String sql = "select id,name,birth from customers where id =?"; MapHandler handler = new MapHandler(); Map<String, Object> map = runner.query(conn, sql, handler, 20); System.out.println(map); } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtilsC3P0.closeResource(conn, null, null); } } // MapListHander是ResultSetHandler接口的实现类,对应表中的多条记录 // 将字段及相应的值作为map中的key和value,将这些map添加到list中 @Test public void testQuery4() throws Exception { Connection conn = null; try { QueryRunner runner = new QueryRunner(); conn = JDBCUtilsC3P0.getConnection(); String sql = "select id,name,birth from customers where id <?"; MapListHandler listHandler = new MapListHandler(); List<Map<String, Object>> list = runner.query(conn, sql, listHandler, 20); list.forEach(System.out::println); /* {name=朱茵, birth=2014-01-16, id=16} {name=何苗, birth=2014-01-17, id=18} {name=芳芳, birth=1996-03-04, id=19} */ } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtilsC3P0.closeResource(conn, null, null); } } // // ScalarHandler是ResultSetHandler接口的实现类, 用于查询特殊值 //返回表中的记录数 @Test public void testQuery5() throws Exception { Connection conn = null; try { QueryRunner runner = new QueryRunner(); conn = JDBCUtilsC3P0.getConnection(); String sql = "select count(*) from customers"; ScalarHandler handler = new ScalarHandler(); Long count = (Long) runner.query(conn, sql, handler); System.out.println(count); } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtilsC3P0.closeResource(conn, null, null); } } // ScalarHandler是ResultSetHandler接口的实现类, //返回表中的最大出生日期 @Test public void testQuery6() throws Exception { Connection conn = null; try { QueryRunner runner = new QueryRunner(); conn = JDBCUtilsC3P0.getConnection(); String sql = "select max(birth) from customers"; ScalarHandler handler = new ScalarHandler(); Date maxBirth = (Date) runner.query(conn, sql, handler); System.out.println(maxBirth); } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtilsC3P0.closeResource(conn, null, null); } } }

 

 

使用Dbutils关闭资源

例如:

JDBCUtilsC3P0

package com.aff.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbutils.DbUtils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

//使用C3P0数据库连接池
public class JDBCUtilsC3P0 {
    // 把池子拿到外边,连接池一个就够,需要的连接从池子中拿
    private static ComboPooledDataSource cbpds = new ComboPooledDataSource("helloc3p0");

    public static Connection getConnection() throws SQLException {
        Connection conn = cbpds.getConnection();
        return conn;
    }
    public static void closeResource(Connection conn, PreparedStatement ps, ResultSet rs) {
        // 7.资源的关闭
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
        DbUtils.closeQuietly(rs);
    }
}

 

posted @ 2020-04-12 14:31  林淼零  阅读(2417)  评论(1编辑  收藏  举报