Tomcat 5.5配置DataSource
在Tomcat 5.5/conf/server.xml的<GlobalNamingResources>中添加:
<Resource name="jdbc/test" auth="Container"
type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
username="root" password="admin" maxIdle="2" maxWait="5000" maxActive="4"
url="jdbc:mysql://localhost:3306/mydb" />
在Tomcat 5.5/webapps/test/WEB-INF/web.xml的<web-app>中添加:
<resource-ref>
<description>MySQL Connection Pool</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Tomcat 5.5/webapps/test/META-INF/context.xml;如果没有该文件请复制以下内容,创建context.xml
<Context privileged="true" antiResourceLocking="false" antiJARLocking="false" >
<ResourceLink name="jdbc/test" global="jdbc/test" type="javax.sql.DataSourcer"/>
</Context>
在jsp文件中加入以下代码,测试:
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<body>
<%
out.print("Start<br/>");
try{
Context ctx = new InitialContext();
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/test");
Connection conn=ds.getConnection();
out.print("MySQL connection pool runs perfectly!");
conn.close();
}
catch(Exception ex){
out.print(ex.getMessage());
ex.printStackTrace();
}
%>
</body>
本文参阅