apache dbcp數(shù)據(jù)庫連接池的使用
?
??1
public
?
class
?DaoUtil?
{
??2
??3
????
/**?*/
/**
??4
?????*?數(shù)據(jù)庫連接池
??5
?????*?
??6
?????*?
@see
?
http://jakarta.apache.org/commons/dbcp/index.html
??7
?????
*/
??8
????
private
?
static
?PoolingDriver?driver?
=
?
null
;
??9
?10
????
/**?*/
/**
?11
?????*?設(shè)置一個數(shù)據(jù)庫連接池
?12
?????*?
?13
?????*?
@param
?name
?14
?????*????????????連接池的名稱
?15
?????*?
@param
?url
?16
?????*????????????數(shù)據(jù)源
?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
?????????????????*?首先創(chuàng)建一個對象池來保存數(shù)據(jù)庫連接?使用?commons.pool?的?GenericObjectPool對象
?25
?????????????????
*/
?26
????????????????ObjectPool?connectionPool?
=
?
new
?GenericObjectPool();
?27
????????????????
/**?*/
/**
?28
?????????????????*?創(chuàng)建一個?DriverManagerConnectionFactory對象?連接池將用它來獲取一個連接
?29
?????????????????
*/
?30
????????????????ConnectionFactory?connectionFactory?
=
?
new
?DriverManagerConnectionFactory(
?31
????????????????????????url,?
null
);
?32
????????????????
/**?*/
/**
?33
?????????????????*?創(chuàng)建一個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
?????*?關(guān)閉所有數(shù)據(jù)庫連接池
?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
?????*?取得一個數(shù)據(jù)庫連接對象。
?72
?????*?
?73
?????*?因為可能使用兩個不同的數(shù)據(jù)庫,?所以依據(jù)report的值來確定使用那個數(shù)據(jù)庫。
?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驅(qū)動
?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(
"
無法裝入數(shù)據(jù)庫引擎
"
);
?94
????????}
?
catch
?(SQLException?sqle)?
{
?95
????????????
throw
?
new
?RuntimeException(
"
無法打開數(shù)據(jù)庫連接
"
);
?96
????????}
?97
????}
?98
?99
????
/**?*/
/**
100
?????*?執(zhí)行清理過程
101
?????*?<li>關(guān)閉數(shù)據(jù)庫連接</li>
102
?????*?<li>關(guān)閉語句對象</li>
103
?????*?<li>關(guān)閉結(jié)果集</li>
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
}



??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

138

139

140

141

142

143

144

145

146

posted on 2006-07-17 11:19 javajohn 閱讀(3842) 評論(1) 編輯 收藏 所屬分類: 數(shù)據(jù)庫