数据库连接池

package com.yunta.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;

public class DBconection {
private int sum;

private String driver;

private String username;

private String password;

private String URL;

private Vector<Connection> vector=new Vector<Connection>();

private int maxConection;

private int minConection;

/**
* 初始化配置参数
*/
public DBconection(){
ParseConfigFile.init();
driver=ParseConfigFile.getDBType("driver");
username=ParseConfigFile.getDBType("username");
password=ParseConfigFile.getDBType("password");
URL=ParseConfigFile.getDBType("URL");
maxConection=Integer.parseInt(ParseConfigFile.getDBType("maxConection"));
minConection=Integer.parseInt(ParseConfigFile.getDBType("minConection"));
initPool();
}

private void initPool() {
while(vector.size()<=minConection){
vector.addElement(getNewConnection());
System.out.println(vector.size());
}

}

/**
* 把连接放回连接池Vector
*
* @param con
*/
public synchronized void freeConnection(Connection con){
if(con!=null){
vector.addElement(con);

sum--;
}
notifyAll();
}

/**
* 从连接池获取的方法
* @return
*/
public synchronized Connection getConnection(){
Connection con=null;
if(vector.size()>0){
con=vector.firstElement();
vector.removeElementAt(0);
try {
if(con.isClosed()){
con=getConnection();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
con=getConnection();
}
}else if(maxConection==0||sum<maxConection){
con=getNewConnection();
}
if(con!=null){
sum++;
}
return con;
}
/**
* 从连接池获取连接的方法,参数time是超时参数
* @param time
* @return
*/
public synchronized Connection getConnection(long time){
long startTime = new Date().getTime();
Connection con=null;
while((con=getConnection())==null){
try {
wait(time);//与释放连接的notifyall协同工作
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(new Date().getTime()-startTime >time){
break;
}
}
return con;
}

public Connection getNewConnection(){
Connection conn=null;
try {
Class.forName(driver).newInstance();
conn =DriverManager.getConnection(URL, username, password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return conn;
}
/**
* 删除所有连接
*/
public synchronized void relase(){
Enumeration<Connection> allConnections = vector.elements();
while(allConnections.hasMoreElements()){
Connection con=allConnections.nextElement();
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
vector.removeAllElements();
}

public Vector<Connection> getVector() {
return vector;
}

public void setVector(Vector<Connection> vector) {
this.vector = vector;
}

public static void main(String[] arg){
DBconection DB=new DBconection();
Connection con = DB.getNewConnection();
System.out.println(con);
}
}


文件读取类:
package com.yunta.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class ParseConfigFile {

private final static String path=System.getProperty("user.dir")+File.separator+"DBconfig.xml";

private static Properties p;


/**
* 初始化
*/
public static void init(){
try {
InputStream in = new FileInputStream(path);
p=new Properties();
p.load(in);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取数据库配置信息
* @return
*/
public static String getDBType(String key){
String value=null;
if(p!=null){
value=p.getProperty(key);
}else{
ParseConfigFile.init();
value=p.getProperty(key);
}
return value;

}

public static void main(String[] arg){
ParseConfigFile.init();
System.out.println(ParseConfigFile.getDBType("DBType"));
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值