1、把mysql的jar包拷贝到tomcat中的lib文件夹中
2、在tomcat的conf文件夹中找到一个叫context.xml文件
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource
name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/数据库名称"
username="root"
password="root"
maxActive="10000"
maxIdle="30"
maxWait="10000" />
</Context>
3、在你的项目中web.xml中添加
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4、好了tomcat连接池就这样配置好了,接下来获取连接
public class DBPoolTomcat {
public static Connection getConnection() {
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/mysql");
Connection conn = ds.getConnection();
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
5、详细参数说明!请参照http://commons.apache.org/dbcp/configuration.html
本文详细介绍如何在Tomcat服务器中配置MySQL连接池,包括将MySQL的jar包添加到lib目录,修改context.xml文件设置数据源参数,在web.xml中添加资源引用,并提供了一段Java示例代码用于获取数据库连接。
551

被折叠的 条评论
为什么被折叠?



