看看以下我自己写的代码,你就明白怎么写了,我曾经用过的,(可以直接用)但建议不要拷贝,自己可以试着写,下面给你参考。//用于创建连接池;
import java.sql.*;
import java.util.*;public class ConnBean {
private int inUse=0;
private Vector connections=new Vector();
private String poolname;
private String dbid;
private String drivername;
private String username;
private String passwd;
private int maxconn;
public ConnBean(String poolname,String dbid,String drivername,
String username,String passwd,int maxconn) {
this.poolname=poolname;
this.dbid=dbid;
this.drivername=drivername;
this.username=username;
this.passwd=passwd;
this.maxconn=maxconn;
}
public synchronized void releaseConn(Connection conn){
connections.addElement(conn);
inUse--;
}
public synchronized Connection getConn(){
Connection con=null;
if(connections.size()>0){
con=(Connection)connections.elementAt(0);
connections.removeElementAt(0);
try {
if (con.isClosed())
con = getConn();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
else if(maxconn==0||inUse<maxconn){
con=newConn();
}
if(con!=null){
inUse++;
}
return con;
}
private Connection newConn(){
Connection con=null;
try{
Class.forName(drivername).newInstance();
con=DriverManager.getConnection(dbid,username,passwd);
}
catch(Exception e){
e.printStackTrace();
return null;
}
return con;
}
public synchronized void closeconn(){
Enumeration allConnections=connections.elements();
while(allConnections.hasMoreElements()){
Connection con=(Connection) allConnections.nextElement();
try{
con.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
connections.removeAllElements();
}
}//管理连接池import java.sql.*;
import java.util.*;public class ConnManager {
private Vector poolnames=new Vector();
private Vector drivernames=new Vector();
private Vector dbids=new Vector();
private Vector usernames=new Vector();
private Vector passwds=new Vector();
private Vector maxcons=new Vector();
private Hashtable connPools=new Hashtable();
public ConnManager() {
//在这里创建不同类型数据库的连接池,只需在这里重写下面的代码,比如SQLSERVER,ORACLE, ACCESS,MYSQL
poolnames.addElement("连接池名");
drivernames.addElement("JDBC驱动");
dbids.addElement("url");
usernames.addElement("用户名");
passwds.addElement("密码");
maxcons.addElement("最大连接数");
createPools();
}
public void realeaseConn(String name,Connection con){
ConnBean pool=(ConnBean)connPools.get(name);
if(pool!=null)
pool.releaseConn(con);
}
public Connection getConn(String name){
ConnBean pool=(ConnBean)connPools.get(name);
if(pool!=null)
return pool.getConn();
return null;
}
public synchronized void closeConn(){
Enumeration allPools=connPools.elements();
while(allPools.hasMoreElements()){
ConnBean pool=(ConnBean)allPools.nextElement();
pool.closeconn();
}
}
private void createPools(){
for(int i=0;i<poolnames.size();i++){
String poolname=poolnames.elementAt(i).toString();
String drivername=drivernames.elementAt(i).toString();
String dbid=dbids.elementAt(i).toString();
String username=usernames.elementAt(i).toString();
String passwd=passwds.elementAt(i).toString();
int maxconn=0;
try{
maxconn=Integer.parseInt(maxcons.elementAt(i).toString());
}
catch(NumberFormatException e){
e.printStackTrace();
}
ConnBean pool=new ConnBean(poolname,dbid,drivername,username,passwd,maxconn);
connPools.put(poolname,pool);
}
}
}
import java.sql.*;
import java.util.*;public class ConnBean {
private int inUse=0;
private Vector connections=new Vector();
private String poolname;
private String dbid;
private String drivername;
private String username;
private String passwd;
private int maxconn;
public ConnBean(String poolname,String dbid,String drivername,
String username,String passwd,int maxconn) {
this.poolname=poolname;
this.dbid=dbid;
this.drivername=drivername;
this.username=username;
this.passwd=passwd;
this.maxconn=maxconn;
}
public synchronized void releaseConn(Connection conn){
connections.addElement(conn);
inUse--;
}
public synchronized Connection getConn(){
Connection con=null;
if(connections.size()>0){
con=(Connection)connections.elementAt(0);
connections.removeElementAt(0);
try {
if (con.isClosed())
con = getConn();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
else if(maxconn==0||inUse<maxconn){
con=newConn();
}
if(con!=null){
inUse++;
}
return con;
}
private Connection newConn(){
Connection con=null;
try{
Class.forName(drivername).newInstance();
con=DriverManager.getConnection(dbid,username,passwd);
}
catch(Exception e){
e.printStackTrace();
return null;
}
return con;
}
public synchronized void closeconn(){
Enumeration allConnections=connections.elements();
while(allConnections.hasMoreElements()){
Connection con=(Connection) allConnections.nextElement();
try{
con.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
connections.removeAllElements();
}
}//管理连接池import java.sql.*;
import java.util.*;public class ConnManager {
private Vector poolnames=new Vector();
private Vector drivernames=new Vector();
private Vector dbids=new Vector();
private Vector usernames=new Vector();
private Vector passwds=new Vector();
private Vector maxcons=new Vector();
private Hashtable connPools=new Hashtable();
public ConnManager() {
//在这里创建不同类型数据库的连接池,只需在这里重写下面的代码,比如SQLSERVER,ORACLE, ACCESS,MYSQL
poolnames.addElement("连接池名");
drivernames.addElement("JDBC驱动");
dbids.addElement("url");
usernames.addElement("用户名");
passwds.addElement("密码");
maxcons.addElement("最大连接数");
createPools();
}
public void realeaseConn(String name,Connection con){
ConnBean pool=(ConnBean)connPools.get(name);
if(pool!=null)
pool.releaseConn(con);
}
public Connection getConn(String name){
ConnBean pool=(ConnBean)connPools.get(name);
if(pool!=null)
return pool.getConn();
return null;
}
public synchronized void closeConn(){
Enumeration allPools=connPools.elements();
while(allPools.hasMoreElements()){
ConnBean pool=(ConnBean)allPools.nextElement();
pool.closeconn();
}
}
private void createPools(){
for(int i=0;i<poolnames.size();i++){
String poolname=poolnames.elementAt(i).toString();
String drivername=drivernames.elementAt(i).toString();
String dbid=dbids.elementAt(i).toString();
String username=usernames.elementAt(i).toString();
String passwd=passwds.elementAt(i).toString();
int maxconn=0;
try{
maxconn=Integer.parseInt(maxcons.elementAt(i).toString());
}
catch(NumberFormatException e){
e.printStackTrace();
}
ConnBean pool=new ConnBean(poolname,dbid,drivername,username,passwd,maxconn);
connPools.put(poolname,pool);
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货