Spring DBCP连接池例子

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.Driver
jdbc.url=jdbc:mysql://localhost:3306:test
jdbc.username=root
jdbc.password=root



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值