站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题Tomcat5.0-5.5连接池配置指南

Tomcat5.0-5.5连接池配置指南

摘要: Tomcat5.0-5.5连接池配置指南
<table width="100%" height="92%" border="0" cellpadding="6" cellspacing="0"> <tr> <td height="623" align="left" valign="top" class="content">   本文以目前最流行的MySQL为例,讲解通过Tomcat连接池连接MySQL数据库的基本步骤,如果你了解MySQL可跳过第一步。

  在进行Tomcat连接池配置前,先解压缩mysql-connector-java-xxx.zip,将其中的mysql-connector-java-3.x.x-xxx.jar取出,置于<%TOMCAT_HOME%>commonlib中。

  接下来,让我们一起进入精彩的Tomcat配置之旅。

一.新建用户及数据库

操作步骤如下:

C:Documents and SettingsAdministrator>d:

D:>cd mysqlin

D:MySQLin>mysql -u root -p
Enter password: *******
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 51 to server version: 4.1.12a-nt

Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

mysql> GRANT ALL PRIVILEGES ON jcc.* TO jcc@localhost IDENTIFIED BY 'jsp.com.cn'
WITH GRANT OPTION;
Query OK, 0 rows affected (0.01 sec)

mysql> USE mysql;
Database changed
mysql> SELECT Host,User,Password FROM user;
+-----------+------+-------------------------------------------+
| Host | User | Password |
+-----------+------+-------------------------------------------+
| localhost | root | *60D5B730382EC2170CA366DE181767E4C5343DE8 |
| % | jsp | *C22AB0FD8A289C7D337C9998B63B8EA8335E5F35 |
| localhost | jcc | *C22AB0FD8A289C7D337C9998B63B8EA8335E5F35 |
+-----------+------+-------------------------------------------+
3 rows in set (0.01 sec)

mysql> exit
Bye

D:MySQLin>mysql -u jcc -p
Enter password: **********
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 57 to server version: 4.1.12a-nt

Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

mysql> CREATE DATABASE jcc;
Query OK, 1 row affected (0.02 sec)

mysql> USE jcc;
Database changed
mysql> CREATE TABLE user(
-> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> name VARCHAR(8) NOT NULL
-> );
Query OK, 0 rows affected (0.08 sec)

mysql> INSERT INTO user (name) VALUES ('Corebit');
Query OK, 1 row affected (0.03 sec)

mysql> INSERT INTO user (name) VALUES ('Ivan');
Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM user;
+----+---------+
| id | name |
+----+---------+
| 1 | Corebit |
| 2 | Ivan |
+----+---------+
2 rows in set (0.00 sec)

mysql>

二.配置Tomcat连接池

Tomcat5.0进行如下配置:

在<%TOMCAT_HOME%>confserver.xml的...之间加入以下代码:

debug="5" reloadable="true" crossContext="true">

prefix="localhost_DBTest_log." suffix=".txt"
timestamp="true"/>

auth="Container"
type="javax.sql.DataSource"/>



factory
org.apache.commons.dbcp.BasicDataSourceFactory



maxActive
100



maxIdle
30



maxWait
10000



username
jcc


password
jsp.com.cn



driverClassName
com.mysql.jdbc.Driver



url
jdbc:mysql://localhost/jcc




Tomcat5.5进行如下配置:

在<%TOMCAT_HOME%>confserver.xml的...之间加入以下代码:

debug="5" reloadable="true" crossContext="true">

maxActive="100" maxIdle="30" maxWait="10000"
username="jcc" password="jsp.com.cn" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/jcc"/>



三.在<%wwwroot%>/下,新建MySQL数据库连接文件Select.jsp

  Select.jsp源码如下:

<%@page contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="javax.naming.*"%>

<body>
<%
try{
Context initCtx=new InitialContext();
DataSource db = (DataSource)initCtx.lookup("java:comp/env/jdbc/JCC");
Connection conn = db.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM user");
out.println("User-list"+"
");
while(rs.next()){
out.print(rs.getString(1)+" ");
out.print(rs.getString(2)+"
");
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
out.print(e);
}
%>
</body>


四.运行http://localhost/Select.jsp,显示结果如下:

User-list
1 Corebit
2 Ivan

  则表示数据库连接成功!恭喜!恭喜!

  否则请检查数据库连接器版本,出错可能性比较高!

  *注:

  Tomcat连接池中,部分参数说明如下:

  maxActive="100"   </tr> </table></td> </tr> </table>
↑返回目录
前一篇: ANT安装、配置
后一篇: hai&&web与struts_config与database.properties的配置