dbUtils结果集处理器
1,首先要有c3p0-0.9.2-pre1.jar,commons-dbutils-1.4jar,mcchange-commons-0.2jar,mysql-connector-java-5.1.28-bin.jar包
2,在src同级目录下添加c3p0-config.xml文件 :
1 <?xml version="1.0" encoding="UTF-8"?> 2 <c3p0-config> 3 <!-- 这是默认配置信息 --> 4 <default-config> 5 <!-- 连接四大参数配置 --> 6 <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property> 7 <property name="driverClass">com.mysql.jdbc.Driver</property> 8 <property name="user">root</property> 9 <property name="password">abcd@1234</property> 10 <!-- 池参数配置 --> 11 <property name="acquireIncrement">3</property> 12 <property name="initialPoolSize">10</property> 13 <property name="minPoolSize">2</property> 14 <property name="maxPoolSize">10</property> 15 </default-config> 16 17 <!-- 专门为oracle提供的配置信息 --> 18 <named-config name="oracle-config"> 19 <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property> 20 <property name="driverClass">com.mysql.jdbc.Driver</property> 21 <property name="user">root</property> 22 <property name="password">123</property> 23 <property name="acquireIncrement">3</property> 24 <property name="initialPoolSize">10</property> 25 <property name="minPoolSize">2</property> 26 <property name="maxPoolSize">10</property> 27 </named-config> 28 29 </c3p0-config>
3,写实体类:person的set,get方法,构造函数,等
4,引入jdbcUtils类:
1 package cn.itcast.utils; 2 3 import java.sql.Connection; 4 import java.sql.SQLException; 5 6 import javax.sql.DataSource; 7 8 import com.mchange.v2.c3p0.ComboPooledDataSource; 9 10 public class JdbcUtils { 11 // 配置文件的默认配置!要求你必须给出c3p0-config.xml!!! 12 private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); 13 14 /** 15 * 使用连接池返回一个连接对象 16 * @return 17 * @throws SQLException 18 */ 19 public static Connection getConnection() throws SQLException { 20 return dataSource.getConnection(); 21 } 22 23 /** 24 * 返回连接池对象! 25 * @return 26 */ 27 public static DataSource getDataSource() { 28 return dataSource; 29 } 30 }
5:结果集处理的几种方法,非常简便,
1 package cn.itcast.dbutils; 2 3 import java.sql.SQLException; 4 import java.util.List; 5 import java.util.Map; 6 7 import org.apache.commons.dbutils.QueryRunner; 8 import org.apache.commons.dbutils.handlers.BeanHandler; 9 import org.apache.commons.dbutils.handlers.BeanListHandler; 10 import org.apache.commons.dbutils.handlers.MapHandler; 11 import org.apache.commons.dbutils.handlers.MapListHandler; 12 import org.apache.commons.dbutils.handlers.ScalarHandler; 13 import org.junit.Test; 14 15 import cn.itcast.jdbc.JdbcUtils; 16 17 public class Demo3 { 18 @Test 19 public void fun1() throws SQLException { 20 QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()); 21 String sql = "insert into t_stu values(?,?,?,?)"; 22 Object[] params = {1002, "liSi", 88, "female"}; 23 24 qr.update(sql, params); 25 } 26 27 @Test 28 public void fun2() throws SQLException { 29 // 创建QueryRunner,需要提供数据库连接池对象 30 QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()); 31 // 给出sql模板 32 String sql = "select * from t_stu where sid=?"; 33 // 给出参数 34 Object[] params = {1001}; 35 36 // ResultSetHandler<Stu> rsh = new ResultSetHandler<Stu>() { 37 // 38 // @Override 39 // public Stu handle(ResultSet rs) throws SQLException { 40 // // TODO Auto-generated method stub 41 // return null; 42 // } 43 // }; 44 // 执行query()方法,需要给出结果集处理器,即ResultSetHandler的实现类对象 45 // 我们给的是BeanHandler,它实现了ResultSetHandler 46 // 它需要一个类型,然后它会把rs中的数据封装到指定类型的javabean对象中,然后返回javabean 47 Stu stu = qr.query(sql, new BeanHandler<Stu>(Stu.class), params); 48 System.out.println(stu); 49 } 50 51 /** 52 * BeanListHandler的应用,它是多行处理器 53 * 每行对象一个Stu对象! 54 * @throws Exception 55 */ 56 @Test 57 public void fun3() throws Exception { 58 QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()); 59 String sql = "select * from t_stu"; 60 List<Stu> stuList = qr.query(sql, new BeanListHandler<Stu>(Stu.class)); 61 62 System.out.println(stuList); 63 } 64 65 /** 66 * MapHandler的应用,它是单行处理器,把一行转换成一个Map对象 67 * @throws SQLException 68 */ 69 @Test 70 public void fun4() throws SQLException { 71 QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()); 72 String sql = "select * from t_stu where sid=?"; 73 Object[] params = {1001}; 74 Map map = qr.query(sql, new MapHandler(), params); 75 76 System.out.println(map); 77 } 78 79 /** 80 * MapListHandler,它是多行处理器,把每行都转换成一个Map,即List<Map> 81 * @throws SQLException 82 */ 83 @Test 84 public void fun5() throws SQLException { 85 QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()); 86 String sql = "select * from t_stu"; 87 List<Map<String,Object>> mapList = qr.query(sql, new MapListHandler()); 88 89 System.out.println(mapList); 90 } 91 92 /** 93 * ScalarHandler,它是单行单列时使用,最为合适! 94 * @throws SQLException 95 */ 96 @Test 97 public void fun6() throws SQLException { 98 QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()); 99 String sql = "select count(*) from t_stu"; 100 /* 101 * Integer、Long、BigInteger 102 */ 103 Number cnt = (Number)qr.query(sql, new ScalarHandler()); 104 105 long c = cnt.longValue(); 106 System.out.println(c); 107 } 108 }