yyg1107
這家伙很懶,什么都沒有留下
posts(15)
comments(20)
trackbacks(0)
BlogJava
聯系
聚合
管理
公告
聯系方式:
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
給我留言
查看公開留言
查看私人留言
隨筆分類
(11)
axis(1)
hibernate(1)
java(2)
js(2)
other(5)
隨筆檔案
(8)
2008年6月 (1)
2007年8月 (2)
2007年4月 (3)
2006年11月 (2)
文章分類
(37)
C++Builder(3)
eclipse(2)
html(4)
java(8)
jbpm(6)
JS(4)
other(4)
question handly!(4)
web容器(1)
workflow(1)
文章檔案
(36)
2007年4月 (2)
2007年3月 (1)
2007年1月 (2)
2006年12月 (2)
2006年11月 (3)
2006年10月 (11)
2006年9月 (2)
2006年7月 (2)
2006年6月 (1)
2006年5月 (1)
2006年4月 (6)
2006年3月 (3)
新聞檔案
(1)
2007年1月 (1)
收藏夾
(16)
好的BLOG(9)
收藏----blog(7)
JavaBlog
amigoxie
matrix
搜索
積分與排名
積分 - 32832
排名 - 1317
最新評論
1.?re: JS實現文本框輸入提供選擇框的提示功能-1
werewrewrwer
--wrewrwe
2.?re: struts啟動的一個錯誤!
更名struts-config.xml, remove && add struts capability,重新生成一個config.xml。
--barryken
3.?p
42
--2
4.?re: hql語句----隨機查詢取頭10條記錄[未登錄]
沒意思,這不就是分頁的那一部分代碼嘛!
--啊啊
5.?re: 第一次做成功的AXIS例子[未登錄]
樓主:能不能留下你的聯系方式!探討一下相關問題...
--hehe
閱讀排行榜
1.?JS實現文本框輸入提供選擇框的提示功能-1(7298)
2.?第一次做成功的AXIS例子(2432)
3.?命令行創建ODBC數據源(1546)
4.?struts啟動的一個錯誤!(1266)
5.?hibernate調用存儲過程例子(1008)
評論排行榜
1.?JS實現文本框輸入提供選擇框的提示功能-1(7)
2.?第一次做成功的AXIS例子(5)
3.?struts啟動的一個錯誤!(4)
4.?轉:如何在JAVA中使用日期 (0)
5.?命令行創建ODBC數據源(0)
View Post
JDBC數據庫連接池
ConnectionHandler.java
package
?com.ofis.DBConnection;
import
?java.lang.reflect.
*
;
import
?java.sql.
*
;
import
?org.apache.xalan.lib.sql.ConnectionPool;
/**?*/
/**
?*?
?*?數據庫連接的代理類
?*
?
*/
class
?ConnectionHandler?
implements
?InvocationHandler?
{
????Connection?dbconn;
????ConnectionPool?pool;
????
/**?*/
/**
?????*?構造函數
?????*?
@param
?connPool
?????
*/
????
public
?ConnectionHandler(ConnectionPool?connPool)?
{
????????
this
.pool?
=
?connPool;
????}
????
/**?*/
/**
?????*?
?????*?動態綁定Connection
?????*?
@param
?conn?Connection
?????*?
@return
?Connection
?????*?
?????
*/
????
public
?Connection?bind(Connection?conn)?
{
????????Class[]?interfaces?
=
?conn.getClass().getInterfaces();
????????
if
?(interfaces?
==
?
null
?
||
?interfaces.length?
==
?
0
)?
{
????????????interfaces?
=
?
new
?Class[
1
];
????????????interfaces[
0
]?
=
?Connection.
class
;
????????}
????????Connection?proxyConn?
=
?(Connection)?Proxy.newProxyInstance(conn.
????????????????getClass().getClassLoader(),?interfaces,?
this
);
????????
return
?conn;
????}
????
/**?*/
/**
?????*?調用攔截函數
?????
*/
????
public
?Object?invoke(Object?object,?Method?method,?Object[]?objectArray)?
throws
????????????Throwable?
{
????????Object?obj?
=
?
null
;
????????
if
?(
"
close
"
.equals(method.getName()))?
{
????????????pool.releaseConnection(dbconn);
????????}
?
else
?
{
????????????obj?
=
?method.invoke(dbconn,?objectArray);
????????}
????????
return
?obj;
????}
}
DBConnectionPool.java
package
?com.ofis.DBConnection;
import
?org.apache.xalan.lib.sql.ConnectionPool;
import
?java.sql.
*
;
import
?java.sql.SQLException;
import
?java.util.Properties;
import
?java.util.Vector;
/**?*/
/**
?*?數據庫連接池控制
?*
?
*/
public
?
class
?DBConnectionPool?
implements
?ConnectionPool
{
????
private
?
static
?Vector?pool;
????
private
?
final
?
int
?POOL_MAX_SIZE
=
40
;
????
static
?String?strDriver?
=
?
"
net.sourceforge.jtds.jdbc.Driver
"
;
????
static
?String?strURL?
=
"
jdbc:jtds:sqlserver://localhost:1433/template
"
;
????
public
?DBConnectionPool()?
{
????}
????
public
?
boolean
?isEnabled()?
{
????????
return
?
false
;
????}
????
public
?
void
?setDriver(String?string)?
{
????????
this
.strDriver?
=
?string;
????}
????
public
?
void
?setURL(String?string)?
{
????????
this
.strURL
=
string;
????}
????
public
?
void
?freeUnused()?
{
????}
????
public
?
boolean
?hasActiveConnections()?
{
????????
return
?
false
;
????}
????
public
?
void
?setPassword(String?string)?
{
????}
????
public
?
void
?setUser(String?string)?
{
????}
????
public
?
void
?setMinConnections(
int
?_int)?
{
????}
????
public
?
boolean
?testConnection()?
{
????????
return
?
false
;
????}
????
/**?*/
/**
?????*?得到數據庫連接操作
?????
*/
????
public
?
synchronized
?Connection?getConnection()?
throws
?SQLException?
{
????????
if
(pool
==
null
)
{
????????????pool
=
new
?Vector();
????????}
????????Connection?conn;
????????
if
(pool.isEmpty())
{
????????????conn
=
createConnection();
????????}
else
{
????????????
int
?last_idx
=
pool.size()
-
1
;
????????????conn
=
(Connection)pool.get(last_idx);
????????????pool.remove(pool.get(last_idx));
????????}
????????ConnectionHandler?connHandler
=
new
?ConnectionHandler(
this
);
????????
return
?connHandler.bind(conn);
????}
????
/**?*/
/**
?????*?釋放數據庫連接操作
?????
*/
????
public
?
synchronized
?
void
?releaseConnection(Connection?connection)?
throws
?SQLException?
{
????????
if
(pool.size()
>
POOL_MAX_SIZE)
{
????????????
try
?
{
????????????????connection.close();
????????????}
?
catch
?(SQLException?ex)?
{
????????????????ex.printStackTrace();
????????????}
????????}
else
{
????????????pool.add(connection);
????????}
????}
????
/**?*/
/**
?????*?創建數據庫連接操作
?????*?
@return
?????
*/
????
private
?
static
?Connection?createConnection()
{
????????Connection?conn;
????????
try
?
{
????????????Class.forName(strDriver);
????????????conn?
=
?DriverManager.getConnection(strURL,?
"
sa
"
,?
""
);
????????????
return
?conn;
????????}
?
catch
?(SQLException?ex)?
{
????????????
return
?
null
;
????????}
?
catch
?(ClassNotFoundException?ex)?
{
????????????
return
?
null
;
????????}
????}
????
public
?
void
?releaseConnectionOnError(Connection?connection)?
throws
????????????SQLException?
{
????}
????
public
?
void
?setPoolEnabled(
boolean
?_boolean)?
{
????}
????
public
?
void
?setProtocol(Properties?properties)?
{
????}
}
DBHelper.java
package
?com.ofis.DBConnection;
import
?java.sql.
*
;
/**?*/
/**
?*?數據庫連接操作
?*
?
*/
public
?
class
?DBHelper?
{
????
/**?*/
/**
?????*?得到數據庫連接
?????*?
@return
?????
*/
????
public
?
static
?Connection?getConnection()?
{
????????DBConnectionPool?cp
=
new
?DBConnectionPool();
????????cp.setDriver(
"
net.sourceforge.jtds.jdbc.Driver
"
);
????????cp.setURL(
"
jdbc:jtds:sqlserver://localhost:1433/template
"
);
????????Connection?conn?
=
?
null
;
????????
try
?
{
????????????conn?
=
?cp.getConnection();
????????}
?
catch
?(SQLException?ex)?
{
????????}
????????
return
?conn;
??}
????
/**?*/
/**
?????*?釋放數據庫連接
?????*?
@param
?conn?
?????
*/
????
public
?
static
?
void
?releaseConnection(Connection?conn)
{
????????DBConnectionPool?cp
=
new
?DBConnectionPool();
????????
try
?
{
????????????cp.releaseConnection(conn);
????????}
?
catch
?(SQLException?ex)?
{
????????}
????}
}
DBHelperTest.java
package
?com.ofis.DBConnection;
import
?java.sql.Connection;
import
?java.sql.ResultSet;
import
?java.sql.SQLException;
import
?java.sql.Statement;
import
?com.ofis.DBConnection.DBHelper;
import
?junit.framework.TestCase;
public
?
class
?DBHelperTest?
extends
?TestCase?
{
????
/**/
/*
?????*?Test?method?for?'com.ofis.DBConnection.DBHelper.getConnection()'
?????
*/
????
public
?
void
?testGetConnection()?
{
????????Connection?h1?
=
?DBHelper.getConnection()?;
????????Connection?h2?
=
?DBHelper.getConnection()?;
????????Connection?h3?
=
?DBHelper.getConnection()?;
????????Connection?h4?
=
?DBHelper.getConnection()?;
????????Connection?h5?
=
?DBHelper.getConnection()?;
????????
try
{
????????????Statement?stmt?
=
?h1.createStatement();
????????????ResultSet?rs?
=
?stmt.executeQuery(
"
select?*?from?tbusers
"
);
????????????
if
(rs.next())
????????????
{
????????????????System.out.println(rs.getString(
"
user_truename
"
));
????????????}
????????}
????????
catch
(SQLException?ex)
{
????????????System.err.println(ex.getMessage());
????????}
????????
????????
finally
?
{
????????????
if
?(h1?
!=
?
null
)?
{
????????????????DBHelper.releaseConnection(h1);
????????????}
????????????
if
?(h2?
!=
?
null
)?
{
????????????????DBHelper.releaseConnection(h2);
????????????}
????????????
if
?(h3?
!=
?
null
)?
{
????????????????DBHelper.releaseConnection(h3);
????????????}
????????????
if
?(h4?
!=
?
null
)?
{
????????????????DBHelper.releaseConnection(h4);
????????????}
????????????
if
?(h5?
!=
?
null
)?
{
????????????????DBHelper.releaseConnection(h5);
????????????}
????????????testCon();
????????????
????????}
????}
????
public
?
void
?testCon()
{
????????Connection?h1?
=
?DBHelper.getConnection()?;
????????Connection?h2?
=
?DBHelper.getConnection()?;
????????
try
{
????????????Statement?stmt?
=
?h1.createStatement();
????????????ResultSet?rs?
=
?stmt.executeQuery(
"
select?*?from?tbusers
"
);
????????????
if
(rs.next())
????????????
{}
????????}
????????
catch
(SQLException?ex)
{
????????????System.err.println(ex.getMessage());
????????}
????????
????????
finally
?
{
????????????
if
?(h1?
!=
?
null
)?
{
????????????????DBHelper.releaseConnection(h1);
????????????}
????????????
if
?(h2?
!=
?
null
)?
{
????????????????DBHelper.releaseConnection(h2);
????????????}
//
????????????if?(h3?!=?null)?{
//
????????????????DBHelper.releaseConnection(h3);
//
????????????}
//
????????????if?(h4?!=?null)?{
//
????????????????DBHelper.releaseConnection(h4);
//
????????????}
//
????????????if?(h5?!=?null)?{
//
????????????????DBHelper.releaseConnection(h5);
//
????????????}
????????????
????????}
????}
}
?
?
posted on 2006-09-30 13:42
young
閱讀(308)
評論(0)
編輯
收藏
所屬分類:
java
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
Hibernate Gossip: 樂觀鎖定(Optimistic locking)
轉:如何在JAVA中使用日期
Jakarta Commons:巧用類和組件1 (3)
Jakarta Commons:巧用類和組件1 (2)
Jakarta Commons:巧用類和組件1 (1)
Struts VS Turbine [轉]
JDBC數據庫連接池
Powered by:
BlogJava
Copyright © young
主站蜘蛛池模板:
会昌县
|
洞口县
|
双牌县
|
佳木斯市
|
西乌珠穆沁旗
|
河南省
|
武安市
|
呈贡县
|
勃利县
|
兴国县
|
南溪县
|
香港
|
肥城市
|
都兰县
|
互助
|
黎城县
|
交口县
|
敦煌市
|
观塘区
|
青龙
|
乡城县
|
儋州市
|
宜章县
|
南平市
|
五家渠市
|
娄烦县
|
井研县
|
池州市
|
左贡县
|
河曲县
|
邛崃市
|
弋阳县
|
海晏县
|
农安县
|
蒙阴县
|
开远市
|
格尔木市
|
承德市
|
香河县
|
虹口区
|
双城市
|