Spring DBCP连接池例子
来源:互联网 发布:淘宝商城运营主管招聘 编辑:程序博客网 时间:2024/04/26 13:41
1、引入以下jar包:commons-dbcp.jar、commons-pool.jar、mysql-connector-java-5.1.11-bin.jar、commons-logging.jar、spring.jar
2、相关java文件:
package vo;
public class UserVo {
private int id;
private String username;
private int age;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package dao;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import vo.UserVo;
public class UserDaoImpl {
private DataSource dataSource;
private Connection con;
private Statement st;
public void createUser(UserVo user){
try {
con=dataSource.getConnection();
st=con.createStatement();
String sql="insert into tbuser(username,age,address) value('"+user.getUsername()+"',"+user.getAge()+",'"+user.getAddress()+"')";
int m=st.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
st.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public Connection getCon() {
return con;
}
public void setCon(Connection con) {
this.con = con;
}
public Statement getSt() {
return st;
}
public void setSt(Statement st) {
this.st = st;
}
}
spring核心配置文件*.xm:
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="userDaoImpl" class="dao.UserDaoImpl">
<property name="dataSource" ref="DataSource"/>
</bean>
</beans>
测试类:
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dao.UserDaoImpl;
import vo.UserVo;
public class Testdbcp {
@Test
public void testcreate(){
UserVo vo=new UserVo();
vo.setUsername("孔子");
vo.setAge(111002392);
vo.setAddress("蜀国");
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
UserDaoImpl dao=(UserDaoImpl)context.getBean("userDaoImpl");
dao.createUser(vo);
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------
二、链接池其他常用参数说明:
maxActive="80" 最大活动连接
initialSize="10" 初始化连接
maxIdle="60" 最大空闲连接
minIdle="10" 最小空闲连接
maxWait="3000" 从池中取连接的最大等待时间,单位ms.
validationQuery = "SELECT 1" 验证使用的SQL语句
testWhileIdle = "true" 指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除.
testOnBorrow = "false" 借出连接时不要测试,否则很影响性能
timeBetweenEvictionRunsMillis = "30000" 每30秒运行一次空闲连接回收器
minEvictableIdleTimeMillis = "1800000" 池中的连接空闲30分钟后被回收
numTestsPerEvictionRun="3" 在每次空闲连接回收器线程(如果有)运行时检查的连接数量
removeAbandoned="true" 连接泄漏回收参数,当可用连接数少于3个时才执行
removeAbandonedTimeout="180" 连接泄漏回收参数,180秒,泄露的连接可以被删除的超时值
3、还可以用propertise文件配置这种叫PropertyPlaceholderConfigurer方式
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:com/foo/jdbc.properties</value> </property></bean><bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/></bean>
propertise文件:
jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306:testjdbc.username=rootjdbc.password=root
- Spring DBCP连接池例子
- Spring DBCP 连接池
- spring配置dbcp连接池
- Spring 连接池之 DBCP
- spring dbcp 连接池配置
- Spring 连接池之 DBCP
- spring配置dbcp连接池
- spring dbcp连接池配置
- spring配置dbcp连接池
- spring配置dbcp连接池
- 数据库--数据库链接池--DBCP--Spring 连接池之 DBCP
- spring 配置dbcp连接池不能释放,
- Spring之DBCP连接池注入实例
- spring集成mybatis,dbcp连接池,配置文件
- Spring整合Hibernate 使用dbcp连接池
- Spring DBCP 连接Sqlserver
- dbcp开源连接池的使用例子
- spring运用DBCP连接数据源
- View 5部署之三:vCenter Server的预留与限制
- C语言的数学函数
- 数值类型转换规则以及进制转换
- View 5部署之四:vCenter插件简化存储管理
- maven 不能设置为web3.0人解决方法
- Spring DBCP连接池例子
- Web上的服务分类方法-Leonard Richardson
- View 5部署之五:域控制器、用户组及DHCP
- 一针见血:error C2146: 语法错误 : 缺少“;”
- View 5部署之六:Connection Server安装与配置
- View 5部署之七:View Composer安装
- 表的创建修改及增删改查-DML操作
- View 5部署之八:准备桌面模板
- MAC下cocos2d-x的andorid 环境配置