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