Tomcat+mysql连接池的配置与测试
[转贴请注明出处]
龙族联盟:网络巡警
?
Tomcat+mysql连接池的配置与测试
?
环境:
Tomcat6.0.18
Mysql: 5.1.36
Windwos XP sp2
?
最近需要用到Tomcat+mysql连接池的配置,今天在网上查了好多资料,老资料太多,对于以上的老版本tomcat可能有用,本人没有测试过,正确写出tomcat6.0配置的文间很少!好多资料上面的配置对于现在新版的几乎都是有问题的,十错七八!今天把我配置的正确方法写下来,供大家参考!
?
1.安装tomcat,略;
2.安装mysql略,
进入mysql,新建数据库test1;
test1 数据库 中建表pet,表结构如下;
mysql> desc pet;
+---------+----------+------+-----+---------+-------+
| Field?? | Type???? | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| name??? | char(20) | YES? |???? | NULL??? |?????? |
| owner?? | char(20) | YES? |???? | NULL??? |?????? |
| species | char(20) | YES? |???? | NULL??? |?????? |
| sex???? | char(1)? | YES? |???? | NULL??? |?????? |
| birth?? | date???? | YES? |???? | NULL??? |?????? |
| death?? | date???? | YES? |???? | NULL??? |?????? |
+---------+----------+------+-----+---------+-------+
?
加入一些数据;
INSERT INTO pet VALUES ('猪八戒','唐僧','神仙','f','2001-12-01',NULL);
?
这里注意mysql表字符集设置,很有可能在客户端和JSP中乱码,可参考我写的文章: eclipse连接mysql乱码及mysql[ERROR 1366 (HY000):错误]
http://c02949.blog.163.com/blog/static/485037200962345218147/
?
网上搜到的好多资料说:tomcat配置mysql连接池需要改:
配置tomcat下的conf下的context.xml
配置tomcat下的conf下的webt.xml
配置tomcat下的conf下的server.xml 乱七八糟,害我把Tomcat6.0重装一次!浪费了好多时间.
?
正确的配置方法只需要修改context.xml和web.xml两个文件:
context.xml在<context></context>之间添加连接池如下:
<Resource name="jdbc/mysql" auth="Container"
??????????????? type="javax.sql.DataSource"
??????????????? maxActive="50" maxIdle="10" maxWait="5000"
??????????????? username="你的mysql用户" password="你的mysql密码"
??????????????? driverClassName="org.gjt.mm.mysql.Driver"
??????????????? url="jdbc:mysql://localhost/test1" />
?
web.xml中的<web-app></web-app>之间加入:
<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>
?
注意的地方: context.xml文件中的name="jdbc/mysql"要和web.xml中的<res-ref-name>jdbc/mysql</res-ref-name>要一致;
?
mysql 的jdbc驱动复制到配置tomcat下的lib目录;
?
OK,到些就配置完了,下来就是写测试代码:
?
打开Eclipse其手工建个WEB project(我的工程名mysql),加入mysql JDBC驱动,记得这个一定要加入;
?
新建jsp文件:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>??
<%@ page import="javax.sql.*"%>??
<%@ page import="javax.naming.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <head>
??? <title>mysql连接池测试</title>
??????? <meta http-equiv="pragma" content="no-cache">
??????? <meta http-equiv="cache-control" content="no-cache">
??????? <meta http-equiv="expires" content="0">???
??????? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
??????? <meta http-equiv="description" content="This is my page">
??????? <!--
??????? <link rel="stylesheet" type="text/css" href="styles.css">
??????? -->??
? </head>
?
? <body>
??????? <%?
??? out.print("我的测试开始<br>");??
??? DataSource ds = null;??
????? try{
??????????? InitialContext ctx=new InitialContext();
??????????? ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
??????????? Connection conn = ds.getConnection();?
??????????? Statement stmt = conn.createStatement();
??????????? String strSql = " select * from pet";
??????????? ResultSet rs = stmt.executeQuery(strSql);
?????????????????? while(rs.next()){
??????????????????????????????????????? out.print("name:" + rs.getString(1));
??????????????????????????????????????? out.print("\t所有者:" + rs.getString(2));
??????????????????????????????????????? out.print("\tbirth:" + rs.getString("birth"));
??????????????????????????????????????? out.println("<br>");
??????????????? }
??????????????? rs.close();
??????????????? stmt.close();
??????????????? conn.close();
???? }catch(Exception ex){
???????????? ex.printStackTrace();
???? }
??????? %>?
???
? </body>
</html>
启动tomcat ,部署web 工程
?
IE中打开:http://localhost/mysql/
显示结果:
我的测试开始
name:猪八戒 所有者:唐僧 birth:2001-12-01
?
以上本人测试成功,希望能给朋友们有所参考!
2009-7-24
龙族联盟:网络巡警