Spring和C3P0数据连接池使用

来源:互联网 发布:易语言会员系统源码 编辑:程序博客网 时间:2024/06/05 02:29

Spring和C3P0数据连接池使用

书山有路勤为径

工具:
- MyEclipse
- JDK1.7
- Oracle数据库


步骤

1.在MyEclipse中新建Web项目

2.添加jar包到WebRoot\WEB-INF\lib目录下

-Spring-3.2.0 jar包

-c3p0.jar包

-数据库驱动包

-其它依赖jar包

spring3.2.0+c3p0等包获取地址

3.spring配置文件(application.xml)

<!-- 文件名:application.xml --><?xml version="1.0" encoding="UTF-8"?><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-3.0.xsd">    <!--读取其它配置文件,这里读取db.properties文件 -->    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="locations" value="classpath:db.properties"/>    </bean>    <!-- 配置数据源 -->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="driverClass" value="${db_driverClass}"/>        <property name="jdbcUrl" value="${db_jdbcUrl}"/>        <property name="user" value="${db_user}"/>        <property name="password" value="${db_password}"/>        <!-- 当资源耗尽时,一次性获取的连接数 -->        <property name="acquireIncrement" value="${db_acquireIncrement}"/>        <!-- 初始连接数 -->        <property name="initialPoolSize" value="${db_initialPoolSize}"/>        <!-- 连接池中,创建最少连接数 -->        <property name="minPoolSize" value="${db_minPoolSize}"/>        <!-- 连接池中,创建的最多连接数 -->        <property name="maxPoolSize" value="${db_maxPoolSize}"/>    </bean>    <!-- JDBC模板 -->    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource" ref="dataSource" />    </bean>    <!-- 注入Bean -->    <bean id="spring_c3p0_Demo" class="com.wqy.dao.Spring_c3p0_Demo">        <property name="jdbcTemplate" ref="jdbcTemplate"/>    </bean></beans>

4.连接数据库配置文件(db.properties)

#数据库db_driverClass=oracle.jdbc.driver.OracleDriver#访问数据库Urldb_jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcldb_user=wqydb_password=wqy#初始化时,数据池中的连接数db_initialPoolSize=10#数据池中最小连接数db_minPoolSize=2#数据池中最多连接数db_maxPoolSize=10#当数据池中连接耗尽时,c3p0一次性获取的连接数db_acquireIncrement=3

4.在WebRoot\WEB-INF\web.xml中配置spring的配置文件

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"     xmlns="http://java.sun.com/xml/ns/javaee"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <!-- 声明spring的配置文件 -->  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:application.xml</param-value>  </context-param></web-app>

5.编写测试程序

目录结构:

这个目录结构

5.1Spring_c3p0_Demo.java

package com.wqy.dao;import java.util.UUID;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.jdbc.core.JdbcTemplate;import com.wqy.entity.User;/** * <p>此类测试c3p0连接池和spring框架</p> * <p>使用Spring中的JdbcTemplate进行对数据库的插入操作</p> *  * @author wqy * */public class Spring_c3p0_Demo {    private JdbcTemplate jdbcTemplate;    //Spring setter注入,创建setter方法    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {        this.jdbcTemplate = jdbcTemplate;    }    public static void main(String[] args) {        /*         * 获得Spring中定义的Bean实例(对象)         */        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");        Spring_c3p0_Demo scd = (Spring_c3p0_Demo) ctx.getBean("spring_c3p0_Demo");        /*         * 创建一个用户         */        User user = new User();        user.setUserid(UUID.randomUUID().toString().replace("-", "").toUpperCase());//使用UUID生成全局唯一的字符串,并转换成大写        user.setUname("wqy");        user.setUpass("123");        scd.insert(user);    }    public void insert(User user){        String sql = "insert into t_user values(?,?,?)";        jdbcTemplate.update(sql,user.getUserid(),user.getUname(),user.getUpass());    }}

5.2 User.java

package com.wqy.entity;public class User {    private String userid;    private String uname;    private String upass;    public String getUserid() {        return userid;    }    public void setUserid(String userid) {        this.userid = userid;    }    public String getUname() {        return uname;    }    public void setUname(String uname) {        this.uname = uname;    }    public String getUpass() {        return upass;    }    public void setUpass(String upass) {        this.upass = upass;    }}

6.运行测试类,得到结果

运行结果(数据库)

ps:这是我的第一篇博客哦!哈哈!Fighting!

1 0
原创粉丝点击