数据库连接池
packagecom.yunta.common;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
importjava.util.Date;
importjava.util.Enumeration;
importjava.util.Vector;
publicclassDBconection{
privateintsum;
privateStringdriver;
privateStringusername;
privateStringpassword;
privateStringURL;
privateVector<Connection>vector=newVector<Connection>();
privateintmaxConection;
privateintminConection;
/**
*初始化配置参数
*/
publicDBconection(){
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();
}
privatevoidinitPool(){
while(vector.size()<=minConection){
vector.addElement(getNewConnection());
System.out.println(vector.size());
}
}
/**
*把连接放回连接池Vector
*
*@paramcon
*/
publicsynchronizedvoidfreeConnection(Connectioncon){
if(con!=null){
vector.addElement(con);
sum--;
}
notifyAll();
}
/**
*从连接池获取的方法
*@return
*/
publicsynchronizedConnectiongetConnection(){
Connectioncon=null;
if(vector.size()>0){
con=vector.firstElement();
vector.removeElementAt(0);
try{
if(con.isClosed()){
con=getConnection();
}
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
con=getConnection();
}
}elseif(maxConection==0||sum<maxConection){
con=getNewConnection();
}
if(con!=null){
sum++;
}
returncon;
}
/**
*从连接池获取连接的方法,参数time是超时参数
*@paramtime
*@return
*/
publicsynchronizedConnectiongetConnection(longtime){
longstartTime=newDate().getTime();
Connectioncon=null;
while((con=getConnection())==null){
try{
wait(time);//与释放连接的notifyall协同工作
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
if(newDate().getTime()-startTime>time){
break;
}
}
returncon;
}
publicConnectiongetNewConnection(){
Connectionconn=null;
try{
Class.forName(driver).newInstance();
conn=DriverManager.getConnection(URL,username,password);
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(InstantiationExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IllegalAccessExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnconn;
}
/**
*删除所有连接
*/
publicsynchronizedvoidrelase(){
Enumeration<Connection>allConnections=vector.elements();
while(allConnections.hasMoreElements()){
Connectioncon=allConnections.nextElement();
try{
con.close();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
vector.removeAllElements();
}
publicVector<Connection>getVector(){
returnvector;
}
publicvoidsetVector(Vector<Connection>vector){
this.vector=vector;
}
publicstaticvoidmain(String[]arg){
DBconectionDB=newDBconection();
Connectioncon=DB.getNewConnection();
System.out.println(con);
}
}
文件读取类:
packagecom.yunta.common;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.util.Properties;
publicclassParseConfigFile{
privatefinalstaticStringpath=System.getProperty("user.dir")+File.separator+"DBconfig.xml";
privatestaticPropertiesp;
/**
*初始化
*/
publicstaticvoidinit(){
try{
InputStreamin=newFileInputStream(path);
p=newProperties();
p.load(in);
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
/**
*获取数据库配置信息
*@return
*/
publicstaticStringgetDBType(Stringkey){
Stringvalue=null;
if(p!=null){
value=p.getProperty(key);
}else{
ParseConfigFile.init();
value=p.getProperty(key);
}
returnvalue;
}
publicstaticvoidmain(String[]arg){
ParseConfigFile.init();
System.out.println(ParseConfigFile.getDBType("DBType"));
}
}