网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  在web应用中使用xml文件配置数据源     
  文章作者:未知  文章来源:水木森林  
  查看:114次  录入:管理员--2007-11-17  
 
  在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
 
 
上一篇: 技术分享 使用xml-rpc来访问web服务    下一篇: xml——连接sql和web程序的桥梁(图)
  相关文档
经验谈:“三步走”成为一名优秀程序员 11-16
入门:jpetstore学习struts新的开发模式 01-09
thinking in java (the 2nd edition) study note 11-17
jbuilder9+weblogic platfrom 8.1+oracle 9i安装和配置 11-17
JAVA基础:使用Properties类带来的好处 08-06
用java实现web服务器 http协议 11-17
javabean 在jsp和serlvle中的传递技术要点 11-17
从jar和zip档案文件中提取java资源讲解 11-16
scjp认证套题解析之十二 11-16
java2的安全新特性下的applet数字签名具体实现方法 11-17
j2ee综合--java开发工具安装配置心得 01-22
lesson1.04配j2ee环境及cloudscape数据库 11-17
给在校生-java学习,一条漫长的道路 11-16
用java编写的密码算法类 11-17
对等计算实践:p2p遇上ssl 11-17
什么是ejb 11-17
java学习笔记swing jframe窗口学习 11-17
printscreen读取位图转为jpeg格式输出 11-17
vj6.0的使用方法 11-17
再谈cocoon兼谈jsp 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息