今天在机器上做了一下tomcat如何连接mysql,记录如下!
先下载mysql jdbc mysql-connector-Java-5.1.7-bin.jar
然后把它放到TOMCAT的安装目录LIB下面:我的是/home/tomcat_new/lib/
然后需要配置两个文件,一个是context.xml、另一个是web.xml
安装就不再叙述了,这里只写配置
一。安装myql
然后在mysql中创建库和表:
mysql> create database first;
mysql> grant all privileges on first.* to first@localhost identified by "first";
mysql> use first;
mysql> create table about(id int(8) primary key,name varchar(10));
mysql> insert into about vlaues('xyw1026','laojiang');
mysql> flush privileges;
mysql> select * from aout;
mysql>select * from aout;
+----+----------+
| id | name |
+----+----------+
| 0 | laojiang |
+----+----------+
1 row in set (0.00 sec)
1.先修改context.xml
在之间加入以下
url="jdbc:mysql://192.168.1.34:3306/first" first 是我新建的数据库名字username="first" 数据库的用户名password="first" 数据库的密码maxActive="100" maxIdle="30"maxWait="10000" />
2.修改web.xml
在和之间加入以下:
DB Connectionjdbc/mysqlxjavax.sql.DataSourceContainer
这步配置成功后写一个test.jsp来测试是否成功。
vi test.jsp
更改完配置文件后,需要重新启动tomcat
在网页上测试http://192.168.1.34:8081/test.jsp
出现:laojiang 证明调用mysql成功。
不过今天看到tomcat后台总是报一个错误,具体是什么意思在网上也查了很久,还不是很清楚,据一篇文档说明是tomcat的问题,但是好像没有影响使用。
报错如下:
严重: A web application registered the JBDC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.