1.将数据库的JDBC驱动放入TOMCAT_HOME/common/lib下
2.配置TOMCAT_HOME/conf/server.xml
在<GlobalNamingResources></GlobalNamingResources>中增加
<Resource name="jdbc/oa" type="javax.sql.DataSource" username="oa" password="password"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://localhost:1433;DatabaseName=OA"
maxIdle="2" maxWait="50" maxActive="4" />
3.配置TOMCAT_HOME/conf/Catalina/localhost/oa.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="D:/oa/WebRoot" privileged="true" useHttpOnly="true">
<Resource name="jdbc/oa" type="javax.sql.DataSource" username="oa" password="password"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://localhost:1433;DatabaseName=OA"
maxIdle="2" maxWait="50" maxActive="4" />
</Context>
4.配置项目的web.xml文件,将下面代码加入到web.xml中:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/oa</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
5.启动tomcat,用代码:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/oa");
Connection conn = ds.getConnection();
测试是否配置成功