tomcat连接池样例
tomcat6.0连接mysql数据库连接池[原创]
1下载Tomcat最新版本
下载地址:http://tomcat.apache.org/
2下载mysql最新版本以及最近版本的驱动程序
下载地址:http://dev.mysql.com/downloads
http://dev.mysql.com/downloads/connector
并将下载的mysql-connector-java-5.1.0-bin.jara连接文件放到$CATALINA_HOME/lib/下。
3安装mysql数据库
4创建一个tomcat应用程序,工程的名字为DBTest
5修改$CATALINA_HOME/conf/context.xml为以下内容
<Contextpath="/DBTest"docBase="DBTest"
debug="5"reloadable="true"crossContext="true">
<!--maxActive:MaximumnumberofdBconnectionsinpool.Makesureyouconfigureyourmysqldmax_connectionslargeenoughtohandle
allofyourdbconnections.Setto0fornolimit.
-->
<!--maxIdle:MaximumnumberofidledBconnectionstoretaininpool.
Setto-1fornolimit.SeealsotheDBCPdocumentationonthis
andtheminEvictableIdleTimeMillisconfigurationparameter.
-->
<!--maxWait:MaximumtimetowaitforadBconnectiontobecomeavailable
inms,inthisexample10seconds.AnExceptionisthrownif
thistimeoutisexceeded.Setto-1towaitindefinitely.
-->
<!--usernameandpassword:MySQLdBusernameandpasswordfordBconnections-->
<!--driverClassName:Classnamefortheoldmm.mysqlJDBCdriveris
org.gjt.mm.mysql.Driver-werecommendusingConnector/Jthough.
ClassnamefortheofficialMySQLConnector/Jdriveriscom.mysql.jdbc.Driver.
-->
<!--url:TheJDBCconnectionurlforconnectingtoyourMySQLdB.
TheautoReconnect=trueargumenttotheurlmakessurethatthe
mm.mysqlJDBCDriverwillautomaticallyreconnectifmysqldclosedthe
connection.mysqldbydefaultclosesidleconnectionsafter8hours.
-->
<Resourcename="jdbc/TestDB"auth="Container"type="javax.sql.DataSource"
maxActive="100"maxIdle="30"maxWait="10000"
username="javauser"password="javadude"driverClassname="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/jtest?autoReconnect=true"/></Context>
此时要注意修改自己的数据库的用户名和密码
我建立的
数据库:jtest
用户名:javauser
密码:javadude
6修改工程目录下的web.xml文件添加如下
<description>MySQLTestApp</description>
<resource-ref>
<description>DBConnection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>7创建一个java类
packagecom.test;
/*
*DBTest.java
*
*Createdon2007/06/07,10:33:02
*
*Tochangethistemplate,chooseTools|TemplateManager
*andopenthetemplateintheeditor.
*/
/**
*
*@authorwangzicai
*/
importjavax.naming.*;
importjavax.sql.*;
importjava.sql.*;
publicclassDBTest{
Stringfoo="NotConnected";
intbar=-1;
publicvoidinit(){
try{
Contextctx=newInitialContext();
if(ctx==null)
thrownewException("Boom-NoContext");
DataSourceds=
(DataSource)ctx.lookup(
"java:comp/env/jdbc/TestDB");
if(ds!=null){
Connectionconn=ds.getConnection();
if(conn!=null){
foo="GotConnection"+conn.toString();
Statementstmt=conn.createStatement();
ResultSetrst=
stmt.executeQuery(
"select*fromtest");
if(rst.next()){
foo=rst.getString(1);
bar=208;
}
conn.close();
}
}}catch(Exceptione){
e.printStackTrace();
}
}
publicStringgetFoo(){returnfoo;}
publicintgetBar(){returnbar;}
}
8编辑index.jsp
<%@pagelanguage="java"pageEncoding="UTF-8"%>
<%@pageimport="com.test.*"%>
<html>
<head>
<title>DBTest</title>
</head>
<body>
<%
DBTesttst=newDBTest();
tst.init();
%>
<h2>Results</h2>
Foo<%=tst.getFoo()%><br>
Bar<%=tst.getBar()%>
</body>