欢迎光临
我们一直在努力

java的mysql连接池怎么写

利用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();

}

}

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。