在META-INF中配置context.xml文件:
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/jinda"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=dboJinDa"
username="sa"
password="123456"
maxActive="50"
maxIdle="20"
maxWait="10000" />
</Context>
然后再web.xml中配置
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/jinda</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
调用
Context initContext = new InitialContext();
DataSource envContext = (DataSource)initContext.lookup("java:/comp/env/jdbc/jinda");
// 找到DataSource
Connection conn=envContext.getConnection();