Spring之c3p0连接池xml配置和使用举例
1、导入jar包
c3p0-0.9.5.2.jar
mchange-commons-java-0.2.11.jar
2、源码:
beans.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:contexnt="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
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!--配置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://10.15.1.200:3306/gxrdb"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean> <!--创建service对象,注入dao对象-->
<bean id="userSerivce" class="helloworld.c3p0.UserSerivce">
<property name="userDao" ref="userDao"></property>
</bean> <!--创建DAO对象,注入JdbcTemplate对象-->
<bean id="userDao" class="helloworld.c3p0.UserDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <!--创建JdbcTemplate对象,注入连接池dataSource-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> </beans>
UserSerivce.java
package helloworld.c3p0;
public class UserSerivce {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
userDao.add();
}
}
UserDao.java
package helloworld.c3p0;
import org.springframework.jdbc.core.JdbcTemplate;
public class UserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
// 实现添加操作
public void add(){
// 调用jdbcTemplate对象中的方法实现操作
String sql = "insert into user value(?,?,?)";
// 表结构:id(int、自增),name(varchar 100),age(int 10)
int rows = jdbcTemplate.update(sql, null, "Tom3", 35);
System.out.println("插入行数:" + rows);
}
}
TestC3p0.java
package helloworld.c3p0; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestC3p0 {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans_c3p0.xml");
UserSerivce userSerivce = (UserSerivce) context.getBean("userSerivce");
userSerivce.add();
}
}
Spring之c3p0连接池xml配置和使用举例的更多相关文章
- Spring中c3p0连接池的配置 及JdbcTemplate的使用 通过XML配置文件注入各种需要对象的操作 来完成数据库添加Add()方法
通过配置文件XML方法的配置 可以使用非常简练的Service类 UserService类代码如下: package com.swift; public class UserService { pri ...
- C3P0连接池详细配置
C3P0连接池详细配置 转自http://msq.javaeye.com/blog/60387 <c3p0-config> <default-config> <!--当连 ...
- Spring之c3p0连接池配置和使用
1.导入包:c3p0和mchange包 2.代码实现方式: package helloworld.pools; import com.mchange.v2.c3p0.ComboPooledDataSo ...
- Java框架spring 学习笔记(十六):c3p0连接池的配置以及dao使用jdbcTemplate
连接池可以大大提高数据库的性能和连接速度,将那些已连接的数据库连接存放在一个连接池里,以后别人要连接数据库的时候,将不会重新建立数据库连接,直接从连接池中取出可用的连接,用户使用完毕后,会释放连接重新 ...
- Hibernate的配置中,c3p0连接池相关配置
一.配置c3p0 1.导入 hibernate-c3po连接池包,Maven地址是:http://mvnrepository.com/artifact/org.hibernate/hibernate- ...
- spring 5.x 系列第5篇 —— 整合 mybatis + druid 连接池 (xml配置方式)
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 项目目录结构 1.创建maven工程,除了Spring基本依赖外,还需要导 ...
- spring hibernate4 c3p0连接池配置
c3p0-0.9.1.2.jar,c3p0-oracle-thin-extras-0.9.1.2.jar,点此下载 <bean id="dataSource" class=& ...
- C3P0连接池参数配置
<!--acquireIncrement:链接用完了自动增量3个. --> <property name="acquireIncrement">3</ ...
- C3P0连接池的配置与使用
1.下载c3p0-0.9.1.2.jar 下载地址:http://download.csdn.net/detail/chunxiaqiudong5/9661922 2.添加配置文件c3p0-confi ...
随机推荐
- JS实现 类似图片3D效果
其实这样的效果 目前很多网站上都有 其实以前也写过一个 只是当时代码只是为了实现而已,代码很乱,所以现在有业余时间研究了下,其实也并没有什么特殊地方 很类似于左右控制按钮切换图片的思路.效果如下: 可 ...
- Python2.7-canlendar
calendar模块的主要功能是针对万年历.星期几的,此外模块内还有方便的判断闰年.获取月份名.星期名的方法 1.模块的类 1.1.calendar.Calendar([firstweekday]) ...
- JAVA框架 Spring JDBC模板
一:引入jar包: 1.数据驱动jar包: dbcp依赖的包: spring的事务包和数据库包: IOC包AOP包.log4j的包: 所有的jar包: 编写测试类: 注入JdbcTemplate类,该 ...
- 常用的php数组函数
以下是自己比较常用的数组函数 数组元素增加减少array_pusharray_poparray_shiftarray_unshift array_splice (对数组的增删改) array_sli ...
- java 读取excel内容转为JSONArray
需要引入的JAR <!--*.xls--> <dependency> <groupId>net.sourceforge.jexcelapi</grou ...
- 20155229《网络对抗技术》Exp6:信息收集与漏洞扫描
实验内容 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具体服务的查点 (4)漏洞扫描:会扫,会看报告,会查漏洞说明,会修补 ...
- sinopia 搭建记录
最近公司有个问题,一些公共部分每次都要手动发送,放到 git 上涉及到父子 git 问题,现在就想在内部搭建一个 npm,涉及到公共模块了就直接 npm update 更新一下.找到了 sinopia ...
- debian系统下改语言设置
debian系统下改语言设置 安装debian 的时候选择了中文zh_CN_UTF-8,然后进系统后想换成en_US_UTF-8 可以使用一下命令选择:找到需要的语言 确定即可 dpkg-reconf ...
- stm32f051 DMA需要注意的一点
STM32f051的DMA注意事项 问题说明:在使用ADC的DMA通道时,遇到了序列转换的乱序问题,我使用的是DMA循环模式,但是采集的数据却总是错的:第二个内存地址存放的是ADC序列转换中的第一个通 ...
- Eclipse编辑器设置
1. 自己不太喜欢Eclipse按回车后自动插入参数的默认选项. 可以在Window-Preferences-Java-Editor-Content Assist选项里,将Fill method ar ...