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, "蔡徐坤", "[email protected]", "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 =?"; BeanHandlerhandler = 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 handler = new BeanListHandler<>(Customer.class); List 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 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
使用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); } }