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"));
}
}
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"));
}
}