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