BitTigerio 2017-10-07
common-dbutils.jar是Apache组织提供的一个对JDBC进行简单封装的开源工具类库,使用它能够简化JDBC应用程序的开发,同时也不会影响程序的性能。
1、QueryRunner类
①update方法:
②query方法:
2、ResultSetHandler接口:
3、示例:
1 import<span> cn.itcast.jdbc.JdbcUtils; 2 import<span> org.apache.commons.dbutils.QueryRunner; 3 import org.apache.commons.dbutils.handlers.*<span>; 4 import<span> org.junit.Test; 5 import<span> java.sql.SQLException; 6 import<span> java.util.List; 7 import<span> java.util.Map; 8 9 public class<span> Demo3 { 10 <span> @Test 11 public void fun1() throws<span> SQLException { 12 QueryRunner qr = new<span> QueryRunner(JdbcUtils.getDataSource()); 13 String sql = "INSERT INTO t_stu VALUES(?,?,?,?)"<span>; 14 Object[] params = {1002,"lisi",99,"female"<span>}; 15 <span> qr.update(sql,params); 16 <span> } 17 <span> @Test 18 public void fun2() throws<span> SQLException { 19 //创建QueryRunner,提供数据库连接池对象 20 QueryRunner qr = new<span> QueryRunner(JdbcUtils.getDataSource()); 21 //给出sql模板 22 String sql = "SELECT * FROM t_stu WHERE sid=?"<span>; 23 //给出参数 24 Object[] params = {1002<span>}; 25 //执行query()方法,需要给出集处理器,即 ResultSetHandler的实现类对象 26 //我们需要给的是BeanHandler,它实现了ResultSetHandler 27 //它需要一个类型,然后它会把rs中的数据封装到指定类型的javabean对象中,然后返回javabean对象 28 Stu stu = qr.query(sql,new BeanHandler<Stu>(Stu.class<span>),params); 29 <span> System.out.println(stu); 30 <span> } 31 //BeanListHandler的应用,它是多行处理器 32 //每行对象一个Stu对象 33 <span> @Test 34 public void fun3() throws<span> Exception{ 35 QueryRunner qr = new<span> QueryRunner(JdbcUtils.getDataSource()); 36 String sql = "SELECT * FROM t_stu"<span>; 37 List<Stu> stuList = qr.query(sql,new BeanListHandler<Stu>(Stu.class<span>)); 38 <span> System.out.println(stuList); 39 <span> } 40 41 //MapHandler的应用,它是单行处理器,把一行转换成一个Map对象 42 <span> @Test 43 public void fun4() throws<span> Exception{ 44 QueryRunner qr = new<span> QueryRunner(JdbcUtils.getDataSource()); 45 String sql = "SELECT * FROM t_stu WHERE sid=?"<span>; 46 Object[] params = {1001<span>}; 47 Map map = qr.query(sql,new<span> MapHandler(),params); 48 <span> System.out.println(map); 49 <span> } 50 51 //MapListHandler,它是多行处理器,把每行都转换成一个Map,即List<Map> 52 <span> @Test 53 public void fun5() throws<span> Exception{ 54 QueryRunner qr = new<span> QueryRunner(JdbcUtils.getDataSource()); 55 String sql = "SELECT * FROM t_stu "<span>; 56 List<Map<String,Object>> mapList = qr.query(sql,new<span> MapListHandler()); 57 <span> System.out.println(mapList); 58 <span> }@Test 59 //ScalarHandler,它是单行单列时使用,最为合适。 60 public void fun6() throws<span> Exception{ 61 QueryRunner qr = new<span> QueryRunner(JdbcUtils.getDataSource()); 62 String sql = "SELECT COUNT(*) FROM t_stu "<span>; 63 Number cnt = (Number) qr.query(sql,new<span> ScalarHandler()); 64 long c =<span> cnt.longValue(); 65 <span> System.out.println(c); 66 <span> } 67 }
1 import<span> com.mchange.v2.c3p0.ComboPooledDataSource; 2 import<span> java.sql.Connection; 3 import<span> java.sql.SQLException; 4 5 public class<span> JdbcUtils { 6 //配置文件的默认配置,要求你必须给出从c3p0-config.xml 7 private static ComboPooledDataSource dataSource = new<span> ComboPooledDataSource(); 8 //使用连接池返回一个连接对象 9 public static Connection getConnection() throws<span> SQLException { 10 return<span> dataSource.getConnection(); 11 <span> } 12 //返回连接池对象 13 public static<span> ComboPooledDataSource getDataSource() { 14 return<span> dataSource; 15 <span> } 16 }