---
title: "配置TOMCAT6的中mysql数据库连接池"
author: "cainwise"
date: "2010-08-13T15:11:34Z"
tags: []
categories: ["数据库"]
slug: "tomcat6-mysql"
draft: false
lastmod: "2010-08-13T15:12:26Z"
id: 736727
url: "https://cainwise.iteye.com/blog/736727"
---

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](http://commons.apache.org/dbcp/configuration.html)