Tomcat的数据源配置.txt

  1. 1.将tomcat安装目录下/conf.context.xml文件拷贝到WebRoot/MEAT-INF下。
  2. 2.将  <Resource name="jdbc/tfms" auth="Container" type="javax.sql.DataSource"
  3.                maxActive="50" maxIdle="30" maxWait="10000" logAbandoned="true" 
  4.                username="sa" password="password" driverClassName="net.sourceforge.jtds.jdbc.Driver"
  5.                url="jdbc:jtds:sqlserver://localhost:1433/TransfusionMngDB;tds=8.0;lastupdatecount=true"/>
  6.  拷贝到context.xml 的 Context元素下。
  7. 3.将
  8.   <resource-ref>
  9.       <description>DB Connection</description>
  10.       <res-ref-name>jdbc/tfms</res-ref-name>
  11.       <res-type>javax.sql.DataSource</res-type>
  12.       <res-auth>Container</res-auth>
  13.   </resource-ref>
  14. 拷贝到web.xml 的 <welcome-file-list>元素标签后。
  15. 3.
  16. Tomcat 5的数据源配置跟Tomcat 5.5的数据源配置有很多的差别,Tomcat 6的数据源配置跟Tomcat 5.5的配置基本上相同。
  17. 以前的Tomcat5的配置需要在server.xml文件当中配置或者在conf/Catalina/localhost下面相应的上下文配置文件做配置。这种配置方式不合理的地方在于,假如数据库做了更改,程序员需要手工去修改这些文件,不利于团队开发。
  18. Tomcat 5.5跟Tomcat 6的配置显得更为简单,我们只需要在我们的WebRoot目录下,新建一个META-INF的目录(假如不存在),在该目录下创建一个context.xml文件,并且在context.xml文件当添加以下的配置信息:
  19.  程序代码
  20. <Context>
  21.   <Resource name="jdbc/tfms" auth="Container" type="javax.sql.DataSource"
  22.                maxActive="50" maxIdle="30" maxWait="10000" logAbandoned="true" 
  23.                username="sa" password="password" driverClassName="net.sourceforge.jtds.jdbc.Driver"
  24.                url="jdbc:jtds:sqlserver://localhost:1433/TransfusionMngDB;tds=8.0;lastupdatecount=true"/>
  25. </Context>
  26. 其中:
  27. name 表示指定的jndi名称
  28. auth 表示认证方式,一般为Container
  29. type 表示数据源类型,使用标准的javax.sql.DataSource
  30. maxActive 表示连接池当中最大的数据库连接
  31. maxIdle 表示最大的空闲连接数
  32. maxWait 当池的数据库连接已经被占用的时候,最大等待时间
  33. logAbandoned 表示被丢弃的数据库连接是否做记录,以便跟踪
  34. username 表示数据库用户名
  35. password 表示数据库用户的密码
  36. driverClassName 表示JDBC DRIVER
  37. url 表示数据库URL地址
  38. 在以往的tomcat当中还需要在web.xml指定相应的resource,在tomcat 5.5以后的版本不写也可以,但建议还是配置。
  39.  程序代码
  40.   <resource-ref>
  41.       <description>DB Connection</description>  <!-- 这句可要可不要-->
  42.       <res-ref-name>jdbc/tfms</res-ref-name>
  43.       <res-type>javax.sql.DataSource</res-type>
  44.       <res-auth>Container</res-auth>
  45.   </resource-ref>
  46. 配置完之后,还需要将JDBC DRIVER存放在%TOMCAT_HOME%/lib里面,这是必须的,不然,tomcat没有办法找到driver
  47. 之后重新启动tomcat
  48. 最后,测试数据源是否正确,写一个test.jsp,在test.jsp得到DataSource,以下是程序片断
  49.  程序代码
  50. Context initContext = new InitialContext();
  51. Context envContext  = (Context)initContext.lookup("java:/comp/env");
  52. DataSource ds = (DataSource)envContext.lookup("jdbc/tfms");
  53. Connection conn = ds.getConnection();
原文地址:https://www.cnblogs.com/huangjihua/p/4125249.html