zoukankan      html  css  js  c++  java
  • Tomcat数据库连接池配置

    Tomcat数据库连接池配置

    1.            Server.xml的配置

    (1)找到tomcat所在目录下的confserver.xml文件

    (2)在文件最后一个</host>前加入如下代码

    <Context path="/website" docBase="website" debug="0" reloadable="true">

          <Resource   <!-- /website为开发目录,可自定-->

    name="jdbc/connectDB"    <!—连接名称,connectDB可自定 -->

           auth="Container"

           type="javax.sql.DataSource"

           maxActive="20"      

           maxIdle="5"

           maxWait="10000"

           username="sa"      <!--SqlServer2000登陆名称,与你的数据库设置应相同-->   

           password="123456"  <!—SqlServer2000密码,自定-->

           factory="org.apache.commons.dbcp.BasicDataSourceFactory"

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

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

           <!—这里要特别注意,DatabaseName为建立好的数据库名,首先要保证数据库存在,其次,在等号后边和数据库名称之间不能有空格,除非你的数据库名称本身就含有空格,如写成这样DatabaseNae=  website,那么你的数据库名称就是“_website”而不是”website”了,下划线标出了空格所在的位置,如果数据库名称打错,调试时会报:org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory ([Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]无法打开登录 'db_web' 中请求的数据库。登录失败。)-->

        </Context>

    2.            web.xml的配置

    (1)找到开发目录下的WEB-INFweb.xml文件

    (2)在其中的<webapp></webapp>之间加入如下代码

    <resource-ref>

                   <description>SQL server text app</description>

                   <res-ref-name>jdbc/connectDB</res-ref-name> <!—这个名称一定要与之前定义的连接名称相同!-->

                   <res-type>javax.sql.DataSource</res-type>

                   <res-auth>Container</res-auth>

        </resource-ref>

    3.            程序内部引用:

    在程序内部,按以下代码建立数据库连接池:

    Context ctx = new InitialContext();

    DataSource ds = (DataSource)ctx.lookup(“java:comp/env/jdbc/connectDB”);

    Connection con = ds.getConnection();

    然后就可以使用数据库了

  • 相关阅读:
    ExecuteScalar requires the command to have a transaction when the connection assigned to the command is in a pending
    如何从vss中分离程序
    String or binary data would be truncated
    the pop3 service failed to retrieve authentication type and cannot continue
    The POP3 service failed to start because
    IIS Error he system cannot find the file specified _找不到页面
    pku2575Jolly Jumpers
    pku2940Wine Trading in Gergovia
    pku3219二项式系数
    pku1029false coin
  • 原文地址:https://www.cnblogs.com/frankzone/p/7751524.html
Copyright © 2011-2022 走看看