在web应用中使用XML配置数据源,我们一般要通过以下几步来实现:
(一)编写配置数据源的XML文件
本例中的配置文件存放在/WEB-INF/目录下,也可以放在别的目录下,只是在操作的时候不同罢了。
(1)MS SQL 的配置文件/WEB-INF/MSSQL.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?
<DataSource
<!-- configurethe datasourceofMSSQL--
<DatabaseUsersa</DatabaseUser
<DatabasePassWordjckjdkmcj</DatabasePassword
<DatabaseNamenorthwind</DatabaseName
<ServerName10.0.0.168</ServerName
<ServerPort1433</ServerPort
<MaxConnections100</MaxConnections
</DataSource
(2)Oracle的配置文件/WEB-INF/oracle.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?
<DataSource
<!-- configurethe datasourceofMSSQL--
<DatabaseUserzhangyi</DatabaseUser
<DatabasePasswordjckjdkmcj</DatabasePassword
<DatabaseNamezydb</DatabaseName
<ServerName10.0.0.168</ServerName
<ServerPort1521</ServerPort
<MaxConnections100</MaxConnections
</DataSource
注重:此处两个文件的格式是一样的,因为在下面的解析的过程中我们用到了是用的同一个接口
(二)设计解析XML文件的一个接口
在此,我们用定义了一个接口:config.Java
/*
* Created on 2005-8-29
*
* thesupperclassforparsethe xmlfiles
*
* 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 {
/**
* thesupperclassforparsethe xmlfiles
*/
protected Element root;
protected void init(ServletContext sctx, String xmlFile) throws Exception {
InputStreamis=null;
try{
is=sctx.getResourceAsStream(xmlFile);
DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
DocumentBuilderbuilder=factory.newDocumentBuilder();
Documentdoc=builder.parse(new InputSource(is));
root=doc.getDocumentElement();
System.out.println("root: "+root );
}catch(Exception e){
e.printStackTrace();
}finally{
if(is!=null){
is.close();
}
}
}
protectedString getElementText(Element parent,String name){
NodeListnodeList=parent.getElementsByTagName(name);
if(nodeList.getLength()==0){
return null;
}
Elementelement=(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();
}
protectedvoid cleanup(){
root=null;
}
}
(三)定义解析我们自定义配置文件(XML文件)的 抽象类,此处我们定义了DataSourceConfig.java,文件内容如下:
/*
* Created on 2005-8-29
*
*readingtheJDBCdatasourceproperties fromxmlfiles
*
* 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";
protectedDataSource ds;
protected String databaseUser;
protected String databasePassword;
protected String serverName;
protected StringportNumber;
protected String databaseName;
public voidinit(ServletContext sctx,String xmlFile) throws Exception{
super.init(sctx,xmlFile);
databaseUser=this.getElementText(root,DATABASE_USER);
System.out.println("<brdatabaseUser: "+databaseUser);
databasePassword=this.getElementText(root,DATABASE_PASSWORD);
System.out.println("<brdatabasePassword: "+databasePassword);
databaseName=this.getElementText(root,DATABASE_NAME);
System.out.println("<brdatabaseName: "+databaseName);
serverName=this.getElementText(root,SERVER_NAME);
System.out.println("<brserverName: "+serverName);
portNumber=this.getElementText(root,SERVER_PORT);
System.out.println("<brportNumber: "+portNumber);
}
publicDataSourcegetDataSource(){
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("<brds: " + 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
*
*parsethexmlfileoftheoracleconfigure
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
pa