Tomcat配置SQLServer连接池

1.       进入tomcat目录下的conf文件夹目录里,找到Server.xml

tomcat的Server.xml文件中在</Context>标签与</Host>标签里,定议连接语句,格式如下

<!—path定议你的应用程序的目录所 ,/DBTest表示在Tomcat Webapps目录下à

<!—docBase=”DBTest” 表示你执行止程序时路径名称,例如以下的执行路径就是http://localhost:8080/DBTest-->

<Context path="/DBTest" docBase="DBTest"

        debug="5" reloadable="true" crossContext="true">

    <!-- maxActive: 连接池的最大数量,要确保有足够的连接数-->

    <!-- maxIdle: 最大空闲连接数,设置为-1即表示不限制-->

    <!-- maxWait:最长等待连接时间(最大等待连接池反回可用的时间), 以纳秒为单位,即设为10000相等于10秒,如果设置成-1表示不确定-->

    <!-- username and password: 连接数据库使用的帐号与密码  -->

    <!-- driverClassName:连接数据库的驱动程序,SQLServer就是

com.microsoft.jdbc.sqlserver.SQLServerDriver.-->

    <!-- url: 连接数据库路径,

jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=KB_Rate-->

 

 

 

  <Resource name=" Default_JDBC " auth="Container" type="javax.sql.DataSource"

               maxActive="100" maxIdle="30" maxWait="10000"

               username="javauser" password="javadude"

driverClassName=" com.microsoft.jdbc.sqlserver.SQLServerDriver "

            url=" jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=KB_Rate""/>

 

 

 

</Context>

 

 

 

配置完成后,DBTest目录下添加一个Test文件,如下:

<%@ 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("/Default_JDBC ");//获取连接池对象

      javax.sql.DataSource ds = (javax.sql.DataSource) obj; //类型转换

      conn = ds.getConnection();

  Statement stmt = conn.createStatement();

  PreparedStatement ps=conn.prepareStatement("select * from FinalOrderdata");

  ResultSet rs=ps.executeQuery();

  while(rs.next()){

      out.println(rs.getString(1)+"<BR>"); 

      i++;

  }

rs.close();

stmt.close();

conn.close();

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

 

 

 

到于TOMCAT5.0版本的设置也是差不多,不过在Server.xml中添加连接池设置语句上有所有不,可参考

http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html

其它操作也是差不多,

<Context path="/DBTest" docBase="DBTest"

        debug="5" reloadable="true" crossContext="true">

<Context path="/DBTest" docBase="DBTest"

        debug="5" reloadable="true" crossContext="true">

  <Logger className="org.apache.catalina.logger.FileLogger"

             prefix="localhost_DBTest_log." suffix=".txt"

             timestamp="true"/>

  <Resource name="jdbc/TestDB"

               auth="Container"

               type="javax.sql.DataSource"/>

  <ResourceParams name="jdbc/TestDB">

    <parameter>

      <name>factory</name>

      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>

    </parameter>

    <!-- Maximum number of dB connections in pool. Make sure you

         configure your mysqld max_connections large enough to handle

         all of your db connections. Set to 0 for no limit.

         -->

    <parameter>

      <name>maxActive</name>

      <value>100</value>

    </parameter>

    <!-- Maximum number of idle dB connections to retain in pool.

         Set to -1 for no limit.  See also the DBCP documentation on this

         and the minEvictableIdleTimeMillis configuration parameter.

         -->

    <parameter>

      <name>maxIdle</name>

      <value>30</value>

    </parameter>

    <!-- Maximum time to wait for a dB connection to become available

         in ms, in this example 10 seconds. An Exception is thrown if

         this timeout is exceeded.  Set to -1 to wait indefinitely.

         -->

    <parameter>

      <name>maxWait</name>

      <value>10000</value>

    </parameter>

    <!-- MySQL dB username and password for dB connections  -->

    <parameter>

     <name>username</name>

     <value>javauser</value>

    </parameter>

    <parameter>

     <name>password</name>

     <value>javadude</value>

    </parameter>

    <!-- Class name for the old mm.mysql JDBC driver - uncomment this entry and comment next

         if you want to use this driver - we recommend using Connector/J though

    <parameter>

       <name>driver

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值