Chinaunix首页 | 论坛 | 博客
  • 博客访问: 807929
  • 博文数量: 780
  • 博客积分: 7000
  • 博客等级: 少将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-12 09:11
文章分类

全部博文(780)

文章存档

2011年(1)

2008年(779)

我的朋友
最近访客

分类:

2008-09-12 09:13:21


  在web应用中使用XML配置数据源,我们一般要通过以下几步来实现:
  
  (一)  编写配置数据源的XML文件
  
  本例中的配置文件存放在/WEB-INF/目录下,也可以放在别的目录下,只是在操作的时候不同罢了。
  
  (1)  MS SQL 的配置文件/WEB-INF/mssql.xml,内容如下:
  
  
  
  sa
  jckjdkmcj
  northwind
  10.0.0.168
  1433
  100
  

  
  (2)  的配置文件/WEB-INF/oracle.xml,内容如下:
  
  
  
  zhangyi
  jckjdkmcj
  zydb
  10.0.0.168
  1521
  100
  

  注意:此处两个文件的格式是一样的,因为在下面的解析的过程中我们用到了是用的同一个接口
  
  (二)  设计解析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 - - 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 - - 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("
databaseUser:      "+databaseUser);
  databasePassword=this.getElementText(root,DATABASE_PASSWORD);
  System.out.println("
databasePassword:     "+databasePassword);
  databaseName=this.getElementText(root,DATABASE_NAME);
  System.out.println("
databaseName:    "+databaseName);
  serverName=this.getElementText(root,SERVER_NAME);
  System.out.println("
serverName:     "+serverName);
  portNumber=this.getElementText(root,SERVER_PORT);
  System.out.println("
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("
databaseURL :    " + databaseURL);
  ds = new BasicDataSource();
  /*
  *此处使用的是apache 给提供的DBCP数据源
  */
  System.out.println("
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数据源的实现类Config.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
【责编:admin】

--------------------next---------------------

阅读(474) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~