在Web应用中使用XML文件配置数据源
来源: 时间:06-12-30 点击: 点击这里收藏本文
}
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);
把此文章收藏到: