在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 : " + databaseU