单独使用 commons-dbcp 连接池

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

dbcp 是 apache 上的一个 java 连接池项目,也是 tomcat 使用的连接池组件。单独使用dbcp需要3个包:common-dbcp.jar ,common-pool.jar,common-collections.jar。

下面是个dbcp的实用类,通过它可以完成DBCP的使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
/**
 * @author space
 * @date Aug 12, 2008 3:25:49 PM
 *
 * dbcp 实用类,提供了dbcp连接,不允许继承;
 *
 * 该类需要有个地方来初始化 DS ,通过调用initDS 方法来完成,
 * 可以在通过调用带参数的构造函数完成调用,
 * 可以在其它类中调用,也可以在本类中加一个static{}来完成;
 */
public final class DbcpBean {
 /** 数据源,static */
 private static DataSource DS;
 /** 从数据源获得一个连接 */
 public Connection getConn() {
  Connection con = null;
  if (Ds!= null) {
   try {
    con = Ds.getConnection();
   } catch (Exception e) {
    e.printStackTrace(System.err);
   }
 
   try {
    con.setAutoCommit(false);
   } catch (SQLException e) {
    e.printStackTrace();
   }
   return con;
  }
  return con;
 }
 /** 默认的构造函数 */
 public DbcpBean() {
 }
 /** 构造函数,初始化了 DS ,指定 数据库 */
 public DbcpBean(String connectURI) {
  initDS(connectURI);
 }
 /** 构造函数,初始化了 DS ,指定 所有参数 */
 public DbcpBean(String connectURI, String username, String pswd, String driverClass, int initialSize,
   int maxActive, int maxIdle, int maxWait) {
  initDS(connectURI, username, pswd, driverClass, initialSize, maxActive, maxIdle, maxWait);
 }
 /**
  * 创建数据源,除了数据库外,都使用硬编码默认参数;
  *
  * @param connectURI 数据库
  * @return
  */
 public static void initDS(String connectURI) {
  initDS(connectURI, "root", "password", "com.mysql.jdbc.Driver", 5, 100, 30, 10000);
 }
 /**
  * 指定所有参数连接数据源
  *
  * @param connectURI 数据库
  * @param username 用户名
  * @param pswd 密码
  * @param driverClass 数据库连接驱动名
  * @param initialSize 初始连接池连接个数
  * @param maxActive 最大激活连接数
  * @param maxIdle 最大闲置连接数
  * @param maxWait 获得连接的最大等待毫秒数
  * @return
  */
 public static void initDS(String connectURI, String username, String pswd,
    String driverClass, int initialSize,
    int maxActive, int maxIdle, int maxWait) {
  BasicDataSource ds = new BasicDataSource();
  ds.setDriverClassName(driverClass);
  ds.setUsername(username);
  ds.setPassword(pswd);
  ds.setUrl(connectURI);
  ds.setInitialSize(initialSize); // 初始的连接数;
  ds.setMaxActive(maxActive);
  ds.setMaxIdle(maxIdle);
  ds.setMaxWait(maxWait);
  DS = ds;
 }
 /** 获得数据源连接状态 */
 public static Map<String, Integer> getDataSourceStats() throws SQLException {
  BasicDataSource bds = (BasicDataSource) DS;
  Map<String, Integer> map = new HashMap<String, Integer>(2);
  map.put("active_number", bds.getNumActive());
  map.put("idle_number", bds.getNumIdle());
  return map;
 }
 /** 关闭数据源 */
 protected static void shutdownDataSource() throws SQLException {
  BasicDataSource bds = (BasicDataSource) DS;
  bds.close();
 }
 public static void main(String[] args) {
  DbcpBean db = new DbcpBean("jdbc:<a href="mysql://localhost:3306/testit">mysql://localhost:3306/testit");
  Connection conn = null;
  Statement stmt = null;
  ResultSet rs = null;
  try {
   conn = db.getConn();
   stmt = conn.createStatement();
   rs = stmt.executeQuery("select * from test limit 1 ");
   System.out.println("Results:");
   int numcols = rs.getMetaData().getColumnCount();
   while (rs.next()) {
    for (int i = 1; i <= numcols; i++) {
     System.out.print("\\t" + rs.getString(i) + "\\t");
    }
    System.out.println("");
   }
   System.out.println(getDataSourceStats());
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   try {
    if (rs != null)
     rs.close();
    if (stmt != null)
     stmt.close();
    if (conn != null)
     conn.close();
    if (db != null)
     shutdownDataSource();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }
}