apache dbcp数据库连接池的使用
2008-01-04 00:26
716 查看
导读:
1
public class DaoUtil
{
2
3
/** */ /**
4
* 数据库连接池
5
*
6
* @see http://jakarta.apache.org/commons/dbcp/index.html
7
*/
8
private static PoolingDriver driver = null ;
9
10
/** */ /**
11
* 设置一个数据库连接池
12
*
13
* @param name
14
* 连接池的名称
15
* @param url
16
* 数据源
17
* @throws SQLException
18
*/
19
private static void setUpDriverPool(String name, String url)
20
throws SQLException
{
21
if ((driver == null ) || driver.getPoolNames().length < 2 )
{
22
try
{
23
/** */ /**
24
* 首先创建一个对象池来保存数据库连接 使用 commons.pool 的 GenericObjectPool对象
25
*/
26
ObjectPool connectionPool = new GenericObjectPool();
27
/** */ /**
28
* 创建一个 DriverManagerConnectionFactory对象 连接池将用它来获取一个连接
29
*/
30
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
31
url, null );
32
/** */ /**
33
* 创建一个PoolableConnectionFactory 对象。
34
*/
35
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
36
connectionFactory, connectionPool, null , null , false ,
37
true );
38
/** */ /**
39
* 注册PoolingDriver。
40
*/
41
Class.forName( " org.apache.commons.dbcp.PoolingDriver " );
42
driver = (PoolingDriver) DriverManager.getDriver( " jdbc:apache:commons:dbcp: " );
43
driver.registerPool(name, connectionPool);
44
} catch (ClassNotFoundException e)
{
45
throw new RuntimeException(e);
46
}
47
}
48
}
49
50
/** */ /**
51
* 关闭所有数据库连接池
52
*
53
*/
54
public static void shutDownDriver()
{
55
56
try
{
57
PoolingDriver driver = (PoolingDriver) DriverManager
58
.getDriver( " jdbc:apache:commons:dbcp: " );
59
String[] poolNames = driver.getPoolNames();
60
if (poolNames.length > 1 )
{
61
for ( int i = 0 ; i < poolNames.length; i ++ )
{
62
driver.closePool( " pool " );
63
}
64
}
65
} catch (SQLException sqle)
{
66
throw new RuntimeException(sqle);
67
}
68
}
69
70
/** */ /**
71
* 取得一个数据库连接对象。
72
*
73
* 因为可能使用两个不同的数据库, 所以依据report的值来确定使用那个数据库。
74
*
75
* @param report
76
* @return
77
*/
78
public static Connection getConnection()
{
79
Connection con = null ;
80
try
{
81
ReadConfig readConfig = new ReadConfig();
82
readConfig.init( null );
83
// 装载mysql的jdbc驱动
84
String driver = readConfig.getDBDriver();
85
String url = readConfig.getDBUrl();
86
String poolName = " pool " ;
87
Class.forName(driver);
88
setUpDriverPool(poolName, url);
89
con = DriverManager.getConnection( " jdbc:apache:commons:dbcp: "
90
+ poolName);
91
return con;
92
} catch (ClassNotFoundException cnfe)
{
93
throw new RuntimeException( " 无法装入数据库引擎 " );
94
} catch (SQLException sqle)
{
95
throw new RuntimeException( " 无法打开数据库连接 " );
96
}
97
}
98
99
/** */ /**
100
* 执行清理过程
101
* 关闭数据库连接
102
* 关闭语句对象
103
* 关闭结果集
104
*
105
* @param con
106
* @param s
107
* @param rs
108
*/
109
public static void closeAll(Connection con, Statement s, ResultSet rs)
{
110
try
{
111
if (rs != null )
{
112
rs.close();
113
rs = null ;
114
}
115
if (s != null )
{
116
s.close();
117
s = null ;
118
}
119
if (con != null )
{
120
con.close();
121
con = null ;
122
}
123
} catch (SQLException sqle)
{
124
// nothing to do, forget it;
125
}
126
}
127
128
public static void main(String[] args)
{
129
// DaoUtil daoUtil = new DaoUtil();
130
// Connection connection = null;
131
// Statement statement = null;
132
// connection = daoUtil.getConnection();
133
// ResultSet rs = null;
134
// try {
135
// statement = connection.createStatement();
136
// rs = statement.executeQuery("select * from admin");
137
// while(rs.next()){
138
// System.out.println(rs.getString("adminName"));
139
// }
140
// } catch (SQLException e) {
141
// e.printStackTrace();
142
// }
143
144
}
145
146
}
本文转自
http://www.blogjava.net/javajohn/archive/2006/07/17/58532.html
1
public class DaoUtil
{
2
3
/** */ /**
4
* 数据库连接池
5
*
6
* @see http://jakarta.apache.org/commons/dbcp/index.html
7
*/
8
private static PoolingDriver driver = null ;
9
10
/** */ /**
11
* 设置一个数据库连接池
12
*
13
* @param name
14
* 连接池的名称
15
* @param url
16
* 数据源
17
* @throws SQLException
18
*/
19
private static void setUpDriverPool(String name, String url)
20
throws SQLException
{
21
if ((driver == null ) || driver.getPoolNames().length < 2 )
{
22
try
{
23
/** */ /**
24
* 首先创建一个对象池来保存数据库连接 使用 commons.pool 的 GenericObjectPool对象
25
*/
26
ObjectPool connectionPool = new GenericObjectPool();
27
/** */ /**
28
* 创建一个 DriverManagerConnectionFactory对象 连接池将用它来获取一个连接
29
*/
30
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
31
url, null );
32
/** */ /**
33
* 创建一个PoolableConnectionFactory 对象。
34
*/
35
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
36
connectionFactory, connectionPool, null , null , false ,
37
true );
38
/** */ /**
39
* 注册PoolingDriver。
40
*/
41
Class.forName( " org.apache.commons.dbcp.PoolingDriver " );
42
driver = (PoolingDriver) DriverManager.getDriver( " jdbc:apache:commons:dbcp: " );
43
driver.registerPool(name, connectionPool);
44
} catch (ClassNotFoundException e)
{
45
throw new RuntimeException(e);
46
}
47
}
48
}
49
50
/** */ /**
51
* 关闭所有数据库连接池
52
*
53
*/
54
public static void shutDownDriver()
{
55
56
try
{
57
PoolingDriver driver = (PoolingDriver) DriverManager
58
.getDriver( " jdbc:apache:commons:dbcp: " );
59
String[] poolNames = driver.getPoolNames();
60
if (poolNames.length > 1 )
{
61
for ( int i = 0 ; i < poolNames.length; i ++ )
{
62
driver.closePool( " pool " );
63
}
64
}
65
} catch (SQLException sqle)
{
66
throw new RuntimeException(sqle);
67
}
68
}
69
70
/** */ /**
71
* 取得一个数据库连接对象。
72
*
73
* 因为可能使用两个不同的数据库, 所以依据report的值来确定使用那个数据库。
74
*
75
* @param report
76
* @return
77
*/
78
public static Connection getConnection()
{
79
Connection con = null ;
80
try
{
81
ReadConfig readConfig = new ReadConfig();
82
readConfig.init( null );
83
// 装载mysql的jdbc驱动
84
String driver = readConfig.getDBDriver();
85
String url = readConfig.getDBUrl();
86
String poolName = " pool " ;
87
Class.forName(driver);
88
setUpDriverPool(poolName, url);
89
con = DriverManager.getConnection( " jdbc:apache:commons:dbcp: "
90
+ poolName);
91
return con;
92
} catch (ClassNotFoundException cnfe)
{
93
throw new RuntimeException( " 无法装入数据库引擎 " );
94
} catch (SQLException sqle)
{
95
throw new RuntimeException( " 无法打开数据库连接 " );
96
}
97
}
98
99
/** */ /**
100
* 执行清理过程
101
* 关闭数据库连接
102
* 关闭语句对象
103
* 关闭结果集
104
*
105
* @param con
106
* @param s
107
* @param rs
108
*/
109
public static void closeAll(Connection con, Statement s, ResultSet rs)
{
110
try
{
111
if (rs != null )
{
112
rs.close();
113
rs = null ;
114
}
115
if (s != null )
{
116
s.close();
117
s = null ;
118
}
119
if (con != null )
{
120
con.close();
121
con = null ;
122
}
123
} catch (SQLException sqle)
{
124
// nothing to do, forget it;
125
}
126
}
127
128
public static void main(String[] args)
{
129
// DaoUtil daoUtil = new DaoUtil();
130
// Connection connection = null;
131
// Statement statement = null;
132
// connection = daoUtil.getConnection();
133
// ResultSet rs = null;
134
// try {
135
// statement = connection.createStatement();
136
// rs = statement.executeQuery("select * from admin");
137
// while(rs.next()){
138
// System.out.println(rs.getString("adminName"));
139
// }
140
// } catch (SQLException e) {
141
// e.printStackTrace();
142
// }
143
144
}
145
146
}
本文转自
http://www.blogjava.net/javajohn/archive/2006/07/17/58532.html
相关文章推荐
- Apache DBCP数据库连接池使用Demo
- 使用dbcp做为数据库连接池
- tomcat-dbcp数据库连接池配置以及使用时候的一些坑
- JDBC(java数据库连接技术)和DBCP(数据库连接池)联合使用
- 03_dbcp数据源依赖jar包,DBCP中API介绍,不同过dbcp方式使用dbcp数据库连接池,通过配置文件使用dbcp数据库连接池
- 数据库连接池(数据源)2: 开源数据源的使用DBCP和C3P0
- 使用Apache DBCP连接池重构DBUtility
- JAVA使用DBCP创建数据库连接池及测试
- dbcp的简单使用_数据库连接池
- 使用dbcp数据库连接池连接数据库aaa的代码
- 使用dbcp构建数据库连接池
- DBCP数据库连接池的简单使用
- DBCP数据库连接池笔记-基本使用
- DBCP使用apache
- DBCP数据库连接池的使用
- DBCP和Druid数据库连接池使用
- Tomcat中内置DBCP数据库连接池使用
- 使用MyEclipse整合Struts+Spring+Hibernate时,在“applicationContext.xml”中提示class"org.apache.commons.dbcp.Basi
- JDBC实例--工具类升级,使用Apache DBCP连接池重构DBUtility,让连接数据库更有效,更安全
- 数据库连接池c3p0和dbcp中spring中使用的配置