Reference http://tomcat.apache.org/tomcat-5.0-doc/jndi-datasource-examples-howto.html
1. Download the MySQL JDBC driver
Http://mysql.mirror.vmmatrix.net/Downloads/Connector-J/mysql-connector-java-3.1.13.tar.gz
Decompress the package and copy the JAR file to Tomcat Lib.
CD downloads
Tar xvzf mysql-connector-java-3.1.13
Sudo CP mysql-connector-java-3.1.13/opt/tomcat/common/lib
2. configure Server. xml
<Host name = "localhost" DEBUG = "0" appbase = "webapps"
Unpackwars = "true" autodeploy = "true"
Xmlvalidation = "false" xmlnamespaceaware = "false">
<! -- Configure the connection pool -->
<Context Path = "/zhaobiao" docbase = "/Media/workspace/J2EE/project/zhaobiao" DEBUG = "0" reloadable = "true">
<Resource Name = "JDBC/zhaobiao" auth = "Container" type = "javax. SQL. datasource"/>
<Resourceparams name = "JDBC/zhaobiao">
<Parameter>
<Name> factory </Name>
<Value> org. Apache. commons. DBCP. basicperformancefactory </value>
</Parameter>
<Parameter> <Name> maxactive </Name> <value> 10 </value> </parameter>
<Parameter> <Name> maxidle </Name> <value> 30 </value> </parameter>
<Parameter> <Name> maxwait </Name> <value> 5000 </value> </parameter>
<Parameter> <Name> username </Name> <value> chenzhixin </value> </parameter>
<Parameter> <Name> password </Name> <value> 19851026 </value> </parameter>
<Parameter> <Name> driverclassname </Name> <value> com. MySQL. JDBC. Driver </value> </parameter>
<Parameter>
<Name> URL </Name>
<Value> JDBC: mysql: // localhost: 3306/zhaobiao? Autoreconnect = true </value>
</Parameter>
</Resourceparams>
</Context>
Zhaobiao in <Resource Name = "JDBC/zhaobiao" is my database name
3. Configure web. XML in your working directory
Before </Web-app>, add
<Resource-ref>
<Description> connection pools </description>
<Res-ref-Name> JDBC/zhaobiao </RES-ref-Name>
<Res-type> javax. SQL. datasource </RES-type>
<Res-auth> container </RES-auth>
</Resource-ref>
4. Test
<HTML>
<% @ Page import = "javax. SQL. *, java. SQL. *, javax. Naming. *" %>
<Body>
<%
Try {
Context CTX = new initialcontext ();
Context envctx = (context) CTX. Lookup ("Java: COMP/ENV ");
Datasource DS = (datasource) envctx. Lookup ("JDBC/zhaobiao ");
Connection conn = Ds. getconnection ();
Statement ST = conn. createstatement ();
Resultset rs1_st.exe cutequery ("select * From maker ");
While (Rs. Next () {out. println (Rs. getstring (2 ));}
} Catch (exception e) {out. println (E );}
%>
</Body>
</Html>
5. Restart Tomcat and enter http: // localhost/zhaobiao/connectionpools. jsp.