在/tomcat/conf/context.xml的context根节点下添加:
<Resource name="dataSource" auth="Container"
type="javax.sql.DataSource"
username="doggy" password="pass"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/world"
maxActive="100" maxIdle="10" maxWait="5000" />
Servlet中使用:
Context ctx = new InitialContext();
//这里的/env/xxxx中,xxxx必须与配置文件中的name一致
DataSource ds = (DataSource)ctx.lookup("java:comp/env/dataSource");
Connection conn = ds.getConnection();
即可完成配置