数据库连接池,话不多说,上代码,代码解释连接池原理,看着代码就懂了
自建连接池核心类哈!
package com.zkb.jdbctest;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import javax.sql.DataSource;
public class MyDataPool implements DataSource {//首先要来实现这DataSource接口
private LinkedList
linkedList = new LinkedList
();
private static String driver = "com.mysql.jdbc.Driver";//数据库驱动
private static String dbUrl = "jdbc:mysql://localhost:3306/userdb";//数据库服务地址
private static String dbUserName ="root";//用户名
private static String dbPassWord ="root";//密码
//初始化连接数量
public MyDataPool() {
for(int i = 0; i < 10; i++) {
try {
Class.forName(driver);
Connection con =DriverManager.getConnection(dbUrl, dbUserName, dbPassWord);
linkedList.add(con);
System.out.println(i+"条已被创建,放入了连接池");
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public Connection getConnection() throws SQLException {
// TODO Auto-generated method stub
final Connection conn = linkedList.removeFirst();
System.out.println("有人已经获取了一条连接,还剩"+linkedList.size()+"条");
return conn;
}
//这里为释放掉连接
public void releaseConn(Connection con) {
linkedList.add(con); //借了就得还给我啊
System.out.println("有人归还了连接,此时空闲连接为"+linkedList.size()+"条");
}
@Override
public Connection getConnection(String username, String password)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PrintWriter getLogWriter() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public int getLoginTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setLogWriter(PrintWriter arg0) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setLoginTimeout(int arg0) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public boolean isWrapperFor(Class
arg0) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public
T unwrap(Class
arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
}
package com.zkb.jdbctest;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class TestPool {
/**
*
*/
public static void main(String[] args) throws SQLException {
MyDataPool dataSource = new MyDataPool();
Connection conn =(Connection) dataSource.getConnection();
Statement state =(Statement) conn.createStatement();
ResultSet result =state.executeQuery("select * from userinfo");
while(result.next()){
System.out.println(result.getString("id")+"\t"+result.getString("username")+"\t"+result.getString("password"));
}
result.close();//结果集没得说关掉!!!
state.close(); //用于执行静态 SQL 语句并返回它所生成结果的对象。关掉!!!
dataSource.releaseConn(conn);//连接就不要关了,还给连接池
}
}
DROP TABLE IF EXISTS `userinfo`;
CREATE TABLE `userinfo` (
`id` int(8) NOT NULL auto_increment,
`username` varchar(20) NOT NULL,
`password` varchar(20) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of userinfo
-- ----------------------------
INSERT INTO `userinfo` VALUES ('1', 'admin', 'admin');
INSERT INTO `userinfo` VALUES ('2', 'user', 'user');
INSERT INTO `userinfo` VALUES ('3', 'pass', 'pass');