Tomcat 5.5.X JNDI 连接池

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

在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 >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值