softe version:tomcat5.0.12/mysql4.1.7/MySQL_driver---
MySQL-connector-Java-3.1.4-beta-bin.jar
mysql驅動程序可從www.MySQL.com官方網站下載
路徑:
tomcat5在d:myweb omcat5;
mysql在C:Program FilesMySQLMySQL Server 4.1
Path(在原來的基礎上加上):
d:myweb omcat5in;d:mywebjdk1.4;d:myweb
jdk1.4in;
d:myweb omcat5commonlibservlet-api.jar;C:Program Files
MySQLMySQL Server4.1in
CLASSPATH:d:myweb omcat5commonlibservlet-api.jar;d:myweb
tomcat5commonlibJSP-api.jar
Java_HOME:d:mywebjdk1.4
CATALINA_HOME:d:myweb omcat5
二、建立測試數據庫
在MySQL中建立一個forumdb數據庫,同時創建一個表member如下:
create database forumdb;
create table member
(
id int,
name varchar(6)
);
然後插入兩條測試數據如下:
insert into member values(1,"zhang");
insert into member values(2,"wang");
至此,數據庫准備完畢。
三、配置server.XML
注:我的web在d:mywebmyaPPS
首先要將MySQL的驅動程序放到d:myweb omcat5commonlib下面,一定要放".jar"的文件,如是".zip"的文件直接改為.jar即可。
用文本編輯器打開D: omcat5confserver.XML,找到結束標志,然後在之前加上如下語句:
<Context path="/myapps" docBase="d:mywebmyaPPS" debug="0"
reloadable="true" >
<Resource name="jdbc/DBConnection"
auth="Container"
type="Javax.sql.DataSource" />
<ResourceParams name="jdbc/DBConnection">
<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>10</value>
</parameter>
<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>3</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.
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>root</value>
</parameter>
<parameter>
<name>passWord</name>
<value>1234</value>
</parameter>
<!-- Class name for mm.MySQL JDBC 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/forumdb?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context>
再次提醒:一定要放在之前!
四、配置web.XML
web.xml位於d:mywebmyaPPSWEB-INF下,也即是你的WEB裡的web.XML。
同樣用文本編輯器打開web.XML,然後加入如下語句(在與之間)
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/DBConnection</res-ref-name>
<res-type>Javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
五、編寫測試JSP page。
在d:mywebmyaPPS下編寫一個dbtest.JSP,代碼如下:
<%@ page import="java.io.*,java.util.*,java.sql.*,javax.sql.*,Javax.naming.*"%>
<%@ page contentType="text/Html;charset=GB2312"%>
<Html>
<head><title>DataSourse Connection Test</title></head>
<body>
<%
try{
Java.sql.Connection con;
Statement stmt;
ResultSet rs;
Context ctx = new InitialContext();
DataSource ds=(DataSource)ctx.lookup("Java:comp/env/jdbc/DBConnection");
con=ds.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery("select * from member");
while(rs.next()){
out.print(rs.getInt(1);
out.print(rs.getString(2));
}
rs.close();
stmt.close();
con.close();
}catch(Exception e){
out.print(e.getMessage());
}
%>
</body>
</Html>
六、開始測試
運行tomcat.bat,打開IE在地址欄中輸入:http://localhost:8080/myaPPS/dbtest.JSP
如果能夠看到看到如下數據,說明配置成功。