您的位置:首页 > 产品设计 > UI/UE

02-使用C3P0连接池连接MySql并且使用QueryRunner简化数据库操作

2015-10-31 23:31 706 查看
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
@Test
public 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是参数(多个参数);

6.测试TxQueryRunner
@Test
public 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);
}
@Test
public 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
@Test
public 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
@Test
public 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
@Test
public 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>
@Test
public 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
@Test
public void testQuery5() throws SQLException{
String sql = "select count(*) from user_info";
QueryRunner q = new TxQueryRunner();
Object obj = q.query(sql, new ScalarHandler());
//select count(*)的结果强转成Number
Number n = (Number)obj;
System.out.println(n.longValue());
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: