<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/datasourcetest">
<Resource name="jdbc/testdb"
auth="Container"
type="javax.sql.DataSource"
username="username"
password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/testdb"
maxActive="8"
maxIdle="4" />
</Context>
[size=large]2.設置WEN-INF/web.xml,添加資源引用[/size]
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<resource-ref>
<res-ref-name>jdbc/testdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>
[size=large]3.使用jndi獲得數據源[/size]
Context context = new InitialContext();
DataSource source = (DataSource) context.lookup("java:comp/env/jdbc/testdb");
Connection conn = source.getConnection();
[size=large]4.mysql驅動必須放在tomcat/lib中[/size]
記此以備忘