DBUtils的简单使用
①引入所需的jar包
②引入C3P0的配置文件
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="automaticTestTable">con_test</property> <property name="checkoutTimeout">30000</property> <property name="idleConnectionTestPeriod">30</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> <property name="maxStatements">200</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost/bank?user=root&password=root</property> </default-config> </c3p0-config>
③创建辅助类
1 package com.yxfyg.util; 2 3 import java.sql.Connection; 4 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 import com.mchange.v2.c3p0.ComboPooledDataSource; 9 10 public class JDBCUtil { 11 12 private static ComboPooledDataSource dataSource = null; 13 14 static { 15 dataSource = new ComboPooledDataSource(); 16 } 17 18 public static Connection getConnection() throws SQLException { 19 return dataSource.getConnection(); 20 } 21 22 public static ComboPooledDataSource getDataSource() { 23 return dataSource; 24 } 25 26 public static void release(ResultSet rs,Statement st,Connection conn) { 27 closeRs(rs); 28 closeSt(st); 29 closeConn(conn); 30 } 31 32 public static void release(Statement st,Connection conn) { 33 closeSt(st); 34 closeConn(conn); 35 } 36 37 private static void closeRs(ResultSet rs) { 38 try { 39 if(rs != null) { 40 rs.close(); 41 } 42 }catch(SQLException e) { 43 e.printStackTrace(); 44 }finally { 45 rs = null; 46 } 47 } 48 49 private static void closeSt(Statement st) { 50 try { 51 if(st != null) { 52 st.close(); 53 } 54 }catch(SQLException e) { 55 e.printStackTrace(); 56 }finally { 57 st = null; 58 } 59 } 60 61 private static void closeConn(Connection conn) { 62 try { 63 if(conn != null) { 64 conn.close(); 65 } 66 }catch(SQLException e) { 67 e.printStackTrace(); 68 }finally { 69 conn = null; 70 } 71 } 72 }
1 package com.yxfyg.bean; 2 3 public class Account { 4 5 private int id; 6 7 private String name; 8 9 private int money; 10 11 public int getId() { 12 return id; 13 } 14 15 public void setId(int id) { 16 this.id = id; 17 } 18 19 public String getName() { 20 return name; 21 } 22 23 public void setName(String name) { 24 this.name = name; 25 } 26 27 public int getMoney() { 28 return money; 29 } 30 31 public void setMoney(int money) { 32 this.money = money; 33 } 34 35 @Override 36 public String toString() { 37 return "Account [id=" + id + ", name=" + name + ", money=" + money + "]"; 38 } 39 40 }
④开始使用DBUtils
1 package com.yxfyg.test; 2 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.util.List; 6 7 import org.apache.commons.dbutils.QueryRunner; 8 import org.apache.commons.dbutils.ResultSetHandler; 9 import org.apache.commons.dbutils.handlers.BeanHandler; 10 import org.apache.commons.dbutils.handlers.BeanListHandler; 11 import org.junit.Test; 12 13 import com.mchange.v2.c3p0.ComboPooledDataSource; 14 import com.yxfyg.bean.Account; 15 16 public class MainTest { 17 18 @Test 19 public void test() { 20 21 //1.创建数据库连接池 22 ComboPooledDataSource dataSource = new ComboPooledDataSource(); 23 //2.以数据库连接池作为参数,创建QueryRunner对象 24 QueryRunner queryRunner = new QueryRunner(dataSource); 25 26 try { 27 //增、删、改都使用update方法,查使用query方法 28 //插入一条数据 29 queryRunner.update("insert into account values(null,?,?)","Lucy",2000); 30 31 //修改一条数据 32 queryRunner.update("update account set money = money - ? where name = ?",300,"Lucy"); 33 34 //删除一条数据 35 queryRunner.update("delete from account where name = ?","Lucy"); 36 37 //查询一条数据(使用匿名内部类,自己实现方法) 38 Account account = queryRunner.query("select * from account where name = ?", new ResultSetHandler<Account>() { 39 40 @Override 41 public Account handle(ResultSet rs) throws SQLException { 42 Account account = new Account(); 43 while(rs.next()) { 44 account.setId(rs.getInt("id")); 45 account.setName(rs.getString("name")); 46 account.setMoney(rs.getInt("money")); 47 } 48 return account; 49 } 50 },"Lucy"); 51 52 System.out.println(account.toString()); 53 54 //查询一条数据(使用接口ResultSetHandler的实现类BeanHandler) 55 account = queryRunner.query("select * from account where name = ?", new BeanHandler<Account>(Account.class) ,"Lucy"); 56 System.out.println(account.toString()); 57 58 //查询多条数据(使用接口ResultSetHandler的实现类BeanListHandler) 59 List<Account> list = queryRunner.query("select * from account", new BeanListHandler<Account>(Account.class)); 60 for (Account a : list) { 61 System.out.println(a.toString()); 62 } 63 64 } catch (SQLException e) { 65 e.printStackTrace(); 66 } 67 } 68 69 }
浙公网安备 33010602011771号