Username: Password:

Tomcat连接池首次配置成功状态
来源:linux宝库作者:linux宝库 发布时间:2007-09-30 00:00:00


  一.环境变量:

  PATH:

  C:\j2sdk1.4.2\bin;

  JAVA_HOME:

  C:\j2sdk1.4.2

  注:仅此两个.

  二.SQL Server 驱动

  COPY

  Microsoft SQL Server 2000 Driver for JDBC\lib\目录下三个.jar :

  msbase.jar

  mssqlserver.jar

  msutil.jar

  To

  Tomcat5\common\lib

  三.虚拟目录和连接池

  假设要在D:\JsgTest建自己的虚拟目录

  /myjsp

  假设要建连接池jndi 名为aaa

  假设连接数据库为pubs

  则在

  Tomcat5\conf\Catalina\localhost

  目录下新建文档:

  myjsp.xml

  内容如下:

  

  

  

  

  

  url

  jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs

  


  

  password

  kingapex

  


  

  maxActive

  4

  


  

  maxWait

  5000

  


  

  driverClassName

  com.microsoft.jdbc.sqlserver.SQLServerDriver

  


  

  username

  sa

  


  

  maxIdle

  2

  


  


  


  测试:

  在D:\JsgTest目录下建文档:Test.jsp:

  <%@ page contentType="text/html;charset=gb2312"%>

  <%@ page import="java.sql.*"%>

  <%@ page import="javax.sql.*"%>

  <%@ page import="javax.naming.*"%>

  <%

  Connection conn = null;

  Context initCtx = new InitialContext();

  if (initCtx == null)

  throw new Exception("不能获取Context!");

  Context ctx = (Context) initCtx.lookup("java:comp/env");

  //获取连接池对象

  Object obj = (Object) ctx.lookup("aaa");

  //类型转换

  javax.sql.DataSource ds = (javax.sql.DataSource) obj;

  conn = ds.getConnection();

  Statement stmt = conn.createStatement();

  String strSql = "select * from [titles]";

  ResultSet rs = stmt.executeQuery(strSql);

  while(rs.next()){

  out.println(rs.getString(1));

  }

  rs.close();

  stmt.close();

  conn.close();

  out.println("连接池测试成功");

  %>

  测试路径:

  http://localhost:8080/myjsp/Test.jsp

喜欢本文,那就收藏到:

    Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
相关评论  我也要评论
还没有关于此文章的相关评论!
  • 昵称: (为空则显示guest)
  • 评论分数: ★ ★ ★★★ ★★★★ ★★★★★
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • 导航
    赞助商
    文章类别
    订阅