tomcat配置连接池

wuyan19831013 2007-01-24 02:07:40
Tomcat5.0.28,jdk1.5,windowsXP,mysql5.0
主要依据Tomcat的帮助文档:
http://127.0.0.1:8080/tomcat-docs/jndi-datasource-examples-howto.html
1:创建数据库,用户名以及密码

mysql> GRANT ALL PRIVILEGES ON *.* TO javauser@localhost
-> IDENTIFIED BY 'javadude' WITH GRANT OPTION;
mysql> create database javatest;
mysql> use javatest;
mysql> create table testdata (
-> id int not null auto_increment primary key,
-> foo varchar(25),
-> bar int);



2:添加数据
mysql> insert into testdata values(null, 'hello', 12345);
Query OK, 1 row affected (0.00 sec)

mysql> select * from testdata;
+----+-------+-------+
| ID | FOO | BAR |
+----+-------+-------+
| 1 | hello | 12345 |
+----+-------+-------+
1 row in set (0.00 sec)

mysql>
3:配置server.xml文档,即将以下文件插入到server.xml的<host></host>标记之间
<!--Context path="/DBTest" docBase="DBTest"-----此处tomcat的文档是有错误的(反正我配置不对)应该是以下配置,其中ss代表你的WEB程序名称-->
<Context path="/ss" docBase="ss" debug="5" reloadable="true">
debug="5" reloadable="true" crossContext="true">

<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 -1 for no limit. See also the DBCP documentation on this
and the minEvictableIdleTimeMillis configuration parameter.
-->
<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>javauser</value>
</parameter>
<parameter>
<name>password</name>
<value>javadude</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>
-->

<!-- 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>
</parameter>
</ResourceParams>
</Context>
4:配置web.xml

<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">
<!--这个标记是tomcat帮助文件多出来的,不需要<web-app>-->
<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>
5:写测试文档,tomcat自己带的使用jstl标记库
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<sql:query var="rs" dataSource="jdbc/TestDB">
select id, foo, bar from testdata
</sql:query>

<html>
<head>
<title>DB Test</title>
</head>
<body>

<h2>Results</h2>

<c:forEach var="row" items="${rs.rows}">
Foo ${row.foo}<br/>
Bar ${row.bar}<br/>
</c:forEach>

</body>
</html>
如果要使用以上代码作测试,你得下载jakarta-taglibs-standard-1.1.2.zip这个文件解压后,将jakarta-taglibs-standard-1.1.2\lib下的两个.jar文件copy到你的web应用程序的WEB_INFO\lib下面即可或者使用以下测试代码:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*,javax.sql.DataSource,javax.naming.*">
<html>
<head><title>test.jsp</title></head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<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 from testdata");
while(rs.next()){
out.println(rs.getInt("id"));
out.println(rs.getString("foo"));
}
rs.close();
stmt.close();
}catch(Exception e){
out.println(e.getMessage());
}
%>
</body>
</html>
...全文
217 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
dyw31415926 2007-01-24
  • 打赏
  • 举报
回复
mark 省得我写学习笔记

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧