国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

在Web應用中使用XML文件配置數據源

2019-11-18 12:48:27
字體:
來源:轉載
供稿:網友

  在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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳西县| 仁怀市| 嵊泗县| 柞水县| 甘南县| 太谷县| 泰州市| 疏勒县| 高州市| 冕宁县| 永安市| 万山特区| 新乡市| 泸溪县| 荣成市| 安化县| 长宁区| 阿勒泰市| 疏附县| 达拉特旗| 三门县| 武冈市| 兴化市| 于都县| 万山特区| 邢台市| 枞阳县| 鄂州市| 华阴市| 大邑县| 石河子市| 奈曼旗| 滁州市| 余干县| 涿鹿县| 小金县| 岢岚县| 岳西县| 永安市| 康保县| 大竹县|