直接上代码
package cn.sm1234.controller;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;
import javax.sql.DataSource;
import com.sun.istack.internal.Pool;
public class MyPool implements DataSource {
/**创建一个容器存放数据库连接池*/
static List<Connection> pool = new LinkedList<Connection>();
/**静态类初始化数据库连接*/
static{
for (int i = 0; i < 10; i++) {
//可以从工具类里面获取数据库的连接池, 如下:
//Connection con = JDBCUtils.getConnection();
pool.add(con);
}
}
/**提供给外界connection方法,外界用来获取数据库连接*/
public Connection getConnection() throws SQLException {
//从连接池中移除一个对象并返回这个对象
Connection conn = pool.remove(0);
return conn;
}
/**提供一个返回连接的方法*/
public void returnConnection(Connection conn) {
try {
if (conn!=null && !conn.isClosed()) {
pool.add(conn);
}
} catch (Exception e) {
// TODO: handle exception
}
}
public PrintWriter getLogWriter() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public void setLogWriter(PrintWriter out) throws SQLException {
// TODO Auto-generated method stub
}
public void setLoginTimeout(int seconds) throws SQLException {
// TODO Auto-generated method stub
}
public int getLoginTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
// TODO Auto-generated method stub
return null;
}
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}
public Connection getConnection(String username, String password) throws SQLException {
// TODO Auto-generated method stub
return null;
}
}