Tomcat5.5配置连接池

本文详细介绍如何在Tomcat中配置SQL Server数据库连接,包括所需驱动包的安装、连接池设置、JNDI资源引用及页面测试代码。

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

1.配置驱动包
将如下三个sql2000的驱动jar包复制到tomcat-root/common/lib目录下
msbase.jar
mssqlserver.jar
msutil.jar
(注:tomcat-root:tomcat的根目录)
2.配置连接池
在tomcat-root/conf/server.xml里添加以下红色代码片段:

<!-- 上面省略 -->
<Host name="localhost" appBase="webapps" unpackWARs="true"
    autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
    <Context path="/connPoll" docBase="connPoll" debug="5"
       reloadable="true" crossContext="true">
       <Resource name="jdbc/EmployeeDB" auth="Container"
           type="javax.sql.DataSource" username="sa" password=""
           driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
           url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"
           maxActive="8" maxIdle="4" />
    </Context>
</Host>
<!-- 下面省略 -->

3.在web.xml里添加JNDI资源的引用,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 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">
    <resource-ref>
       <description>
           Resource reference to a factory for java.sql.Connection
           instances that may be used for talking to a particular
           database that is configured in the server.xml file.
       </description>
       <res-ref-name>jdbc/EmployeeDB</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

4.页面测试
test.jsp

<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%>
<%@ page import="java.sql.*,javax.sql.*,javax.naming.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
       <title>Tomcat5.5连接池测试</title>
    </head>
    <body>
       <%
           Context initCtx = new InitialContext();
           Context envCtx = (Context) initCtx.lookup("java:comp/env");
           DataSource ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB");
           Connection conn = ds.getConnection();
           Statement sta = conn.createStatement();
           ResultSet rs = sta.executeQuery("select * from authors");
           while (rs.next()) {
              out.println(rs.getString("au_lname") + "<br>");
           }
           conn.close();
       %>
    </body>
</html>


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值