在实际应用开发中,特别是在web应用系统中,如果jsp、servlet或ejb使用jdbc直接访问数据库中的数据,每一次数据访问请求都必须经历建立数据库连接、打开数据库、存取数据和关闭数据库连接等步骤,而连接并打开数据库是一件既消耗资源又费时的工作,如果频繁发生这种数据库操作,系统的性能必然会急剧下降,甚至会导致系统崩溃。数据库连接池技术是解决这个问题最常用的方法.
本文以目前最流行的mysql为例,讲解通过tomcat连接池连接mysql数据库的基本步骤,如果你了解mysql可跳过第一步。
在进行tomcat连接池配置前,先解压缩mysql-connector-java-xxx.zip,将其中的mysql-connector-java-3.x.x-xxx.jar取出,置于< %tomcat_home%>\common\lib中。
接下来,让我们一起进入精彩的tomcat配置之旅。
一.新建用户及数据库
操作步骤如下:
c:\documents and settings\administrator>d:
d:\>cd mysql\bin
d:\mysql\bin>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:\mysql\bin>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 varcha
闽公网安备 35060202000074号