1.tomcat中配置context.xml
<Resource name="jdbc/db" auth="Container" type="javax.sql.DataSource"
maxActive="50" maxIdle="20" maxWait="1000" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/db" username="root" password="123456" />
2.配置web.xml
<resource-ref>
<res-ref-name>jdbc/db</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3.Java代码
Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/db");
Connection conn = dataSource.getConnection();