tomcat6.0 连接池配置及测试用列

1.在tomcat中配置私有连接池

在%CATALINA_HOME%conf/server.xml中 配置一下信息

 

   <Context path="/JSPTEST2" reloadable="true" docBase="D:/xiehl/workspace/JSPTEST2" workDir="D:/xiehl/workspace/JSPTEST2/work" >
    

           <Resource name="jdbc/oracle"
             auth="Container"
             type="javax.sql.DataSource"
             driverClassName="oracle.jdbc.driver.OracleDriver"
             url="jdbc:oracle:thin:@xx.xx.xx.xx:1521:XE"
             username="xiehl"
             password="0455"
             maxActive="100"
             maxIdle="30"
            maxWait="10000"
          />
   </Context>

 

2.在web.xml中加入配置信息

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

 

3.测试用例(采用servlet) 必须在web工程中来测试

package com.ch13;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

public class GetConServlet extends HttpServlet {

 /**
  *
  */
 private static final long serialVersionUID = 1L;
 private Connection con;

 @Override
 public void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {

  System.out.println("doGet start");


  System.out.println("doGet end");
 }

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {

  doGet(req, resp);
 }
 
  System.out.println("constructor start");
  try{
   Connection conn = null;
   InitialContext ctx = new InitialContext();
   DataSource ds = null;
   ds = (DataSource) ctx.lookup("java:comp/env/jdbc/oracle"); 
   if(ds != null) {
    System.out.println("the ds is ok");
   }
   else {
    System.out.println("the ds is ng");
   }
   conn = ds.getConnection();
   if(conn!=null ) {
    System.out.println("the conn is ok");
   }
   else {
    System.out.println("the conn is ng");
   }
  }
  catch (Exception e) {
   e.printStackTrace();
  }
  System.out.println("constructor end");

 }
}

 

4.web.xml中配置servlet

<servlet>
 <servlet-name>Getcon</servlet-name>
 <servlet-class>com.ch13.GetConServlet</servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>Getcon</servlet-name>
 <url-pattern>/getcon</url-pattern>
</servlet-mapping>

 

5.启动tomcat

在ie中输入 http://localhost/JSPTEST2/getcon

 

控制台执行结果:

constructor start

the ds is ok
the conn is ok
constructor end

doGet start
doGet end

结束

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值