Spring和C3P0數據連接池使用

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
#訪問數據庫Url
db_jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
db_user=wqy
db_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!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章