利用java编写mysql连接池的方法
具体内容如下:
public class ConnecionPool {
private int size;
List
connections = new ArrayList<>(); public ConnecionPool(int size){
this.size=size;
init();
}
public void init(){
try {
Class.forName("com.mysql.jdbc.Driver");
while (size--!=0){
connections.add(DriverManager.getConnection(jdbc:mysql://127.0.0.1:3306/d数据库名称, 用户名,密码)// );
}
}catch (Exception e){
e.printStackTrace();
}
}
public Connection getConnection(){
try {//如果没有连接了,线程就等待
while (connections.isEmpty()){
this.wait();
}
}catch (Exception e){
e.printStackTrace();
}
return connections.remove(0);
}
public void returnConntion(Connection connection){
connections.add(connection);
this.notifyAll();
}
}