Java jdbc数据库连接池总结(二)[转]

-------------------------------------------
?DBConnectionManager?.java
------------------------------------------

package com.chunkyo.db;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;

public class DBConnectionManager {
?static private DBConnectionManager instance;//唯一数据库连接池管理实例类
?static private int clients;???????????????? //客户连接数
?private Vector drivers? = new Vector();//驱动信息
?private Hashtable pools=new Hashtable();//连接池
?
?
?public DBConnectionManager() {
??// TODO Auto-generated constructor stub
??this.init();
?}
?
?static synchronized public DBConnectionManager getInstance()
?{
??if(instance==null)
??{
???instance=new DBConnectionManager();
??}
??return instance;
??
?}
?
?public void freeConnection(String name, Connection con)
?{
??DBConnectionPool pool=(DBConnectionPool)pools.get(name);//根据关键名字得到连接池
??if(pool!=null)
??pool.freeConnection(con);//释放连接
?}
?
?public Connection getConnection(String name)
?{
??DBConnectionPool pool=null;
??Connection con=null;
??pool=(DBConnectionPool)pools.get(name);//从名字中获取连接池
??con=pool.getConnection();//从选定的连接池中获得连接
??if(con!=null)
??System.out.println("得到连接。。。");
??return con;
?}
?
?public Connection getConnection(String name, long timeout)
?{
??DBConnectionPool pool=null;
??Connection con=null;
??pool=(DBConnectionPool)pools.get(name);//从名字中获取连接池
??con=pool.getConnection(timeout);//从选定的连接池中获得连接
??System.out.println("得到连接。。。");
??return con;
?}
?
?public synchronized void release()
?{
??Enumeration allpools=pools.elements();
??while(allpools.hasMoreElements())
??{
???DBConnectionPool pool=(DBConnectionPool)allpools.nextElement();
???if(pool!=null)pool.release();
??}
??pools.clear();
?}
?
?private void createPools(DSConfigBean dsb)
?{
??DBConnectionPool dbpool=new DBConnectionPool();
??dbpool.setName(dsb.getName());
??dbpool.setDriver(dsb.getDriver());
??dbpool.setUrl(dsb.getUrl());
??dbpool.setUser(dsb.getUsername());
??dbpool.setPassword(dsb.getPassword());
??dbpool.setMaxConn(dsb.getMaxconn());
??System.out.println("ioio:"+dsb.getMaxconn());
??pools.put(dsb.getName(), dbpool);
?}
?
?private void init()
?{
??//加载驱动程序
??this.loadDrivers();
??//创建连接池
??Iterator alldriver=drivers.iterator();
??while(alldriver.hasNext())
??{
???this.createPools((DSConfigBean)alldriver.next());
???System.out.println("创建连接池。。。");
???
??}
??System.out.println("创建连接池完毕。。。");
?}
?
?private void loadDrivers()
?{
??ParseDSConfig pd=new ParseDSConfig();
?//读取数据库配置文件
??drivers=pd.readConfigInfo("ds.config.xml");
??System.out.println("加载驱动程序。。。");
?}
?
?public static void main(String[] args) {
??// TODO Auto-generated method stub
?}
}
----------------------------------------
DSConfigBean.java
----------------------------------------

package com.chunkyo.db;

public class DSConfigBean {
?private String type???? =""; //数据库类型
?private String name???? =""; //连接池名字
?private String driver?? =""; //数据库驱动
?private String url????? =""; //数据库url
?private String username =""; //用户名
?private String password =""; //密码
?private int maxconn? =0; //最大连接数
?
?public DSConfigBean() {
??// TODO Auto-generated constructor stub
?}
?
?public static void main(String[] args) {
??// TODO Auto-generated method stub
?}
?
?public String getDriver() {
??return driver;
?}
?
?public void setDriver(String driver) {
??this.driver = driver;
?}
?
?public int getMaxconn() {
??return maxconn;
?}
?
?public void setMaxconn(int maxconn) {
??this.maxconn = maxconn;
?}
?
?public String getName() {
??return name;
?}
?
?public void setName(String name) {
??this.name = name;
?}
?
?public String getPassword() {
??return password;
?}
?
?public void setPassword(String password) {
??this.password = password;
?}
?
?public String getType() {
??return type;
?}
?
?public void setType(String type) {
??this.type = type;
?}
?
?public String getUrl() {
??return url;
?}
?
?public void setUrl(String url) {
??this.url = url;
?}
?
?public String getUsername() {
??return username;
?}
?
?public void setUsername(String username) {
??this.username = username;
?}
}
-----------------------------------------------------
ParseDSConfig.java
-----------------------------------------------------

package com.chunkyo.db;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Vector;
import java.util.Iterator;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class ParseDSConfig {
?
?public ParseDSConfig() {
??// TODO Auto-generated constructor stub
?}
?
?public Vector readConfigInfo(String path)
?{
??String rpath=this.getClass().getResource("").getPath().substring(1)+path;
??Vector dsConfig=null;
??FileInputStream fi = null;
??try
??{
???fi=new FileInputStream(rpath);//读取路径文件
???dsConfig=new Vector();
???SAXBuilder sb=new SAXBuilder();
???Document doc=sb.build(fi);
???Element root=doc.getRootElement();
???List pools=root.getChildren();
???Element pool=null;
???Iterator allPool=pools.iterator();
???while(allPool.hasNext())
???{
????pool=(Element)allPool.next();
????DSConfigBean dscBean=new DSConfigBean();
????dscBean.setType(pool.getChild("type").getText());
????dscBean.setName(pool.getChild("name").getText());
????System.out.println(dscBean.getName());
????dscBean.setDriver(pool.getChild("driver").getText());
????dscBean.setUrl(pool.getChild("url").getText());
????dscBean.setUsername(pool.getChild("username").getText());
????dscBean.setPassword(pool.getChild("password").getText());
????dscBean.setMaxconn(Integer.parseInt(pool.getChild("maxconn").getText()));
????dsConfig.add(dscBean);
???}
???
??} catch (FileNotFoundException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??} catch (JDOMException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??} catch (IOException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
??
??finally
??{
???try {
????fi.close();
???} catch (IOException e) {
????// TODO Auto-generated catch block
????e.printStackTrace();
???}
??}
??
??return dsConfig;
?}

?public void modifyConfigInfo(String path,DSConfigBean dsb) throws Exception
?{
??String rpath=this.getClass().getResource("").getPath().substring(1)+path;
??FileInputStream fi=null; //读出
??FileOutputStream fo=null; //写入
??
?}

?public void addConfigInfo(String path,DSConfigBean dsb)
?{
??String rpath=this.getClass().getResource("").getPath().substring(1)+path;
??FileInputStream fi=null;
??FileOutputStream fo=null;
??try
??{
???fi=new FileInputStream(rpath);//读取xml流
???
???SAXBuilder sb=new SAXBuilder();
???
???Document doc=sb.build(fi); //得到xml
???Element root=doc.getRootElement();
???List pools=root.getChildren();//得到xml子树
???
???Element newpool=new Element("pool"); //创建新连接池
???
???Element pooltype=new Element("type"); //设置连接池类型
???pooltype.setText(dsb.getType());
???newpool.addContent(pooltype);
???
???Element poolname=new Element("name");//设置连接池名字
???poolname.setText(dsb.getName());
???newpool.addContent(poolname);
???
???Element pooldriver=new Element("driver"); //设置连接池驱动
???pooldriver.addContent(dsb.getDriver());
???newpool.addContent(pooldriver);
???
???Element poolurl=new Element("url");//设置连接池url
???poolurl.setText(dsb.getUrl());
???newpool.addContent(poolurl);
???
???Element poolusername=new Element("username");//设置连接池用户名
???poolusername.setText(dsb.getUsername());
???newpool.addContent(poolusername);
???
???Element poolpassword=new Element("password");//设置连接池密码
???poolpassword.setText(dsb.getPassword());
???newpool.addContent(poolpassword);
???
???Element poolmaxconn=new Element("maxconn");//设置连接池最大连接
???poolmaxconn.setText(String.valueOf(dsb.getMaxconn()));
???newpool.addContent(poolmaxconn);
???pools.add(newpool);//将child添加到root
???Format format = Format.getPrettyFormat();
????? format.setIndent("");
????? format.setEncoding("utf-8");
????? XMLOutputter outp = new XMLOutputter(format);
????? fo = new FileOutputStream(rpath);
????? outp.output(doc, fo);
??} catch (FileNotFoundException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??} catch (JDOMException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??} catch (IOException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
??finally
??{
???
??}
?}
?
?public void delConfigInfo(String path,String name)
?{
??String rpath=this.getClass().getResource("").getPath().substring(1)+path;
??FileInputStream fi = null;
??FileOutputStream fo=null;
??try
??{
???fi=new FileInputStream(rpath);//读取路径文件
???SAXBuilder sb=new SAXBuilder();
???Document doc=sb.build(fi);
???Element root=doc.getRootElement();
???List pools=root.getChildren();
???Element pool=null;
???Iterator allPool=pools.iterator();
???while(allPool.hasNext())
???{
????pool=(Element)allPool.next();
????if(pool.getChild("name").getText().equals(name))
????{
?????pools.remove(pool);
?????break;
????}
???}
???Format format = Format.getPrettyFormat();
????? format.setIndent("");
????? format.setEncoding("utf-8");
????? XMLOutputter outp = new XMLOutputter(format);
????? fo = new FileOutputStream(rpath);
????? outp.output(doc, fo);
???
??} catch (FileNotFoundException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??} catch (JDOMException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??} catch (IOException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
??
??finally
??{
???try {
????fi.close();
???} catch (IOException e) {
????// TODO Auto-generated catch block
????e.printStackTrace();
???}
??}
?}
?
?public static void main(String[] args) throws Exception {
??// TODO Auto-generated method stub
??ParseDSConfig pd=new ParseDSConfig();
??String path="ds.config.xml";
??pd.readConfigInfo(path);
??//pd.delConfigInfo(path, "tj012006");
??DSConfigBean dsb=new DSConfigBean();
??dsb.setType("oracle");
??dsb.setName("yyy004");
??dsb.setDriver("org.oracle.jdbc");
??dsb.setUrl("jdbc:oracle://localhost");
??dsb.setUsername("sa");
??dsb.setPassword("");
??dsb.setMaxconn(1000);
??pd.addConfigInfo(path, dsb);
??pd.delConfigInfo(path, "yyy001");
?}
}

--------------------------------------
ds.config.xml?? 配置文件
--------------------------------------


<ds-config>
<pool>
<type>mysql</type>
<name>user</name>
<driver>com.mysql.jdbc.driver</driver>
<url>jdbc:mysql://localhost:3306/user</url>
<username>sa</username>
<password>123456</password>
<maxconn>100</maxconn>
</pool>
<pool>
<type>mysql</type>
<name>user2</name>
<driver>com.mysql.jdbc.driver</driver>
<url>jdbc:mysql://localhost:3306/user2</url>
<username>sa</username>
<password>1234</password>
<maxconn>10</maxconn>
</pool>
<pool>
<type>sql2000</type>
<name>books</name>
<driver>com.microsoft.sqlserver.driver</driver>
<url>jdbc:sqlserver://localhost:1433/books:databasename=books</url>
<username>sa</username>
<password></password>
<maxconn>100</maxconn>
</pool>
</ds-config>


3. 连接池的使用
? 1。Connection的获得和释放
? DBConnectionManager?? connectionMan=DBConnectionManager?.getInstance();//得到唯一实例
?? //得到连接
?? String name="mysql";//从上下文得到你要访问的数据库的名字
?? Connection? con=connectionMan.getConnection(name);
? //使用
? 。。。。。。。
? // 使用完毕
?connectionMan.freeConnection(name,con);//释放,但并未断开连接
?2。数据库连接的动态增加和连接池的动态增加
????? 1。调用xml操作增加类

??????2。重新实例华连接池管理池类
转自: http://www.blogjava.net/chunkyo/archive/2007/01/16/94266.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值