在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