---
title: "tomcat6+mysql连接池的简单实现(亲自试验通过)"
author: "tonyzzp"
date: "2010-02-04T18:26:00Z"
tags: []
slug: "tomcat6-mysql"
draft: false
lastmod: "2010-02-04T18:26:00Z"
id: 978050
url: "https://streamcave.iteye.com/blog/978050"
---

在网上找了很久，文章都是你抄、我抄你。看来看去都一样的，google往后翻N页 进去一看都是一样的。郁闷至极。

于上是CSDN论坛求助，还是java\\web坛子里的朋友热情些，终于算是搞好了，自己再试了下，把心得发出来大家指点指点。

1、mysql-connector-java-5.0.8-bin.jar文件，也就是mysql驱动包。注意：要放到tomcat6/lib目录下。放到web-inf/lib下不行，不知道为什么，我会再试试。

2、修改tomcat6/conf/context.xml文件，以&lt;context&gt;标签之间加上

```
  1: <Resource name="jdbc/mysql"
  2: 		auth="Container"
  3: 		type="javax.sql.DataSource"
  4: 		driverClassName="com.mysql.jdbc.Driver"
  5: 		url="jdbc:mysql://localhost:3306/test"
  6: 		username="root"
  7: 		password="root"
  8: 		maxActive="100"
  9: 		maxIdle="30"
 10: 	maxWait="5000" />
```

意思应该一看就能明白，就不解释了。

3、测试页面

```
  1: <!doctype html public "-//w3c//dtd html 4.0 transtional//en"
  2: "help://www.w3.org/TR/REC-html40/strict.dtd">
  3: <%@ page import="java.sql.*,javax.sql.*,javax.naming.*" session="false" %>
  4: <html>
  5: <head>
  6: <meta http-equive="Content-Type" content="text/html"; charset="gbk">
  7: <title></title>
  8: <body>
  9: 
 10: <%
 11: out.println("开始测试!");
 12: DataSource ds;
 13: InitialContext ctx;
 14: try{
 15: 	ctx=new InitialContext();
 16: 	ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
 17: 	Connection con=ds.getConnection();
 18: 	Statement stmt=con.createStatement();
 19: 	String sql="select * from person";
 20: 	ResultSet rs=stmt.executeQuery(sql);
 21: 	while(rs.next()){
 22: 		out.println(rs.getString(2));
 23: 	}
 24: 	out.println("测试成功！");
 25: }
 26: catch(Exception e){
 27: 	out.println(e.toString());
 28: }
 29: 
 30: %>
 31: </body>
 32: </html>
```

```
大功告成！！
```

```

```

```
提示：有人说还要在web-inf/web.xml里加些东西。经试验，完全不需要的。
```