02-使用C3P0连接池连接MySql并且使用QueryRunner简化数据库操作
来源:互联网 发布:mac中如何复制粘贴 编辑:程序博客网 时间:2024/06/18 02:42
1.导入相应的JARs
mysql驱动jar包:mysql-connector-java-5.1.28-bin.jar
C3P0需要的jar包:c3p0-0.9.2-pre1.jar和mchange-commons-0.2.jar
2.在src下添加C3P0配置文件:c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?><c3p0-config><default-config> <property name="jdbcUrl">jdbc:mysql://localhost:3306/grain</property><property name="driverClass">com.mysql.jdbc.Driver</property><property name="user">root</property><property name="password">123</property><property name="acquireIncrement">3</property><property name="initialPoolSize">10</property><property name="minPoolSize">2</property><property name="maxPoolSize">10</property></default-config></c3p0-config>
3.封装为JdbcUtils,用来获取Connection对象,以及开启和关闭事务。有以下接口:
Connection getConnection():从c3p0连接池获取Connection对象,所以需要提供c3p0-config.xml配置文件;
beginTransaction():为当前线程开启事务;
commitTransaction():提交当前线程的事务;
rollbackTransaction():回滚当前线程的事务;
releaseConnection(Connection):如果参数连接对象不是当前事务的连接对象,那么关闭它,否则什么都不做;
4.测试JdbcUtils
@Testpublic void testJdbcUtils() throws SQLException {Connection con = JdbcUtils.getConnection();System.out.println(con);JdbcUtils.releaseConnection(con);System.out.println(con.isClosed());}
5.封装TxQueryRunner,使用JdbcUtils连接数据库,使用QueryRunner简化数据库操作
引入jar包:commons-dbutils-1.4.jar,有以下接口:
int[] batch(String sql, Object[][] params):执行批处理,参数sql是SQL语句模板,params为参数;
T query(String sql, ResultSetHandler<T> rh):执行查询,执行查询,参数sql为要执行的查询语句模板,rh是结果集处理,用来把结果集映射成你想要的结果;
T query(String sql, ResultSetHandler<T> rh, Object… params):执行查询,参数sql为要执行的查询语句模板,rh是结果集处理,用来把结果集映射成你想要的结果,params是sql语句的参数;
int update(String sql):执行增、删、改语句,参数sql是要执行的SQL语句;
int update(Stringsql, Object param):执行增、删、改语句,参数sql是要执行的SQL语句,参数param是参数(一个参数);
- int update(String sql, Object… params):执行增、删、改语句,参数sql是要执行的SQL语句,参数params是参数(多个参数);
@Testpublic void testUpdate() throws SQLException {String sql = "insert into user_info(uid,uname,phonenumber,region,address,postcode) values(?,?,?,?,?,?)";Object[] params = {"0001","xiaoming","12345678912","China","Beijing","123456"};QueryRunner q = new TxQueryRunner();q.update(sql,params);}@Testpublic void testUpdate2() throws SQLException {try{JdbcUtils.beginTransaction();String sql = "insert into user_info(uid,uname,phonenumber,region,address,postcode) values(?,?,?,?,?,?)";Object[] params1 = {"0002","xiaoming","12345678912","China","Beijing","123456"};Object[] params2 = {"0003","xiaoming","12345678912","China","Beijing","123456"};QueryRunner q = new TxQueryRunner();q.update(sql,params1);q.update(sql,params2);JdbcUtils.commitTransaction();}catch(Exception e){try{JdbcUtils.rollbackTransaction();}catch(SQLException ee){}}}//单行结果集,BeanHandler@Testpublic void testQuery() throws SQLException{String sql = "select * from user_info where uid = ?";QueryRunner q = new TxQueryRunner();User u = q.query(sql, new BeanHandler<User>(User.class),"0001");System.out.println(u);}//多行结果集,BeanListHandler@Testpublic void testQuery2() throws SQLException{String sql = "select * from user_info";QueryRunner q = new TxQueryRunner();List<User> list_u = q.query(sql, new BeanListHandler<User>(User.class));System.out.println(list_u);}//单行结果集,map@Testpublic void testQuery3() throws SQLException{String sql = "select * from user_info where uid = ?";QueryRunner q = new TxQueryRunner();Map<String,Object> map_u = q.query(sql, new MapHandler(),"0001");System.out.println(map_u);}//多行结果集,List<map>@Testpublic void testQuery4() throws SQLException{String sql = "select * from user_info";QueryRunner q = new TxQueryRunner();List<Map<String,Object>> list_map_u = q.query(sql, new MapListHandler());System.out.println(list_map_u);}//单行单列,ScalarHandler@Testpublic void testQuery5() throws SQLException{String sql = "select count(*) from user_info";QueryRunner q = new TxQueryRunner();Object obj = q.query(sql, new ScalarHandler());//select count(*)的结果强转成NumberNumber n = (Number)obj;System.out.println(n.longValue());}
- 02-使用C3P0连接池连接MySql并且使用QueryRunner简化数据库操作
- 使用c3p0连接池操作mysql数据库中文乱码解决办法
- 使用c3p0连接mysql数据库
- 用数据库连接池c3p0来连接数据库,用queryrunner来操作
- 使用queryrunner操作数据库
- c3p0连接池使用
- 使用C3P0连接池
- C3P0连接池使用
- C3P0连接池使用
- java使用c3p0连接数据库
- java使用c3p0连接mysql
- Java编程使用C3P0连接Mysql数据库(十二)
- c3p0连接MySql数据库
- c3p0连接池的使用
- Tomcat 使用 c3p0连接池
- C3P0连接池使用小结
- 使用c3p0创建连接池
- 使用c3p0连接池checkoutTimeout
- 64位Win7系统中配置Apache+Mysql+PHP环境
- C++调用python
- HDU 2864 Repository 字典树
- xutils dbutils使用中遇到的问题
- 读《中国文学史》
- 02-使用C3P0连接池连接MySql并且使用QueryRunner简化数据库操作
- coderforce A. Case of the Zeros and Ones
- LeetCode(155) Min Stack
- cocos2dx 苹果5分辨率 适配错误
- 程序读取特定目录下的字符数
- unity3d 获取系统硬件信息
- oracle监听开启trace file
- [计算机导论思维与系统(一)]
- Android仿微信通通讯录