美文网首页
servlet整合druid

servlet整合druid

作者: 艾特小师叔 | 来源:发表于2019-07-27 21:41 被阅读0次

    servlet整合druid

    druid.properties文件

    #druid配置文件
    #驱动
    driverClassName=com.mysql.jdbc.Driver
    #url
    url=jdbc:mysql://127.0.0.1:3306/cms?useUnicode=true&characterEncoding=utf8&useSSL=true
    #用户名
    username=root
    #密码
    password=root
    #连接池初始化大小
    initialSize=10
    #活动链接
    maxActive=10
    #最小连接数
    minIdle=10
    #使用的内置过滤器  若不配置 则不会统计SQL执行
    filters=stat
    

    DruidUtil.java

    package com.mrt.util;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.SQLException;
    import java.util.Properties;
    
    import com.alibaba.druid.pool.DruidDataSource;
    import com.alibaba.druid.pool.DruidDataSourceFactory;
    
    /**
     * Druid连接池
     * @author tangpengfei
     */
    public class DruidUtil {
        
        static DruidDataSource dataSource;
        
        static {
            Properties prop = new Properties();
            try {
                prop.load(DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties"));
                dataSource = (DruidDataSource)DruidDataSourceFactory.createDataSource(prop);
                //dataSource.addFilters("stat,log4j,wall");
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public static Connection getConn() {
            try {
                return dataSource.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    

    web.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>servlet_druid</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      
      <servlet>
        <servlet-name>StatViewServlet</servlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
        <init-param>  
        <!-- 允许清空统计数据 -->  
        <param-name>resetEnable</param-name>  
        <param-value>true</param-value>  
        </init-param>  
        <init-param>  
        <!-- 用户名 -->  
        <param-name>loginUsername</param-name>  
        <param-value>admin</param-value>  
        </init-param>  
        <init-param>  
        <!-- 密码 -->  
        <param-name>loginPassword</param-name>  
        <param-value>admin</param-value>  
        </init-param>  
      </servlet>
      
      <servlet-mapping>
        <servlet-name>StatViewServlet</servlet-name>
        <url-pattern>/druid/*</url-pattern>
      </servlet-mapping>
      
    </web-app>
    

    相关文章

      网友评论

          本文标题:servlet整合druid

          本文链接:https://www.haomeiwen.com/subject/vlryrctx.html