作者 : manboo
說明:
此連接池有三個類和一個接口組成
DBConnectionPool 數據庫連接池
PoolConnection 一個實現了java
theOnClose 實現了接口OnConnectionClose的一個類用還處理釋放數據庫連接的是動作決定是關閉數據庫還是返回池中
接口 :
OnConnectionClose:此接口是一個聲明
本來要上傳這幾個小類的UML圖的但是我一點IE就死
代碼:
package DBTools;
/**
* <p>T數據庫連接池工具 </p>
* <p>模擬
* <p>Copyright: 可以隨便使用
* <p>Company:自己作品 </p>
* @author董平雷
* @version
*/
import java
import java
import java
interface OnConnectionClose {
public void Action(PoolConnection sender);
}
public class DBConnectionPool {
private static Vector pConnectionVector = new Vector();
// private static int Count=
private static int minCount =
private static String URL =
private static String User =
private static String Password =
private static String DriverName=
synchronized public static void setMinCount(int Value) {
minCount = Value;
}
synchronized public static int getMinCount() {
return minCount;
}
synchronized public static int getCout() {
return pConnectionVector
}
synchronized public static Connection getConnection() throws SQLException {
PoolConnection pConnection = null;
// int aCount=pConnectionVector
for (int I =
Object oCon = pConnectionVector
if (oCon instanceof PoolConnection) {
PoolConnection aCon = (PoolConnection) oCon;
if (!aCon
pConnection = aCon;
break;
}
}
}
if (pConnection == null) {
pConnection = getNewConnection();
pConnectionVector
}
return pConnection;
}
private static PoolConnection getNewConnection() throws SQLException {
try
{
Class
}catch(ClassNotFoundException ex)
{
ex
}
PoolConnection con = new PoolConnection(URL
con
return con;
}
synchronized public static void SetJDBC(String url
URL = url;
User = user;
Password = password;
}
synchronized public static void setURL(String url) {
URL = url;
}
synchronized public static String getUrl() {
return URL;
}
synchronized public static void setUser(String user)
{
User=user;
}
synchronized public static String getUser()
{
return User;
}
synchronized public static void setPassword(String password)
{
Password=password;
}
synchronized public static String getPassword()
{
return Password;
}
synchronized public static void setDriverName(String dName)
{
DriverName=dName;
}
synchronized public static String getDriverName()
{
return DriverName;
}
}
class theOnClose
implements OnConnectionClose {
private Vector v;
public theOnClose(Vector vt) {
v = vt;
}
public void Action(PoolConnection sender) {
v
}
}
class PoolConnection
implements Connection
private Connection aCon = null;
private boolean closed = false;
private boolean inUse = false;
private String DriverName;
private OnConnectionClose onClose = null;
private void writeObject(ObjectOutputStream oos) throws IOException {
oos
}
private void readObject(ObjectInputStream ois) throws ClassNotFoundException
ois
}
protected PoolConnection() {
}
public PoolConnection(String Url
SQLException {
aCon = DriverManager
closed = false;
inUse=true;
}
public PoolConnection(String Url) throws Exception {
aCon = DriverManager
closed = false;
inUse=true;
}
public Statement createStatement() throws SQLException {
/**@todo Implement this java
//throw new java
return aCon
}
public PreparedStatement prepareStatement(String sql) throws SQLException {
/**@todo Implement this java
//throw new java
return aCon
}
public CallableStatement prepareCall(String sql) throws SQLException {
/**@todo Implement this java
//throw new java
return aCon
}
public String nativeSQL(String sql) throws SQLException {
/**@todo Implement this java
// throw new java
return aCon
}
public void setAutoCommit(boolean autoCommit) throws SQLException {
/**@todo Implement this java
// throw new java
aCon
}
public boolean getAutoCommit() throws SQLException {
/**@todo Implement this java
// throw new java
return aCon
}
public void commit() throws SQLException {
/**@todo Implement this java
// throw new java
mit();
}
public void rollback() throws SQLException {
/**@todo Implement this java
//throw new java
aCon
}
public void close() throws SQLException {
/**@todo Implement this java
//throw new java
if(DBConnectionPool
{
inUse = false;
}else
{
closeConnection();
}
}
public boolean isClosed() throws SQLException {
/**@todo Implement this java
//throw new java
return closed;
}
public DatabaseMetaData getMetaData() throws SQLException {
/**@todo Implement this java
// throw new java
return aCon
}
public void setReadOnly(boolean readOnly) throws SQLException {
/**@todo Implement this java
// throw new java
aCon
}
public boolean isReadOnly() throws SQLException {
/**@todo Implement this java
// throw new java
return isReadOnly();
}
public void setCatalog(String catalog) throws SQLException {
/**@todo Implement this java
// throw new java
aCon
}
public String getCatalog() throws SQLException {
/**@todo Implement this java
//throw new java
return aCon
}
public void setTransactionIsolation(int level) throws SQLException {
/**@todo Implement this java
// throw new java
aCon
}
public int getTransactionIsolation() throws SQLException {
/**@todo Implement this java
// throw new java
return aCon
}
public SQLWarning getWarnings() throws SQLException {
/**@todo Implement this java
// throw new java
return aCon
}
public void clearWarnings() throws SQLException {
/**@todo Implement this java
//throw new java
aCon
}
public Statement createStatement(int resultSetType
SQLException {
/**@todo Implement this java
// throw new java
return aCon
}
public PreparedStatement prepareStatement(String sql
int resultSetConcurrency) throws
SQLException {
/**@todo Implement this java
// throw new java
return aCon
}
public CallableStatement prepareCall(String sql
int resultSetConcurrency) throws
SQLException {
/**@todo Implement this java
//throw new java
return aCon
}
public Map getTypeMap() throws SQLException {
/**@todo Implement this java
//throw new java
return aCon
}
public void setTypeMap(Map map) throws SQLException {
/**@todo Implement this java
//throw new java
aCon
}
public void setHoldability(int holdability) throws SQLException {
/**@todo Implement this java
// throw new java
aCon
}
public int getHoldability() throws SQLException {
/**@todo Implement this java
// throw new java
return aCon
}
public Savepoint setSavepoint() throws SQLException {
/**@todo Implement this java
//throw new java
return setSavepoint();
}
public Savepoint setSavepoint(String name) throws SQLException {
/**@todo Implement this java
// throw new java
return setSavepoint(name);
}
public void rollback(Savepoint savepoint) throws SQLException {
/**@todo Implement this java
//throw new java
aCon
}
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
/**@todo Implement this java
// throw new java
aCon
}
public Statement createStatement(int resultSetType
int resultSetHoldability) throws
SQLException {
/**@todo Implement this java
//throw new java
return aCon
resultSetHoldability);
}
public PreparedStatement prepareStatement(String sql
int resultSetConcurrency
int resultSetHoldability) throws
SQLException {
/**@todo Implement this java
// throw new java
return aCon
resultSetHoldability);
}
public CallableStatement prepareCall(String sql
int resultSetConcurrency
int resultSetHoldability) throws
SQLException {
/**@todo Implement this java
//throw new java
return aCon
resultSetHoldability);
}
public PreparedStatement prepareStatement(String sql
SQLException {
/**@todo Implement this java
//throw new java
return aCon
}
public PreparedStatement prepareStatement(String sql
SQLException {
/**@todo Implement this java
// throw new java
return aCon
}
public PreparedStatement prepareStatement(String sql
SQLException {
/**@todo Implement this java
//throw new java
return aCon
}
public void closeConnection() throws SQLException {
if (onClose != null) {
onClose
}
aCon
}
public boolean isUsed() {
return inUse;
}
public void use() {
inUse = true;
}
public void setOnClose(OnConnectionClose Action) {
onClose = Action;
}
}
以上就是我所寫的連接池代碼
使用方法:
DBTools
DBTools
java
當使用完畢了別忘記將con關閉:)
好像現在使用java的人不允許人說java的問題
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26842.html