随笔-153  评论-235  文章-19  trackbacks-0

今天第二次配置连接池,写下来以便以后可以参考。

在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 流浪汗 阅读(635) 评论(0)  编辑  收藏 所属分类: Tomcat

只有注册用户登录后才能发表评论。


网站导航: