dbcp2、c3p0、druid连接池的简单配置
引入Maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>08-spring</artifactId>
<groupId>com.taotao</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>day37_jdbc</artifactId> <properties>
<junit.version>4.12</junit.version>
<spring.version>4.2.4.RELEASE</spring.version>
<slf4j.version>1.6.4</slf4j.version>
<mysql.version>5.1.8</mysql.version>
<dbcp.version>2.1.1</dbcp.version>
<c3p0.version>0.9.1.2</c3p0.version>
<druid.version>1.0.9</druid.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency> <dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies> </project>
配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate0">
<constructor-arg ref="dataSource" name="dataSource"/>
</bean> <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate1">
<constructor-arg ref="basicDataSource" name="dataSource"/>
</bean> <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate2">
<constructor-arg ref="comboPooledDataSource" name="dataSource"/>
</bean> <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate3">
<constructor-arg ref="druidDataSource" name="dataSource"/>
</bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:mysql://172.28.128.5:3306/spring_day03"/>
<property name="username" value="ttsc"/>
<property name="password" value="redhat"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="connectionProperties">
<props>
<prop key="useUnicode">yes</prop>
<prop key="characterEncoding">utf8</prop>
</props>
</property>
</bean> <bean class="org.apache.commons.dbcp2.BasicDataSource" id="basicDataSource">
<property name="url" value="jdbc:mysql://172.28.128.5:3306/spring_day03"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="ttsc"/>
<property name="password" value="redhat"/>
<property name="connectionProperties" value="useUnicode=yes;characterEncoding=utf8"/>
</bean> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="comboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="user" value="ttsc"/>
<property name="password" value="redhat"/>
<property name="jdbcUrl"
value="jdbc:mysql://172.28.128.5:3306/spring_day03?useUnicode=true&characterEncoding=utf-8"/>
</bean> <bean class="com.alibaba.druid.pool.DruidDataSource" id="druidDataSource" destroy-method="close">
<property name="url" value="jdbc:mysql://172.28.128.5:3306/spring_day03"/>
<property name="username" value="ttsc"/>
<property name="password" value="redhat"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="connectionProperties" value="useUnicode=yes;characterEncoding=utf8"/>
</bean> </beans>
编写测试代码
package com.itheima.demo1; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource;
import java.util.Properties; /**
* Created by Eric on 3/9/17.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:applicationContext.xml")
public class demo1 { @Resource(name = "jdbcTemplate0")
private JdbcTemplate template0; @Resource(name = "jdbcTemplate1")
private JdbcTemplate template1; @Resource(name = "jdbcTemplate2")
private JdbcTemplate template2; @Resource(name = "jdbcTemplate3")
private JdbcTemplate template3; @Test
public void run1() throws Exception { Properties properties = new Properties();
properties.put("useUnicode", "yes");
properties.put("characterEncoding", "utf8"); DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://172.28.128.5:3306/spring_day03");
dataSource.setUsername("ttsc");
dataSource.setPassword("redhat");
dataSource.setConnectionProperties(properties); JdbcTemplate template = new JdbcTemplate(dataSource);
template.update("INSERT INTO t_account VALUES (NULL ,?,?)", "小明", 1000);
} @Test
public void run2() throws Exception {
template0.update("INSERT INTO t_account VALUES (NULL ,?,?)", "冠希", 1000);
} @Test
public void run3() throws Exception {
template1.update("INSERT INTO t_account VALUES (NULL ,?,?)", "小强", 1000);
} @Test
public void run4() throws Exception {
template2.update("INSERT INTO t_account VALUES (NULL ,?,?)", "小美", 1000);
} @Test
public void run5() throws Exception {
template3.update("INSERT INTO t_account VALUES (NULL ,?,?)", "小六", 1000);
}
}
dbcp2、c3p0、druid连接池的简单配置的更多相关文章
- Druid连接池参数maxWait配置错误引发的问题
Druid连接池参数maxWait配置错误引发的问题 1. 背景 数据库服务器(服务部署在客户内网环境)的运行一段时间后,网卡出现了问题,导致所有服务都连接不上数据库,客户把网络恢复之后,反馈有个服务 ...
- Druid连接池简介和配置
Druid是什么?有什么作用? Druid首先是一个数据库连接池,但它不仅仅是一个数据库连接池,它还包含一个ProxyDriver,一系列内置的JDBC组件库,一个SQL Parser. Druid ...
- DRUID连接池的实用 配置详解
DRUID介绍 DRUID是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针 ...
- DRUID连接池的简单使用
DRUID——为监控而生的DB池 1. DRUID介绍 DRUID是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监 ...
- spring boot 学习(四)Druid连接池的使用配置
Druid介绍 Druid是一个JDBC组件,druid 是阿里开源在 github 上面的数据库连接池,它包括三部分: * DruidDriver 代理Driver,能够提供基于Filter-Cha ...
- springboot+druid连接池及监控配置
1. 问题描述 阿里巴巴的数据库连接池Druid在效率与稳定性都很高,被很多开发团队使用,并且自带的Druid监控也很好用,本章简单介绍下springboot+druid配置连接池及监控. 2. 解决 ...
- SpringBoot下Druid连接池的使用配置
Druid是一个JDBC组件,druid 是阿里开源在 github 上面的数据库连接池,它包括三部分: * DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体 ...
- Spring系列之集成Druid连接池及监控配置
前言 前一篇文章我们熟悉了HikariCP连接池,也了解到它的性能很高,今天我们讲一下另一款比较受欢迎的连接池:Druid,这是阿里开源的一款数据库连接池,它官网上声称:为监控而生!他可以实现页面监控 ...
- Spring Boot下Druid连接池的使用配置分析
https://blog.csdn.net/blueheart20/article/details/52384032
随机推荐
- centos 7 查看所有登录用户的操作历史
2019-01-07 转自 https://www.cnblogs.com/kevingrace/p/7373146.html centos 7 查看所有登录用户的操作历史 在Linux系统的环境下 ...
- Mac下Go2Shell打开配置界面
open -a Go2Shell --args config
- (转)MySQL出现同步延迟有哪些原因?如何解决?
http://oldboy.blog.51cto.com/2561410/1682147----MySQL出现同步延迟有哪些原因?如何解决? 原文:http://www.zjian.me/mysql/ ...
- linux 创建软链接和硬链接
Linux 系统中有软链接和硬链接两种特殊的“文件”. 软链接可以看作是Windows中的快捷方式,可以让你快速链接到目标档案或目录. 硬链接则透过文件系统的inode来产生新档名,而不是产生新档案. ...
- ES6 rest参数和扩展运算符
rest参数 ES6引入了rest参数(形式为“…变量名”).其中rest参数搭配的变量是一个数组可以使用数组的一切操作. 例: function rest(...values){ let sum=0 ...
- PHP之mb_convert_encoding使用
mb_convert_encoding (PHP 4 >= 4.0.6, PHP 5, PHP 7) mb_convert_encoding - Convert character encodi ...
- a+b+c的N次方展开
今天在给儿子看笔记本上的照片的时候,偶然发现了2009年手绘的一张a+b+c的N次方展开图,故写下面的博客以记之,为年轻时代的我点个赞! 20年前的我,一个充满激情的且富有专注力的数学发烧友! 10年 ...
- c++删除容器中的奇数
出自 c++ primer(4th)282页,26题 题意 数组ia[]={0,1,1,2,3,5,8,13,21,55,89};把ia复制到一个list容器中.使用单个迭代器参数版本的erase() ...
- Examples of GoF Design Patterns--摘录
http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns You can find an overview ...
- Cannot find module 'rxjs/operators/share'
描述: ionic项目,在使用了ngx-translate之后,项目编译完成,运行到浏览器时,出现如下错误: 其中ngx-translate参照官网最新教程使用,并且也尝试了angular4.3之前的 ...