一、代码
1、导入jar包
2、c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="jdbcUrl">jdbc:mysql://localhost:3306/tpexam?characterEncoding=utf-8&useSSL=false</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">tpexam</property> <property name="password">123456</property> <!-- 连接池参数 --> <!-- 超时时间 --> <property name="checkoutTimeout">30000</property> <property name="idleConnectionTestPeriod">30</property> <!-- 初始化申请的连接数量 --> <property name="initialPoolSize">5</property> <property name="maxIdleTime">30</property> <!-- 最大的连接数量 --> <property name="maxPoolSize">10</property> <property name="minPoolSize">1</property> <property name="maxStatements">200</property> </default-config> </c3p0-config>
3、具体代码
test.java
import java.sql.Connection; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import utils.DbUtil; import entity.User; public class Test1 { public static void main(String[] args) { getAll(); getOne(); } /** * 获取所有 */ public static void getAll() { Connection conn = DbUtil.getConnect(); try { List<User> list = DbUtil.queryRunner().query( conn, "select * from tp_user", new BeanListHandler<User>(User.class)); for (User u : list) { System.out.println(u.getUsername()); } } catch (SQLException e) { e.printStackTrace(); } } /** * 获取单个 * * @return */ public static void getOne() { Connection conn = DbUtil.getConnect(); User u; try { u = DbUtil.queryRunner().query(conn, "select * from tp_user where id=?", new BeanHandler<User>(User.class), 1); System.out.println(u.getUsername()); } catch (SQLException e) { e.printStackTrace(); } } }
DbUtil.java
package utils; import java.sql.Connection; import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DbUtil { private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); public static Connection getConnect() { Connection conn = null; if(conn == null) { try { conn = dataSource.getConnection(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return conn; } public static QueryRunner queryRunner() { return new QueryRunner(dataSource); } }
User.java
package entity; public class User { private int id; private String username; private String password; private String date_entered; private int status; private String update_entered; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDate_entered() { return date_entered; } public void setDate_entered(String dateEntered) { date_entered = dateEntered; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public String getUpdate_entered() { return update_entered; } public void setUpdate_entered(String updateEntered) { update_entered = updateEntered; } }