mysql|資料|資料來源
一、配置環境變數:
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'
請高手指教一下!急!