当前位置:网站首页>Data source object management Druid and c3p0
Data source object management Druid and c3p0
2022-08-05 08:00:00 【Java hehe】
第一步导入相应的jar包(maven导入):
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
</dependencies>
第二步在Spring的配置文件中配置DataSource对象:
To be turned on before configurationcontext空间;And use the following configurationcontext空间加载properties文件
<context:property-placeholder location="classpath*:*.properties" system-properties-mode="NEVER"/>
这里的property-placeholder:属性占位符
classpath*是类路径 *表示所依赖的jar中的配置文件
system-properties-modee="NEVER" Indicates that the configuration in the environment variable is ignored
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath*:*.properties" system-properties-mode="NEVER"/>
<bean id="druid" class="com.alibaba.druid.pool.DruidDataSource">
<property name="name" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
</bean>
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="dataSourceName" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="driverClass" value="${jdbc.driver}"/>
</bean>
</beans>
write after configurationmainmethod to get the connection pool object:
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource;
import java.sql.SQLException;
public class MainApp{
public static void main(String[] args) throws SQLException {
ConfigurableApplicationContext appCon=new ClassPathXmlApplicationContext("ApplicationContext.xml");
DataSource Druid= (DataSource) appCon.getBean("druid");
DataSource c3p0= (DataSource) appCon.getBean("c3p0");
System.out.println(Druid.getConnection());
System.out.println(c3p0.getConnection());
}
}

这样我们就通过SpringManage third-party database connection pools,Get the connection pool object.从而获取到Connection对象.
边栏推荐
- The magic weapon for small entrepreneurs!
- 配合屏幕录像专家,又小又清晰!
- Antdesign a-select 下拉框超出长度换行显示
- Redis缓存以及存在的问题--缓存穿透、缓存雪崩、缓存击穿及解决方法
- Unity—物理引擎+“武器模块”
- P1160 队列安排
- Game Thinking 19: Multi-dimensional calculation related to games: point product, cross product, point-line-surface distance calculation
- MySQL 数据库 报错 The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)
- Support touch screen slider carousel plugin
- MM上街前的折腾(有趣)
猜你喜欢
随机推荐
[NOIP2010 提高组] 机器翻译
国家强制性灯具安全标准GB7000.1-2015
网络安全研究发现,P2E项目遭遇黑客攻击只是时间问题
作为一个男人必须明白的22个道理
Redis实现分布式锁-原理-问题详解
监听浏览器刷新操作
unity urp 渲染管线顶点偏移的实现
Codeforce 8.1-8.7做题记录
"Automatic Data Collection Based on R Language"--Chapter 3 XML and JSON
高效使用数码相机的诀窍
MM上街前的折腾(有趣)
An IP conflict is reported after installing the software on a dedicated computer terminal
TensorFlow installation steps
MySQL: order by sorting query, group by grouping query
Unity—物理引擎+“武器模块”
版本号命名规则
2006年星座运势全解-射手
YOLOv3 SPP理论详解(包括CIoU及Focal loss)
爬虫之验证码
Support touch screen slider carousel plugin









