tomcat 数据库连接池

一。复制数据库连接器.jar包 到tomcat$\common\lib下,因为在项目\WEB-INF\lib 下是没用的,它是由tomcat 管理的

二。tomcat\conf\servlet.xml,下面是个人项目源码{
<?xml version='1.0' encoding='utf-8'?>
<Server>
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
  <GlobalNamingResources>
    <Environment name="simpleValue" type="java.lang.Integer" value="30"/>
    <Resource auth="Container" description="User database that can be updated and saved" name="UserDatabase" type="org.apache.catalina.UserDatabase"/>
   
  <ResourceParams name="UserDatabase">
  <parameter>
   <name>factory</name>
   <value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
  </parameter>
  <parameter>
   <name>pathname</name>
   <value>conf/tomcat-users.xml</value>
  </parameter>
 </ResourceParams>
  </GlobalNamingResources>
  <Service name="Catalina">
    <Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" port="8080" redirectPort="8443" maxSpareThreads="75" maxThreads="150" minSpareThreads="25">
    </Connector>
    <Connector port="8009" protocol="AJP/1.3" protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler" redirectPort="8443">
    </Connector>
    <Engine defaultHost="localhost" name="Catalina">
      <Host appBase="webapps" name="localhost">
        <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_log." suffix=".txt" timestamp="true"/>

<!-- Ibox_two 与项目同名-->

  <Context path=" /Ibox_two" docBase="Ibox_two" debug="0" crosscontext="true" reloadable="true">
   <Resource name="jdbc/testdt" auth="Container" type="javax.sql.DataSource" />

   <ResourceParams name="jdbc/testdt">
     <parameter>
    <name>maxWait</name>
    <value>5000</value>
     </parameter>
     <parameter>
    <name>maxActive</name>
    <value>4</value>
     </parameter>
     <parameter>
    <name>password</name>
    <value>1234</value>
     </parameter>
     <parameter>
    <name>url</name>
    <value>jdbc:mysql://127.0.0.1:3306/test</value>
     </parameter>
     <parameter>
    <name>driverClassName</name>
    <value>com.mysql.jdbc.Driver</value>
     </parameter>
     <parameter>
    <name>maxIdle</name>
    <value>2</value>
     </parameter>
     <parameter>
    <name>username</name>
    <value>root</value>
     </parameter>
   </ResourceParams>
  </Context>
      </Host>
      <Logger className="org.apache.catalina.logger.FileLogger" prefix="catalina_log." suffix=".txt" timestamp="true"/>
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
    </Engine>
  </Service>
</Server>


}



三。项目\WEB-INF\web.xml ,下面源码,个人项目源码{

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>com.servlet.ServletAction</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  

    <!-- 配置DWR的核心Servlet --> 
    <servlet> 
        <!-- 指定DWR核心Servlet的名字 --> 
        <servlet-name>dwr-invoker</servlet-name> 
        <!-- 指定DWR核心Servlet的实现类 --> 
        <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class> 
        <!--  指定DWR核心Servlet处于调试状态 --> 
        <init-param> 
            <param-name>debug</param-name> 
            <param-value>true</param-value> 
        </init-param> 
        <!-- 设置使用反向Ajax技术 --> 
        <init-param> 
            <param-name>pollAndCometEnabled</param-name> 
            <param-value>true</param-value> 
        </init-param> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 

    <!-- 指定核心Servlet的URL映射 --> 
    <servlet-mapping> 
        <servlet-name>dwr-invoker</servlet-name> 
        <!-- 指定核心Servlet映射的URL --> 
        <url-pattern>/dwr/*</url-pattern> 
    </servlet-mapping> 
  
  <welcome-file-list>
   <welcome-file>index.html</welcome-file>
   <welcome-file>index.jsp</welcome-file>
   <welcome-file>index.do</welcome-file>
  </welcome-file-list>
  
  <resource-ref>
  <description>MySQL DB Connection Pool</description>
  <res-ref-name>jdbc/testdt</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
  <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>


</web-app>
}

四。使用

public ActionForward dotest(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){

  try{              
   System.out.println("test Start ==============");
   Context cont = (Context) new InitialContext().lookup("java:comp/env");//固定
         DataSource ds = (DataSource)cont.lookup("jdbc/testdt");
         Connection con = ds.getConnection();
         PreparedStatement ps = con.prepareStatement("select * from aaa");
         ResultSet rs = ps.executeQuery();
         while(rs.next()){
          System.out.println("=========="+rs.getInt(1)+"  "+rs.getString(2));
         }

    con.close();//要记得关 不然会用尽连接池而报org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool exhausted
         System.out.println("====================");
  }catch (Exception e) {
   e.printStackTrace();
  }

}
  

要在web项目中运行,因为它是tomcat 建的连接,一般的java 类运行不了

开始以为 Ajax 不能调用,后来运行是可以的

第二步我用了网上的很多种方法都不行,试运行成功的只的这一种

你可能感兴趣的:(tomcat,mysql,Ajax,servlet,DWR)