在web应用中使用xml配置数据源,我们一般要通过以下几步来实现:
(一) 编写配置数据源的xml文件
本例中的配置文件存放在/web-inf/目录下,也可以放在别的目录下,只是在操作的时候不同罢了。
(1) ms sql 的配置文件/web-inf/mssql.xml,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<datasource>
<!-- configure the datasource of mssql -->
<databaseuser>sa</databaseuser>
<databasepassword>jckjdkmcj</databasepassword>
<databasename>northwind</databasename>
<servername>10.0.0.168</servername>
<serverport>1433</serverport>
<maxconnections>100</maxconnections>
</datasource>
(2) oracle的配置文件/web-inf/oracle.xml,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<datasource>
<!-- configure the datasource of mssql -->
<databaseuser>zhangyi</databaseuser>
<databasepassword>jckjdkmcj</databasepassword>
<databasename>zydb</databasename>
<servername>10.0.0.168</servername>
<serverport>1521</serverport>
<maxconnections>100</maxconnections>
</datasource>
注意:此处两个文件的格式是一样的,因为在下面的解析的过程中我们用到了是用的同一个接口
(二) 设计解析xml文件的一个接口
在此,我们用定义了一个接口:config.java
/*
* created on 2005-8-29
*
* the supper class for parse the xml files
*
* todo to change the template for this generated file go to
* window - preferences - java - code style - code templates
*/
package zy.pro.wd.xml;
import java.io.inputstream;
import javax.xml.parsers.*;
import javax.servlet.servletcontext;
import org.xml.sax.inputsource;
import org.w3c.dom.*;
/**
* @author zhangyi
*
* todo to change the template for this generated type comment go to window -
* preferences - java - code style - code templates
*/
public abstract class config {
/**
* the supper class for parse the xml files
*/
protected element root;
protected void init(servletcontext sctx, string xmlfile) throws exception {
inputstream is=null;
try{
is=sctx.getresourceasstream(xmlfile);
documentbuilderfactory factory=documentbuilderfactory.newinstance();
documentbuilder builder=factory.newdocumentbuilder();
document doc=builder.parse(new inputsource(is));
root=doc.getdocumentelement();
system.out.println("root: "+root );
}catch(exception e){
e.printstacktrace();
}finally{
if(is!=null){
is.close();
}
}
}
protected string getelementtext(element parent,string name){
nodelist nodelist=parent.getelementsbytagname(name);
if(nodelist.getlength()==0){
return null;
}
element element=(element)nodelist.item(0);
stringbuffer sb=new stringbuffer();
for(node child=element.getfirstchild();child!=null;child=child.getnextsibling()){
if(child.getnodetype()==node.text_node){
sb.append(child.getnodevalue());
}
}
return sb.tostring().trim();
}
protected void cleanup(){
root=null;
}
}
(三) 定义解析我们自定义配置文件(xml文件)的 抽象类,此处我们定义了datasourceconfig.java,文件内容如下:
/*
* created on 2005-8-29
*
*reading the jdbc datasource properties from xml files
*
* todo to change the template for this generated file go to
* window - preferences - java - code style - code templates
*/
package zy.pro.wd.xml;
import javax.sql.datasource;
import javax.servlet.servletcontext;
/**
* @author zhangyi
*
* todo to change the template for this generated type comment go to window -
* preferences - java - code style - code templates
*/
public abstract class datasourceconfig extends config {
private static final string database_user = "databaseuser";
private static final string database_password = "databasepassword";
private static final string server_name = "servername";
private static final string database_name = "databasename";
private static final string server_port = "serverport";
protected datasource ds;
protected string databaseuser;
protected string databasepassword;
protected string servername;
protected string portnumber;
protected string databasename;
public void init(servletcontext sctx,string xmlfile) throws exception{
super.init(sctx,xmlfile);
databaseuser=this.getelementtext(root,database_user);
system.out.println("<br>databaseuser: "+databaseuser);
databasepassword=this.getelementtext(root,database_password);
system.out.println("<br>databasepassword: "+databasepassword);
databasename=this.getelementtext(root,database_name);
system.out.println("<br>databasename: "+databasename);
servername=this.getelementtext(root,server_name);
system.out.println("<br>servername: "+servername);
portnumber=this.getelementtext(root,server_port);
system.out.println("<br>portnumber: "+portnumber);
}
public datasource getdatasource(){
return ds;
}
}
(四) 定义我们解析数据源配置文件的实现类
(1) 定义解析ms sql 数据源的实现类mssqlconfig.java.内容如下:
/*
* created on 2005-8-31
*
* todo to change the template for this generated file go to
* window - preferences - java - code style - code templates
*/
package zy.pro.wd.xml;
import javax.servlet.servletcontext;
import org.apache.commons.dbcp.basicdatasource;
import com.microsoft.jdbc.base.baseconnectionpool;
/**
* @author zhangyi
*
* todo to change the template for this generated type comment go to
* window - preferences - java - code style - code templates
*/
public class mssqlconfig extends datasourceconfig {
private static final string max_connections = "maxconnections";
public void init(servletcontext ctx, string xmlfile) throws exception {
super.init(ctx, xmlfile);
string databaseurl = "jdbc:microsoft:sqlserver://" + this.servername + ":"
+ this.portnumber + ";databasename=" + this.databasename;
system.out.println("<br> databaseurl : " + databaseurl);
ds = new basicdatasource();
/*
*此处使用的是apache 给提供的dbcp数据源
*/
system.out.println("<br>ds: " + ds);
((basicdatasource) ds).seturl(databaseurl);
((basicdatasource) ds).setusername(this.databaseuser);
((basicdatasource) ds).setpassword(this.databasepassword);
try {
int maxconnections = integer.parseint(this.getelementtext(root,
max_connections));
((basicdatasource) ds).setmaxactive(maxconnections);
} catch (exception e) {
e.printstacktrace();
}
this.cleanup();
}
}
(2) 定义实现解析oracle数据源的实现类oracleconfig.java,内容如下:
/*
* created on 2005-8-29
*
*parse the xml file of the oracle configure
*
* todo to change the template for this generated file go to
* window - preferences - java - code style - code templates
*/
pa
闽公网安备 35060202000074号