Spring产生数据源对象(c3p0连接池和druid连接池)

一、c3p0

1. 添加依赖

<dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version> 4.12</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.5.RELEASE</version>
</dependency>

2. 在resources中创建applicationContext.xml文件

3. 在applicationContext.xml中创建c3p0数据源对象

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
    <property name="user" value="root"></property>
    <property name="password" value="root"></property>
</bean>

标签解析

  • id:自己给该对象命名的唯一标签,dataSource就是我命名的
  • class:创建的对象所属类的地址,我创建的是ComboPooledDataSource类的对象,所以就是这个类的地址。如何获取类地址?点这里
  • 4个name:数据源参数的名字,根据类的相关变量的set方法命名。
 //创建数据源
 ComboPooledDataSource dataSource = new ComboPooledDataSource();
 //设置连接参数
 dataSource.setDriverClass("com.mysql.jdbc.Driver");
 dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
 dataSource.setUser("root");
 dataSource.setPassword("root");

解释:ComboPooledDataSource类的四个变量分别为driverClass,jdbcUrl,user,password,这四个名字的是根据四个set方法setDriverClass,setJdbcUrl,setUser,setPassword,去掉set得到的,且将首字母大写改为小写。即setDriverClass,则name=“driverClass”。

  • 4个value:为参数赋值

4.测试是否成功连接上数据库

public void test4() throws Exception{
   //创建Spring容器对象
   ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
   //获取ComboPooledDataSource类的对象(连接池数据源对象)
   DataSource dataSource = (DataSource) applicationContext.getBean("dataSource");
   //获取连接
   Connection connection = dataSource.getConnection();
   //打印连接
   System.out.println(connection);
   //连接放回连接池
   connection.close();
}

结果

com.mchange.v2.c3p0.impl.NewProxyConnection@4c40b76e

二、druid

1. 添加依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version> 4.12</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.5.RELEASE</version>
</dependency>

2. 在resources中创建applicationContext.xml文件

3. 在applicationContext.xml中创建druid数据源对象

<bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
</bean>

标签解析

  • id:自己给该对象命名的唯一标签,dataSource1就是我命名的
  • class:创建的对象所属类的地址,我创建的是DruidDataSource类的对象,所以就是这个类的地址。如何获取类地址?点这里
  • 4个name:数据源参数的名字,根据类的相关变量的set方法命名。
 //创建数据源
 DruidDataSource dataSource = new DruidDataSource();
 //设置连接参数
 dataSource.setDriverClassName("com.mysql.jdbc.Driver");
 dataSource.setUrl("jdbc:mysql://localhost:3306/test");
 dataSource.setUsername("root");
 dataSource.setPassword("root");

解释:DruidDataSource 类的四个变量分别为driverClassName,url,username,password,这四个名字的是根据四个set方法setDriverClassName,setUrl,setUsername,setPassword,去掉set得到的,且将首字母大写改为小写。即setDriverClassName,则name=“driverClassName”。(与c3p0的name有所不同)

  • 4个value:为参数赋值

4.测试是否成功连接上数据库

public void test5() throws Exception{
    //创建Spring容器对象
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    //获取DruidDataSource类的对象(连接池数据源对象)
    DataSource dataSource = (DataSource) applicationContext.getBean("dataSource1");
    //获取连接
    Connection connection = dataSource.getConnection();
    //打印连接
    System.out.println(connection);
    //连接放回连接池
    connection.close();
}

结果

com.mysql.jdbc.JDBC4Connection@402e37bc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值