TomCat 5.5 配置MSSQL数据库连接池

昨天一天,找到网上的文章都不全面。最后配置成功,其实很简单的。也许是我初学,走了弯路。

安装略过。。。版本 TomCat 5.5

首先拷贝下面三个文件到%TOMCAT_HOME%/common/lib 下面

msbase.jar

mssqlserver.jar

msutil.jar

下载地址 http://www.microsoft.com/downloads/details.aspx?familyid=9f1874b6-f8e1-4bd6-947c-0fc5bf05bf71&displaylang=en
下载安装后在安装目录中可以找到上面三个文件

修改以下三个文件

1 下面是server.xml的部分文件 需要在<GlobalNamingResources>和</GlobalNamingResources>添加下面代码

===================server.xml=======================


    <Resource
      auth="Container"
      description="User database that can be updated and saved"
      name="UserDatabase"
      type="org.apache.catalina.UserDatabase"
      pathname="conf/tomcat-users.xml"
      factory="org.apache.catalina.users.MemoryUserDatabaseFactory"/>
    <Resource
      name="jdbc/pcd"
      type="javax.sql.DataSource"
      driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
      password=""
      maxIdle="2"
      maxWait="5000"
      username="sa"
      url="jdbc:microsoft:sqlserver://172.16.1.140:1433;DatabaseName=pubs"
      maxActive="4"/>

2 下面是context.xml的完整文件

===================context.xml=========================

<Context>
<Resource name="jdbc/pcd"
 auth="Container"
       type="javax.sql.DataSource"
 driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
      password=""
      maxIdle="2"
      maxWait="5000"
      username="sa"
      url="jdbc:microsoft:sqlserver://172.16.1.140:1433;DatabaseName=pubs"
      maxActive="4" />
</Context>

3 在web.xml 的<web-app></web-app>之间添加下面的代码

==================web.xml======================

   <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/pcd</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
   </resource-ref>

修改完毕,重新启动tomcat

最后测试文件上场

===================test.jsp==============================

 <%@ page contentType="text/html; charset=gb2312" %>
 <%@ page import="javax.naming.Context" %>
 <%@ page import="javax.sql.DataSource"%>
 <%@ page import="javax.naming.InitialContext"%>
 <%@ page import="java.sql.*"%>

 <%
   DataSource ds = null;
   try{
   
    Context initCtx = new InitialContext();
     Context envCtx = (Context) initCtx.lookup("java:comp/env");
    //从Context中lookup数据源。
     ds = (DataSource)envCtx.lookup("jdbc/pcd");
 if(ds!=null)
 {
 out.println("已经获得DataSource!");
 out.println("<br>");
 Connection conn = ds.getConnection();
 Statement stmt=conn.createStatement();
 ResultSet rst=stmt.executeQuery("select top 10 job_desc from jobs");
 out.println("以下是从数据库中读取出来的数据");
 while(rst.next())
 {
 out.println("bookName:"+rst.getString("job_desc"));
 out.println("<br>");
 }
 }
 else
  out.println("连接失败!");
 }
 catch(Exception ne)
 {
  out.println(ne);
 }
 %>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值