一 安裝Tomcat
在安裝了jdk後,在網上下載個tomcat5.0,安裝方法很簡單,一注意:安裝時會提示輸入系統管理使用者名和密碼,這是以後會用到的使用者名稱和密碼,切記。
二 安裝mysql或sqlserver2005,這裡就以我安裝的mysql為例子。
三 使用Tomcat的Web管理應用配置資料來源
啟動Tomcat伺服器,開啟瀏覽器,輸入http://localhost:8080/admin/(其中localhost是名稱伺服器或稱為主機),入管理介面的登陸頁面,這時候請輸入原來安裝時要求輸入的使用者名稱和密碼,登陸到管理介面, 選擇Resources-Data sources進入配置資料來源介面,選擇Data Source Actions ->選擇Create New Data Source,進入配置詳細資料介面,內容如下:
JNDI Name: jdbc/mysql
Data Source URL: jdbc:mysql://localhost:3306/cs JDBC Driver Class: com.mysql.jdbc.Driver User Name: root Password: ******** Max. Active Connections: 4 Max. Idle Connections: 2 Max. Wait for Connection: 5000 Validation Query: 要求輸入的JNDI Name等資訊,其中除了JDBC DriverClass之外,其他的可以根據你的需要填寫。比如Data Source URL的內容可能是:jdbc:mysql:// IP或是名稱/DataBaseName,其中DataBaseName是你的資料庫名稱,IP是你的資料庫的所在的伺服器的IP或是名稱。最後點擊Save->Commit Change.這樣你的資料來源的基本資料配置一半了。
4、 修改web.xml和%TOMCAT_HOME%/conf/Catalina/localhost下對應你的引用的設定檔
主要添加
<?xml version='1.0' encoding='utf-8'?>
<Context workDir="work/Catalina/localhost/web" path="/web" docBase="web">
<Resource type="javax.sql.DataSource" description="DB2 Datasource mysql" auth="Container" name="jdbc/mysql"/>
</Context>
開啟%TOMCAT_HOME%/conf/web.xml,在</web-app>的前面添加以下內容: <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/mysql</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> 注意res-ref-name填寫的內容要與在上文提到的JNDI Name名稱一致。進入%TOMCAT_HOME%/conf/Catalina/localhost下,找到你的web應用對應的.xml檔案(下面有我的web的目錄結構),如 myweb.xml,並在此檔案的下添入代碼:
<ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSourcer"/>到這裡,配置工作就基本完成了。5、 其他注意事項
別忘了JDBC驅動程式mysql-connector-java-3.0.9-stable-bin.jar一定要放置到Tomcat的對應目錄,你的JDBC驅動可能版比筆者高,不過只要能與所使用的MySql對應就可以了,因為我發現版本太低的JDBC驅動不能支援4.0.*版本的MySQL資料庫,建議放置在%TOMCAT_HOME%/common/lib和應用的WEB-INF/lib下。兩者有什麼不同呢?其實一看就明白了,common/lib是所有的應用都可以使用的庫檔案位置。重啟你的Tomcat服務。6、 編寫測試代碼
在應用的目錄下建立一個Test.jsp檔案,代碼如下:<!doctype html public "-//w3c//dtd html 4.0 transitional//en" "http://www.w3.org/TR/REC-html40/strict.dtd">
<%@ page import="java.sql.*"%><%@ page import="javax.sql.*"%><%@ page import="javax.naming.*"%><%@ page session="false" %><html><head><meta http-equiv="Content-Type" c><title></title><% out.print("我的測試開始");
DataSource ds = null; try{ InitialContext ctx=new InitialContext(); ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql"); Connection conn = ds.getConnection(); Statement stmt = conn.createStatement(); //提示:users必須是資料庫已有的表,
//這裡的資料庫前文提及的Data Source URL配置裡自主資料庫。
String strSql = " select * from users"; ResultSet rs = stmt.executeQuery(strSql); while(rs.next()){ out.print(rs.getString(1)); }
out.print("我的測試結束");
} catch(Exception ex){ out.print(“出現例外,資訊是:”+ex.getMessage());
ex.printStackTrace(); }%></head><body></body></html>運行結果:我的測試開始12345678我的測試結束,因為我的rs.getString(1) 在資料庫就是存放12345678
7、 總結
在h:/webspp/myweb/WEB-INF/lib/下是jstl的jar檔案,classes下為bean,h:/webspp/myweb/WEB-INF/下有web.xml(參考我的另一篇:在Tomcat中安裝JSTL )和8個tld檔案。mysql的驅動放在%tomcat_home%/common/lib/下,在%tomcat_home%/conf/Catalina/localhost/下建立myweb.xml,如下<Context path="" docBase="h:/webapp/myweb" debug="0"> <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_customer_log." suffix=".txt" timestamp="true"/> <ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSourcer"/></Context>