项目使用druid连接池

步骤一 :pom.xml 
  <!--jar package of druid  configuration-->
                 < dependency >
               < groupId > com.alibaba </ groupId >
               < artifactId > druid </ artifactId >
               < version > 1.0.24 </ version >
           </ dependency >

步骤二:   applicationContext.xml
  <!-- 阿里 druid 数据库连接池 -->  
< bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource"
            init-method = "init" destroy-method = "close" >
            <!-- 数据库基本信息配置 -->
            < property name = "driverClassName" value = "${driverClassName}" />
            < property name = "url" value = "${url}" />
            < property name = "username" value = "${username}" />
            < property name = "password" value = "${password}" />            
            <!-- 初始化连接数量 -->
            < property name = "initialSize" value = "${initialSize}" />
            <!-- 最大并发连接数 -->
            < property name = "maxActive" value = "${maxActive}" />
            <!-- 最大空闲连接数 -->
            < property name = "maxIdle" value = "${maxIdle}" />
            <!-- 最小空闲连接数 -->
            < property name = "minIdle" value = "${minIdle}" />
            <!-- 配置获取连接等待超时的时间 -->            
            < property name = "maxWait" value = "${maxWait}" />
            <!-- 超过时间限制是否回收 -->
            < property name = "removeAbandoned" value = "${removeAbandoned}" />
            <!-- 超过时间限制多长; -->
            < property name = "removeAbandonedTimeout" value = "${removeAbandonedTimeout}" />
            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
            < property name = "timeBetweenEvictionRunsMillis" value = "${timeBetweenEvictionRunsMillis}" />
            <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
            < property name = "minEvictableIdleTimeMillis" value = "${minEvictableIdleTimeMillis}" />
            <!-- 用来检测连接是否有效的sql,要求是一个查询语句-->      
            < property name = "validationQuery" value = "${validationQuery}" />
            <!-- 申请连接的时候检测 -->
            < property name = "testWhileIdle" value = "${testWhileIdle}" />
            <!-- 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
            < property name = "testOnBorrow" value = "${testOnBorrow}" />
            <!-- 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能  -->
            < property name = "testOnReturn" value = "${testOnReturn}" />
            <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
            < property name = "poolPreparedStatements" value = "${poolPreparedStatements}" />        
            < property name = "maxPoolPreparedStatementPerConnectionSize" value = "${maxPoolPreparedStatementPerConnectionSize}" />
            <!--属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:                     
                     监控统计用的filter:stat
                     日志用的filter:log4j
                     防御SQL注入的filter:wall -->
            < property name = "filters" value = "${filters}" />        
      </ bean >

步骤三:jdbc.properties
       
driverClassName = oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:@127.0.0.1:1521:ORCL
username = apm
password = root
initialSize = 5
maxActive = 10
minIdle = 3
maxIdle = 10
maxWait = 60000
removeAbandoned = true
removeAbandonedTimeout = 180
timeBetweenEvictionRunsMillis = 60000
minEvictableIdleTimeMillis = 300000
validationQuery = SELECT 1 FROM DUAL
testWhileIdle = true
testOnBorrow = false
testOnReturn = false
poolPreparedStatements = true
maxPoolPreparedStatementPerConnectionSize = 50
filters = stat

步骤四:web.xml
      <!-- 连接池 启用 Web 监控统计功能    start-->  
     < filter >
      < filter-name > DruidWebStatFilter </ filter-name >
      < filter-class > com.alibaba.druid.support.http.WebStatFilter </ filter-class >
      < init-param >
        < param-name > exclusions </ param-name >
        < param-value > *.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/* </ param-value >
      </ init-param >
</ filter >
< filter-mapping >
     < filter-name > DruidWebStatFilter </ filter-name >
     < url-pattern > /* </ url-pattern >
</ filter-mapping >     
< servlet >
    < servlet-name > DruidStatView </ servlet-name >
    < servlet-class > com.alibaba.druid.support.http.StatViewServlet </ servlet-class >    
</ servlet >  
< servlet-mapping >
    < servlet-name > DruidStatView </ servlet-name >
    < url-pattern > /druid/* </ url-pattern >
</ servlet-mapping >
  <!-- 连接池 启用 Web 监控统计功能    end-->  
   

监控的url为: http://localhost:8888/xxzx /druid/index.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Druid是阿里巴巴开源的一个数据库连接池,它具有连接池的基本功能,同还提供了监控、防御SQL注入攻击、缓存等高级功能。下面是使用Druid连接池进行JDBC操作的步骤: 1. 引入Druid的依赖 ``` <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> ``` 2. 配置Druid连接池 可以通过properties文件、xml文件或者代码配置Druid连接池,这里以代码配置为例: ``` import com.alibaba.druid.pool.DruidDataSource; import java.sql.Connection; import java.sql.SQLException; public class DruidUtils { private static DruidDataSource dataSource = null; static { dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/test"); dataSource.setUsername("root"); dataSource.setPassword("123456"); } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } } ``` 3. 使用Druid连接池获取数据库连接 ``` Connection conn = DruidUtils.getConnection(); PreparedStatement ps = conn.prepareStatement("SELECT * FROM user WHERE id = ?"); ps.setInt(1, 1); ResultSet rs = ps.executeQuery(); while (rs.next()) { System.out.println(rs.getString("name")); } rs.close(); ps.close(); conn.close(); ``` 4. 关闭连接 使用完数据库连接后,需要关闭连接,释放资源。 ``` if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } ``` 以上就是使用Druid连接池进行JDBC操作的基本步骤,可以有效提高数据库连接的使用效率和安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值