java中连接池的实现

自己实现一个连接池 关键是要控制连接的关闭和读取

步骤1, 实现 Connection 接口 并覆盖里面的close()  方法.

 

package  sky.sql;

import  java.sql. * ;
import  java.util. * ;

public   class  MyConn  implements  Connection  {
    sky.sql.ConnectionPool pool;
    Connection con;
    
public MyConn(Connection con , ConnectionPool pool) {
        
this.con = con;
        
this.pool = pool;
    }


    
public int getHoldability() throws SQLException {
        
return 0;
    }


    
public int getTransactionIsolation() throws SQLException {
        
return 0;
    }


    
public void clearWarnings() throws SQLException {
    }


    
public void close() throws SQLException {

        pool.put(
this);
    }


    
public void commit() throws SQLException {
        con.commit();
    }


    
public void rollback() throws SQLException {
        con.rollback();
    }


    
public boolean getAutoCommit() throws SQLException {
        
return con.getAutoCommit();
    }


    
public boolean isClosed() throws SQLException {
        
return con.isClosed();
    }


    
public boolean isReadOnly() throws SQLException {
        
return false;
    }


    
public void setHoldability(int holdability) throws SQLException {
    }


    
public void setTransactionIsolation(int level) throws SQLException {
    }


    
public void setAutoCommit(boolean autoCommit) throws SQLException {
        con.setAutoCommit(autoCommit);
    }


    
public void setReadOnly(boolean readOnly) throws SQLException {
    }


    
public String getCatalog() throws SQLException {
        
return "";
    }


    
public void setCatalog(String catalog) throws SQLException {
    }


    
public DatabaseMetaData getMetaData() throws SQLException {
        
return con.getMetaData();
    }


    
public SQLWarning getWarnings() throws SQLException {
        
return null;
    }


    
public Savepoint setSavepoint() throws SQLException {
        
return con.setSavepoint();
    }


    
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
    }


    
public void rollback(Savepoint savepoint) throws SQLException {
        con.rollback(savepoint);
    }


    
public Statement createStatement() throws SQLException {
        
return con.createStatement();
    }


    
public Statement createStatement(int resultSetType,
                                     
int resultSetConcurrency) throws
            SQLException 
{
        
return con.createStatement(resultSetType,resultSetConcurrency);
    }


    
public Statement createStatement(int resultSetType,
                                     
int resultSetConcurrency,
                                     
int resultSetHoldability) throws
            SQLException 
{
        
return con.createStatement(resultSetType,resultSetConcurrency,resultSetHoldability);
    }


    
public Map getTypeMap() throws SQLException {
        
return null;
    }


    
public void setTypeMap(Map map) throws SQLException {
    }


    
public String nativeSQL(String sql) throws SQLException {
        
return "";
    }


    
public CallableStatement prepareCall(String sql) throws SQLException {
        
return con.prepareCall(sql);
    }


    
public CallableStatement prepareCall(String sql, int resultSetType,
                                         
int resultSetConcurrency) throws
            SQLException 
{
        
return con.prepareCall(sql,resultSetType,resultSetConcurrency);
    }


    
public CallableStatement prepareCall(String sql, int resultSetType,
                                         
int resultSetConcurrency,
                                         
int resultSetHoldability) throws
            SQLException 
{
        
return con.prepareCall(sql,resultSetType,resultSetConcurrency,resultSetHoldability);
    }


    
public PreparedStatement prepareStatement(String sql) throws SQLException {
        
return con.prepareStatement(sql);
    }


    
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws
            SQLException 
{
        
return null;
    }


    
public PreparedStatement prepareStatement(String sql, int resultSetType,
                                              
int resultSetConcurrency) throws
            SQLException 
{
        
return con.prepareStatement(sql,resultSetType,resultSetConcurrency);
    }


    
public PreparedStatement prepareStatement(String sql, int resultSetType,
                                              
int resultSetConcurrency,
                                              
int resultSetHoldability) throws
            SQLException 
{
        
return null;
    }


    
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws
            SQLException 
{
        
return null;
    }


    
public Savepoint setSavepoint(String name) throws SQLException {
        
return null;
    }


    
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws
            SQLException 
{
        
return null;
    }

    
public void closeTrue(){
        
try {
            con.close();
        }
 catch (SQLException ex) {
        }

    }


}

2.定义获得连接的工厂

 

package  sky.sql;
import  java.util.ArrayList;
import  java.sql. * ;

public   class  ConnectionPool  {
       
static ConnectionPool myPool;
       java.util.ArrayList  al 
= new ArrayList();
       
//设置连接池中连接的个数
       int size =5;
    
private ConnectionPool() {
        
try {
            Class.forName(
"com.microsoft.jdbc.sqlserver.SQLServerDriver");
            
for (int i = 0; i <size; i++{
                Connection con 
= java.sql.DriverManager.getConnection(
                        
"jdbc:microsoft:sqlserver://localhost:1433;databasename=startSky",
                        
"sa""");
                MyConn conn 
=  new MyConn(con,this);
                al.add(conn);
            }

        }
 catch (SQLException ex) {
            ex.printStackTrace();
        }
 catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }


    }

    
public static ConnectionPool getPool(){
        
if(myPool == null){
            myPool 
= new ConnectionPool();
            
return myPool;

        }
else{
          
return  myPool;
        }

    }


    
public  Connection  getConn(){
        
return (Connection) al.remove(0);
    }

    
public void put(Connection con){
        al.add(con);
    }

    
public void closeAll(){
        
for(int i=0;i<al.size();i++){
            MyConn con 
= (MyConn) al.get(i);
            con.closeTrue();
        }

    }


}

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值