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