<!-- 配置C3P0连接池 -->
<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring3_day02"></property>
<property name="user" value="root"></property>
<property name="password" value=""></property> </bean>

配置好连接池之后把它注入到JDBC模板.

21:55:24,695  INFO XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [applicationContext.xml]
21:55:24,969 INFO GenericApplicationContext:510 - Refreshing org.springframework.context.support.GenericApplicationContext@43401ab6: startup date [Fri May 05 21:55:24 CST 2017]; root of context hierarchy
21:55:25,127 INFO DefaultListableBeanFactory:577 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@cf7e5b2: defining beans [dataSource,dataSource1,dataSource2,jdbcTemplate,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
21:55:25,170 INFO DriverManagerDataSource:153 - Loaded JDBC driver: com.mysql.jdbc.Driver
21:55:25,209 INFO MLog:80 - MLog clients using log4j logging.
21:55:25,394 INFO C3P0Registry:204 - Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
21:55:25,564 INFO AbstractPoolBackedDataSource:462 - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 2ygg1c9n1o6hot718sug49|4706c7b2, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 2ygg1c9n1o6hot718sug49|4706c7b2, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql:///spring3_day02, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
21:55:25,975 INFO GenericApplicationContext:1042 - Closing org.springframework.context.support.GenericApplicationContext@43401ab6: startup date [Fri May 05 21:55:24 CST 2017]; root of context hierarchy
21:55:25,975 INFO DefaultListableBeanFactory:444 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@cf7e5b2: defining beans [dataSource,dataSource1,dataSource2,jdbcTemplate,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy

初始化C3P0的连接池等等的一些信息.


<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入beans的头 -->
<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">
<!-- 配置Spring默认的连接池 -->
<!-- 这个类由Spring来帮我们创建,它默认情况下只创建一次,因为是单例的. -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///spring3_day02"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property> </bean>
<!-- 配置DBCP连接池 -->
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///spring3_day02"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property> </bean> <!-- 配置C3P0连接池 -->
<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring3_day02"></property>
<property name="user" value="root"></property>
<property name="password" value=""></property> </bean>
<!-- 定义jdbctemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource2"></property><!-- 把上面定义好的连接池注入进来了 -->
</bean>
</beans>
package cn.itcast.spring3.demo1;

import java.sql.DriverManager;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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 org.springframework.transaction.jta.SpringJtaSynchronizationAdapter;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTest1 { @Autowired
@Qualifier("jdbcTemplate")
private JdbcTemplate jdbcTemplate;//注入Jdbc模板
@Test
public void demo2(){
jdbcTemplate.execute("create table user (id int primary key auto_increment,name varchar(20))");
} @Test
public void demo1(){
// 创建连接池:
DriverManagerDataSource dataSource = new DriverManagerDataSource();//Spring自带的连接池
// 设置参数:
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///spring3_day02");
dataSource.setUsername("root");
dataSource.setPassword(""); //使用JDBC的模板:
//JdbcTemplate jdbcTemplate = new JdbcTemplate();
//jdbcTemplate.setDataSource(dataSource);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("create table user (id int primary key auto_increment,name varchar(20))");
}
}

day39-Spring 15-Spring的JDBC模板:C3P0连接池配置的更多相关文章

  1. Spring之c3p0连接池配置和使用

    1.导入包:c3p0和mchange包 2.代码实现方式: package helloworld.pools; import com.mchange.v2.c3p0.ComboPooledDataSo ...

  2. 【JDBC】C3P0连接池的使用

    C3P0连接池的c3p0-config.xml配置文件 <?xml version="1.0" encoding="UTF-8"?> <c3p ...

  3. C3p0连接池配置

    在Java开发中,使用JDBC操作数据库的四个步骤如下:   ①加载数据库驱动程序(Class.forName("数据库驱动类");)   ②连接数据库(Connection co ...

  4. C3P0连接池配置(C3P0Utils.java)

    配置文件 c3p0-config.xml <?xml version="1.0" encoding="UTF-8"?> <c3p0-confi ...

  5. (30)java web的hibernate使用-c3p0连接池配置

    hibernate支持c3p0连接池 需要导入c3p0的jar包 <!-- 配置连接驱动管理类 --> <property name="hibernate.connecti ...

  6. Spring c3p0连接池配置

    数据库连接池 数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”.预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕之后再放回去.我们可以通过设定连接 ...

  7. spring hibernate4 c3p0连接池配置

    c3p0-0.9.1.2.jar,c3p0-oracle-thin-extras-0.9.1.2.jar,点此下载 <bean id="dataSource" class=& ...

  8. C3P0连接池配置方式

    c3p0的配置方式分为三种,分别是 1.setters一个个地设置各个配置项 2.类路径下提供一个c3p0.properties文件 3.类路径下提供一个c3p0-config.xml文件 1.set ...

  9. C3P0连接池配置和实现详解

    一.配置 <c3p0-config> <default-config> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: 3 --> ...

随机推荐

  1. crontab定时任务语法及应用

    https://mp.weixin.qq.com/s/Oi9hppNQMeFiQo9s-ge79A crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows ...

  2. 【MFC 】关于对话框中的OnVScroll() 和 OnHScroll

    原文地址:[MFC 中]关于对话框中的OnVScroll() 和 OnHScroll()函数作者:Winters     对话框中的滑块,微调控件都会向OnVScroll() 和OnHScroll() ...

  3. Java学习笔记 - 类方法与代码块的执行顺序

    类的初始化顺序 使用一个简单的父子类例子来做示范,代码执行顺序在代码后有标注. class Parent { public static String p_StaticField = "父类 ...

  4. mysql中not in子查询不能为空

    转载自:https://blog.csdn.net/headingalong/article/details/77744755 错误sql delete from company_info where ...

  5. python-基础-练习和面试题

    给程序传参数 import sys print(sys.argv) 运行结果: 列表推导式 所谓的列表推导式,就是指的轻量级循环创建列表 1. 基本的方式 2. 在循环的过程中使用if 3. 2个fo ...

  6. crm-ssh-列表显示(顾客列表,用户,联系人列表)

    客户列表 1 分析 2 书写步骤 1.封装PageBean 2.书写Action 3.书写Service 4.书写Dao 5.完成strutx以及spring的配置 6.书写前台list.jsp页面 ...

  7. vue开发中控制台报错问题

    1.sockjs.js?9be2:1606 GET http://localhost:8566/sockjs-node/info?t=1569478261510 net::ERR_CONNECTION ...

  8. workbench使用

    1.你是指默认的mysql目录下data里面的'mysql'这个schema没有在workbench里面看到吧?点击菜单-Edit->Preferences里面的SQL Editor,然后把&q ...

  9. TZ_01MyBatis_log4j.propertiies

    # Set root category priority to INFO and its only appender to CONSOLE. #log4j.rootCategory=INFO, CON ...

  10. .net4.6版本前设置window子窗口位置主窗口闪烁

    在安装了.net4.6的版本是不会出现该问题的,但是在4.6以下的版本会出现,当设置之窗体的left和top属性时,会让主窗体闪烁一下. 之前是在load事件下写的: child_window.loa ...