1.context.xml文件的配置
<Resource name="jdbc/news" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="root" driverClassName="com.mysql.cj.jdbc.Driver" url="jdbc:mysql://localhost:3306/newsmgr?useUnicode=true&characterEnoding=utf8&serverTimezone=UTC"/>
2.web.xml文件的配置
<resource-ref> <description>news DataSource</description> <res-ref-name>jdbc/news</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
3.获得连接的方法
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/news"); Connection conn = ds.getConnection();