/**
* 操作配置文件类 读 写 修改 删除等操作
*/
package com.whut.mina.dbpool;
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;
/**
* @author chenyanlin
*
*/
public class ParseDSConfig {
/**
* 构造函数
*/
public ParseDSConfig() {
// TODO Auto-generated constructor stub
}
/**
* 读取xml配置文件
* @param path
* @return
*/
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();
}
}
}
/**
* @param args
* @throws Exception
*/
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("mysql");
dsb.setName("DBPool");
dsb.setDriver("com.mysql.jdbc");
dsb.setUrl("jdbc:mysql://127.0.0.1");
dsb.setUsername("root");
dsb.setPassword("111111");
dsb.setMaxconn(1000);
pd.addConfigInfo(path, dsb);
pd.delConfigInfo(path, "yyy001");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
MINA(连接池).rar (50个子文件)
MINA
.project 380B
src
com
whut
mina
udp
threadTest.java 672B
AnaliazeMessageThread.java 7KB
MemoryMonitorHandler.java 4KB
BeatTimer.java 657B
SendList.java 315B
UserMap.java 597B
MemoryMonitor.java 6KB
MessagePushThread.java 3KB
BeatsHandler.java 3KB
dbpool
DSConfigBean.java 2KB
DBConnectionPool.java 5KB
ParseDSConfig.java 7KB
ds.config.xml 254B
DBConnectionManager.java 3KB
lib
jpush-client-2.3.0.jar 39KB
slf4j-log4j12-1.6.6.jar 9KB
gson-2.2.4.jar 186KB
gson-2.2.4-sources.jar 125KB
jdom.jar 149KB
mina-core-2.0.7.jar 630KB
com.springsource.commonj-sources-1.1.0.jar 25KB
log4j-1.2.17.jar 478KB
slf4j-api-1.6.6.jar 26KB
slf4j-api-1.6.6-sources.jar 46KB
mina-example-2.0.7-sources.jar 132KB
jcommon.jar 291KB
gson-2.2.4-javadoc.jar 244KB
mina-example-2.0.7.jar 139KB
mysql-connector-java-5.1.29-bin.jar 856KB
.settings
org.eclipse.jdt.core.prefs 629B
.fatjar 350B
.classpath 1KB
bin
com
whut
mina
udp
BeatTimer.class 1KB
threadTest$2.class 429B
threadTest.class 840B
AnalizeMessageThread.class 6KB
UserMap.class 1KB
SendList.class 737B
MessagePushThread.class 3KB
threadTest$sharedata1.class 833B
threadTest$1.class 429B
MemoryMonitorHandler.class 3KB
BeatsHandler.class 4KB
MemoryMonitor.class 4KB
dbpool
DBConnectionPool.class 4KB
ds.config.xml 254B
ParseDSConfig.class 7KB
DSConfigBean.class 2KB
DBConnectionManager.class 4KB
共 50 条
- 1
资源评论
- zy56517682016-03-30不错,有帮助
- yhfylf2014-05-26对我帮助很大
- u2kxxy2014-04-03对mina udp的使用有帮助
- sizzledx2014-11-18有点借鉴作用
- 驾驭人生2014-09-13链接池也的不错,有借鉴作用
hughtw
- 粉丝: 6
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功