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

    这里以MySQL为例:

    首先是conf目录下的server.xml

    GlobalNamingResources中添加:

        <Resource
          name="java/mysql"
          type="javax.sql.DataSource"
          password=""
          driverClassName="com.mysql.jdbc.Driver"
          maxIdle="2"
          maxWait="5000"
          username="root"
          url="jdbc:mysql://127.0.0.1:3306/edoas2"
          maxActive="20"/>

    在content.xml中添加:

    <ResourceLink
       name="java/mysql"
       type="javax.sql.DataSource"
       global="java/mysql"/>

    在所在应用中的web.xml中添加:

       <resource-ref>
        <description>MySQL DB Connection Pool</description>
        <res-ref-name>java/mysql</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
       </resource-ref>

    注意保持黑体部分的一致性,这样在程序中就可以使用了:

         out.print("DB Test Start......<br>");
        
         DataSource ds=null;
        
         try{
        
         InitialContext ctx=new InitialContext();
        
         ds=(DataSource)ctx.lookup("java:comp/env/java/mysql");
        
         Connection conn=ds.getConnection();
         conn.close();
         out.print("DB Test Success......");
        
         }catch(Exception ex){
         
          out.print("DB Test hava a Error....."+ex.getMessage());
          ex.printStackTrace();
        
         }

  • 相关阅读:
    219. Contains Duplicate II
    189. Rotate Array
    169. Majority Element
    122. Best Time to Buy and Sell Stock II
    121. Best Time to Buy and Sell Stock
    119. Pascal's Triangle II
    118. Pascal's Triangle
    88. Merge Sorted Array
    53. Maximum Subarray
    CodeForces 359D Pair of Numbers (暴力)
  • 原文地址:https://www.cnblogs.com/macula7/p/1960479.html
Copyright © 2011-2022 走看看