今天第二次配置連接池,寫下來以便以后可以參考。
在Tomcat/webapps/目錄下建立DBTest目錄(即為服務目錄)
DBTest建立WEB-INF目錄。
1.WEB-INF目錄下創建web.xml文件,如下:
< web-app xmlns ="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version ="2.4" >
< description > MySQL Test App </ description >
< resource-ref >
< description > DB Connection </ description >
< res-ref-name > jdbc/TestDB </ res-ref-name >
< res-type > javax.sql.DataSource </ res-type >
< res-auth > Container </ res-auth >
</ resource-ref >
</ web-app >
2.再Tomcat/conf/目錄的server.xml文件里</Host>之前加:
<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test?autoReconnect=true"/>
</Context>
3.把MySQL的JDBC驅動程序放到Tomcat/commons/lib/目錄下,jstl.jar和standard.jar放到webapps/DBTest/WEB-INF/lib/目錄下。
4.創建數據庫表:
CREATE TABLE testdata (
id int NOT NULL auto_increment PRIMARY KEY,
name varchar(50),
email varchar(50)
) ENGINE=MyISAM;
5.測試頁面test.jsp:
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<sql:query var="rs" dataSource="jdbc/TestDB">
select id, name, email from testdata
</sql:query>
<html>
<head>
<title>DB Test</title>
</head>
<body>
<h2>Results</h2>
<c:forEach var="row" items="${rs.rows}">
姓名:${row.name}<br/>
郵箱: ${row.email}<br/>
</c:forEach>
</body>
</html>
posted on 2007-03-12 19:26
流浪汗 閱讀(655)
評論(0) 編輯 收藏 所屬分類:
Tomcat