mysql与tomcat连接池配置


一、修改配置文件
首先,你需要打开server.xml文件,将下面的内容贴到</Host>前面,找到相对应的位置。
<Context path="/DBTest" docBase="DBTest"
       debug="5" reloadable="true" crossContext="true">
<!—注意到这里的DBTest了吧,之后你需要将DBTest这个目录建立出来。-->

 <Logger className="org.apache.catalina.logger.FileLogger"
            prefix="localhost_DBTest_log." suffix=".txt"
            timestamp="true"/>

 <Resource name="jdbc/TestDB"
              auth="Container"
              type="javax.sql.DataSource"/>
 <ResourceParams name="jdbc/TestDB">
   <parameter>
     <name>factory</name>
     <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
   </parameter>

   <!-- Maximum number of dB connections in pool. Make sure you
        configure your mysqld max_connections large enough to handle
        all of your db connections. Set to 0 for no limit.
        -->
   <parameter>
     <name>maxActive</name>
     <value>100</value>
   </parameter>

   <!-- Maximum number of idle dB connections to retain in pool.
        Set to 0 for no limit.
        -->
   <parameter>
     <name>maxIdle</name>
     <value>30</value>
   </parameter>

   <!-- Maximum time to wait for a dB connection to become available
        in ms, in this example 10 seconds. An Exception is thrown if
        this timeout is exceeded.  Set to -1 to wait indefinitely.
        -->
   <parameter>
     <name>maxWait</name>
     <value>10000</value>
   </parameter>

   <!-- MySQL dB username and password for dB connections  -->
   <parameter>
    <name>username</name>
    <value>tube</value>
<!—数据库用户名-->

   </parameter>
   <parameter>
    <name>password</name>
    <value>uplooking</value>
<!—数据库密码-->
   </parameter>
   <!-- Class name for the old mm.mysql JDBC driver - uncomment this entry and comment next
        if you want to use this driver - we recommend using Connector/J though
   <parameter>
      <name>driverClassName</name>
      <value>org.gjt.mm.mysql.Driver</value>
</parameter>
这里面是被注释的,因为以前连接mysql是这样连接的,现在建议用下面的方法,自己可以看到。
    -->
  
   <!-- Class name for the official MySQL Connector/J driver -->
   <parameter>
      <name>driverClassName</name>
      <value>com.mysql.jdbc.Driver</value>
   </parameter>
  
   <!-- The JDBC connection url for connecting to your MySQL dB.
        The autoReconnect=true argument to the url makes sure that the
        mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
        connection.  mysqld by default closes idle connections after 8 hours.
        -->
   <parameter>
     <name>url</name>
     <value>jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value>
<!--这是数据库的地址,也可以不要这么繁琐,改为
jdbc:mysql://localhost/javatest即可,javatest为数据库名-->
   </parameter>
 </ResourceParams>
</Context>



二、配置测试页面和MYSQL连接驱动

在tomcat $\webapps文件夹下建立一个名为DBTest的文件夹,注意大小写。在此文件夹下建立WEB-INF文件夹,及一个文件:test.jsp。

再在 WEB-INF文件夹下建立两个文件夹:lib及classes,将数据库驱动文件mysql-connector-java-5.0.7-bin.jar拷贝到lib文件夹下,同样还在WEB-INF文件夹下建一个文件 web.xml,文件内容如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"  version="2.4">
 <description>MySQL Test App</description>
 <resource-ref>
     <description>DB Connection</description>
     <res-ref-name>jdbc/TestDB</res-ref-name>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
 </resource-ref>
</web-app>

然后,再将以下内容编辑进test.jsp文件。这是个用java语言写的测试web页面。


Test.jsp

<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="java.sql.*,javax.sql.DataSource,javax.naming.*"%>
<html>
<head><title>test.jsp</title></head>
<body bgcolor="#ffffff">
<h1>test Tomcat</h1>
<%
try
{
Context initCtx=new InitialContext();
DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/TestDB");
Connection conn=ds.getConnection();
out.println("data from database:<br>");
Statement stmt=conn.createStatement();
ResultSet rs =stmt.executeQuery("select id, foo, bar from testdata");
while(rs.next())
{
out.println(rs.getInt("id"));
out.println(rs.getString("foo"));
out.println(rs.getString("bar"));
}
rs.close();
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>

三、配置MYSQL数据库

在MYSQL数据库中建立相应的用户名和密码,并赋予足够的权限。
mysql>create database javatest;
mysql>grant all on javatest.* to tube@'localhost' identified by 'uplooking';
mysql>flush privileges;
mysql>use javatest;
mysql>create table testdata (
        id int,
        foo float,
        bar text
);
mysql>insert into testdata (id,foo,bar)
        values
        (1,2.2,test);

OK,现在你可以打开http://localhost:8080/DBTest/test.jsp查看结果了。