tomcat5.5中DBCP数据源的配置

本文介绍了如何在Tomcat服务器中配置数据库连接池,并详细展示了server.xml和web.xml中的配置示例,最后通过JSP页面演示了如何使用该连接池进行数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先,在server.xml配置文件中按如下方式配置:

<Context docBase="shopping" path="/shopping" reloadable="true" source="org.eclipse.jst.j2ee.server:shopping">
          <Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="198744" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/mydb?autoReconnect=true"/>
  </Context>

然后在工程的web.xml中添加如下注册代码:

<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>

调试程序通过:

<%@ page contentType="text/html;charset=gb2312" language="java" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<html>
<head>
    <title>result.jsp</title>
</head>
<body>
    <%
    try{
        InitialContext ctx = new InitialContext();
        DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
        Connection con = ds.getConnection();
        Statement smt = con.createStatement();
        ResultSet rs = smt.executeQuery("select user_id, user_password, user_name, sex, birth, description from user order by birth asc");
        while(rs.next()){       
    %>
   
    <%=rs.getString("user_id") %>&nbsp;
    <%=rs.getString("user_password") %>&nbsp;
    <%=new String(rs.getString("user_name").getBytes("iso-8859-1")) %>&nbsp;
    <%=rs.getString("sex") %>&nbsp;
    <%=rs.getLong("birth") %>&nbsp;
    <%=rs.getString("description") %>&nbsp;
    <br>
    <%
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    %>
    <hr>
    <a href="jstl_exec.jsp">jstl_exec.jsp</a>
    <br>
    username:<%=request.getParameter("username") %>
    password:<%=request.getParameter("password")%>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值