一、配置環境變量:
windowsxp + sp2
softe version:
jdk-1_5_0_01
tomcat5.5.12
mysql4.1.14
mysql-connector-java-3.1.111-bin.jar
路徑: tomcat5在c:/tomcat 5.5;
mysql在c:/program files/mysql/mysql server 4.1
jdk在c:/jdk1.5.0_01
path(在原來的基礎上加上):c:/tomcat 5.5/bin;c:/jdk1.5.0_01;c:/jdk1.5.0_01/bin;c:/tomcat 5.5/common/lib/servlet-api.jar;c:/program files/mysql/mysql server 4.1/bin
classpath:c:/tomcat 5.5/common/lib/servlet-api.jar;c:/tomcat 5.5/common/lib/jsp-api.jar
java_home:/jdk1.5.0_01
catalina_home:c:/tomcat 5.5
二、建立測試數據庫
在mysql中建立一個mywebdb數據庫,同時創建一個表tested如下
create database mywebdb
create table tested
(
id int(2),
name varchar(6)
);
然后插入兩條測試數據如下
insert into member values(1,"holmes");
insert into member values(2,"conan");
至此,數據庫準備完畢
三、tomcat5數據源配置
1 以admin賬戶登入找到tomcatserver/service/host/你的web程序名/datasource //-->點擊這里 。
2 選擇“create new data source” 。
3 填寫信息
jndi name: mypool //-->連接池名字
data source url: jdbc:mysql://localhost:3306/mywebdb?autoreconnect=true
jdbc driver class: com.mysql.jdbc.driver
//驅動需下載。
user name: //數據庫用戶名 。
password: //數據庫密碼 。
max. active connections: //最大活動連結數 ,0為不限 。
max. idle connections: //最大等待連結數 ,0為不限 。
max. wait for connection: //建立連接超時時間ms,-1為無限 。
4保存以后一定要點commit changes。
5不用重啟,只要刷新頁面。
我的server.xml 內容為:
<?xml version="1.0" encoding="utf-8"?>
<server>
<listener classname="org.apache.catalina.core.aprlifecyclelistener"/>
<listener classname="org.apache.catalina.mbeans.globalresourceslifecyclelistener"/>
<listener classname="org.apache.catalina.storeconfig.storeconfiglifecyclelistener"/>
<listener classname="org.apache.catalina.mbeans.serverlifecyclelistener"/>
<globalnamingresources>
<environment
name="simplevalue"
type="java.lang.integer"
value="30"/>
<resource
auth="container"
description="user database that can be updated and saved"
name="userdatabase"
type="org.apache.catalina.userdatabase"
pathname="conf/tomcat-users.xml"
factory="org.apache.catalina.users.memoryuserdatabasefactory"/>
<resource
name="mypool"
type="javax.sql.datasource"
driverclassname="com.mysql.jdbc.driver"
password="admin"
maxidle="5"
maxwait="5000"
username="root"
url="jdbc:mysql://localhost:3306/mywebdb?autoreconnect=true"
maxactive="5"/>
</globalnamingresources>
<service
name="catalina">
<connector
port="8080"
redirectport="8443"
minsparethreads="25"
connectiontimeout="20000"
maxsparethreads="75"
maxthreads="150"
maxhttpheadersize="8192">
</connector>
<connector
port="8009"
redirectport="8443"
connectiontimeout="0"
protocol="ajp/1.3">
</connector>
<engine
defaulthost="localhost"
name="catalina">
<realm classname="org.apache.catalina.realm.userdatabaserealm"/>
<host
appbase="webapps"
name="localhost">
</host>
</engine>
</service>
</server>
四、配置web.xml
web.xml位于c:/tomcat 5.5/webapps/root/web-inf下,也即是你的web里的web.xml。(我是直接使用tomcat的工程進行修改)
同樣用文本編輯器打開web.xml,然后加入如下語句(在<web-app>與</web-app>之間)
<resource-ref>
<description>db connection</description>
<res-ref-name>mypool</res-ref-name>
<res-type>javax.sql.datasource</res-type>
<res-auth>container</res-auth>
</resource-ref>
五、編寫測試jsp page
在c:/tomcat 5.5/webapps/root下編寫一個test.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{
statement stmt;
resultset rs;
javax.naming.context ctx=new javax.naming.initialcontext();
javax.sql.datasource ds= (javax.sql.datasource)ctx.lookup ("java:comp/env/mypool"); //紅色 字為連接池名,其他固定。
java.sql.connection con=ds.getconnection();
stmt=con.createstatement();
rs=stmt.executequery("select * from tested");
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,打開ie在地址欄中輸入:http://localhost:8080/root/test.jsp.
可是顯示結果卻是:
cannot create jdbc driver of class '' for connect url 'null'
請高手指教一下!急!
新聞熱點
疑難解答