1.把数据库JDBC驱动拷贝到
%TOMCAT_HOME%/common/lib
或
%TOMCAT_HOME%/webapps/yourweb/WEB-INF/lib 下;
2.修改tomcat安装目录下conf/server.xml,在Host元素下添加一个Context元素,示例:
< Context path="/TomcatPool"docBase="E:\\Tomcat5.5\\webapps\\TomcatPool" debug="1"reloadable="true">
< Resource name="jdbc/ds" type="javax.sql.DataSource"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs"
username="sa" password="sa"
maxWait="5000" maxActive="4"
maxIdle="4"/>
< /Context>
3.修改TomcatPool工程的web.xml,添加一个resource-ref元素
< resource-ref>
< detion>sqlserverDB connection< /detion>
< res-ref-name>jdbc/ds< /res-ref-name>
< res-type>javax.sql.DataSource< /res-type>
< res-auth>Container< /res-auth>
< /resource-ref>
4.调用
javax.naming.Context context =newjavax.naming.InitialContext();
javax.sql.DataSource ds=(javax.sql.DataSource)context.lookup("java:comp/env/jdbc/ds");
java.sql.Connection conn = ds.getConnection();