1 xml version ="1.0" encoding= "UTF-8" ?> 2 < c3p0-config> 3 4 < default-config> 5 6 < property name ="driverClass" > com.mysql.jdbc.Driver property > 7 < property name ="jdbcUrl" > jdbc:mysql:///数据库名 property > 8 < property name ="user" > 数据库用户名 property > 9 < property name ="password" > 数据库密码 property > 10 default-config> 11 c3p0-config>
1 package cn.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 12 // 获得c3p0连接池对象 13 private static ComboPooledDataSource ds = new ComboPooledDataSource(); 14 15 /** 16 * 获得数据库连接对象 17 * 18 * @return 19 * @throws SQLException 20 */ 21 public static Connection getConnection() throws SQLException { 22 return ds.getConnection(); 23 } 24 25 /** 26 * 获得c3p0连接池对象 27 * @return 28 */ 29 public static DataSource getDataSource() { 30 return ds; 31 } 32 }
1 drop database if exists jdbc; 2 CREATE DATABASE IF NOT EXISTS jdbc; 3 USE jdbc; 4 5 drop table if exists user ; 6 CREATE TABLE IF NOT EXISTS `user ` ( 7 `id` int(11) NOT NULL AUTO_INCREMENT, 8 ` name` varchar(50) DEFAULT NULL , 9 `pwd` varchar(50) DEFAULT NULL , 10 PRIMARY KEY (`id`) 11 ) ; 12 #数据初始化 13 insert into user values (null ,'zhangsan' ,'123456' ); 14 insert into user values (null ,'lisi' ,'123456' );
1 //需求:向user表插入一条数据 2 3 @Test 4 5 public void test1(){ 6 7 //第一步:创建queryRunner对象,用来操作sql语句 8 9 QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); 10 11 //第二步:创建sql语句 12 13 String sql = "insert into user values(null,?,?)"; 14 15 //第三步:执行sql语句,params:是sql语句的参数 16 17 //注意,给sql语句设置参数的时候,按照user表中字段的顺序 18 19 try { 20 21 int update = qr.update(sql, "狗蛋","123456"); 22 23 System.out.println(update); 24 25 } catch (SQLException e) { 26 27 e.printStackTrace(); 28 29 } 30 31 } 32 33 //需求:修改id==7的数据 34 35 @Test 36 37 public void test2(){ 38 39 //第一步:创建queryRunner对象,用来操作sql语句 40 41 QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); 42 43 //第二步:创建sql语句 44 45 String sql = "update user set name = ? where id = ?"; 46 47 //第三步:执行sql语句,params:是sql语句的参数 48 49 //注意,给sql语句设置参数的时候,按照user表中字段的顺序 50 51 try { 52 53 int update = qr.update(sql, "柳岩",7); 54 55 System.out.println(update); 56 57 } catch (SQLException e) { 58 59 e.printStackTrace(); 60 61 } 62 63 } 64 65 //需求:删除id==7的数据 66 67 @Test 68 69 public void test3(){ 70 71 //第一步:创建queryRunner对象,用来操作sql语句 72 73 QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); 74 75 //第二步:创建sql语句 76 77 String sql = "delete from user where id = ?"; 78 79 //第三步:执行sql语句,params:是sql语句的参数 80 81 //注意,给sql语句设置参数的时候,按照user表中字段的顺序 82 83 try { 84 85 int update = qr.update(sql, 7); 86 87 System.out.println(update); 88 89 } catch (SQLException e) { 90 91 e.printStackTrace(); 92 93 } 94 95 } 96
1 package cn.handler; 2 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import org.apache.commons.dbutils.ResultSetHandler; 9 10 import cn.itcast.domain.User; 11 // ResultSetHandler, 12 //MyHandler 是自定义的ResultSetHandler封装结果集策略对象 13 public class MyHandler implements ResultSetHandler表示封装结果的类型 >{ 14 15 @Override 16 public List
handle(ResultSet rs) throws SQLException { 17 // 封装数据,数据从 Resultset 中获取 18 List list = new ArrayList (); 19 while(rs.next()){ 20 User u = new User(); 21 u.setId(rs.getInt( "id")); 22 u.setName(rs.getString( "name")); 23 u.setPwd(rs.getString( "pwd")); 24 25 list.add(u); 26 } 27 return list; 28 } 29 30 } 31
1 //需求:获取user表中所有的数据 2 3 @Test 4 5 public void test4(){ 6 7 //第一步:创建queryRunner对象,用来操作sql语句 8 9 QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); 10 11 //第二步:创建sql语句 12 13 String sql = "select * from user"; 14 15 //第三步:执行sql语句,params:是sql语句的参数 16 17 //注意,给sql语句设置参数的时候,按照user表中字段的顺序 18 19 try { 20 21 Listlist = qr.query(sql, new MyHandler()); 22 23 System.out.println(list); 24 25 } catch (SQLException e) { 26 27 e.printStackTrace(); 28 29 } 30 31 }
1 //需求:测试BeanHandler策略 2 3 //BeanHandler:将查询结果的第一行数据,封装到user对象 4 5 @Test 6 7 public void test7(){ 8 9 //第一步:创建queryRunner对象,用来操作sql语句 10 11 QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); 12 13 //第二步:创建sql语句 14 15 String sql = "select * from user"; 16 17 //第三步:执行sql语句,params:是sql语句的参数 18 19 //注意,给sql语句设置参数的时候,按照user表中字段的顺序 20 21 try { 22 23 User user = qr.query(sql, new BeanHandler(User.class)); 24 25 System.out.println(user); 26 27 } catch (SQLException e) { 28 29 e.printStackTrace(); 30 31 } 32 33 }
1 //需求:测试BeanListHandler策略 2 3 //BeanListHandler:将查询结果的每一行封装到user对象,然后,再存入list集合 4 5 @Test 6 7 public void test8(){ 8 9 //第一步:创建queryRunner对象,用来操作sql语句 10 11 QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); 12 13 //第二步:创建sql语句 14 15 String sql = "select * from user"; 16 17 //第三步:执行sql语句,params:是sql语句的参数 18 19 //注意,给sql语句设置参数的时候,按照user表中字段的顺序 20 21 try { 22 23 Listlist = qr.query(sql, new BeanListHandler (User.class)); 24 25 System.out.println(list); 26 27 } catch (SQLException e) { 28 29 e.printStackTrace(); 30 31 } 32 33 } 34
1 //需求:测试ScalarHandler策略 2 3 //ScalarHandler:封装类似count、avg、max、min、sum。。。。函数的执行结果 4 5 @Test 6 7 public void test14(){ 8 9 //第一步:创建queryRunner对象,用来操作sql语句 10 11 QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); 12 13 //第二步:创建sql语句 14 15 String sql = "select count(*) from user"; 16 17 //第三步:执行sql语句,params:是sql语句的参数 18 19 //注意,给sql语句设置参数的时候,按照user表中字段的顺序 20 21 try { 22 23 Object object = qr.query(sql, new ScalarHandler()); 24 25 System.out.println(object); 26 27 } catch (SQLException e) { 28 29 e.printStackTrace(); 30 31 } 32 33 }