配置过程:
1.Tomcat 6.0\conf\context.xml
<Resource name="jdbc/db_school" auth="Container" type="javax.sql.DataSource"
maxActive="50" maxIdle="30" maxWait="10000" logAbandoned="true"
username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/db_school" />
2.Tomcat 6.0\webapps\G309\WEB-INF\web.xml 在<web-app></web-app>之间添加:
<resource-ref>
<description>MyProject</description>
<res-ref-name>jdbc/db_school</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3.调用的代码:
public Connection getConnection() throws SQLException {
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/db_school");
con = ds.getConnection();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}