Java使用C3P0資料庫連接池的方法

來源:互聯網
上載者:User

標籤:friend   config   max   value   user   使用   開啟   efault   檔案   

因不想繼續在PHP花更多的時間,所以入手JAVA,從零學至此利用C3P0實現JDBC串連池,提高資料庫處理的能力,加快效能。一般情況下,操作資料庫的過程都是串連資料庫->執行操作->釋放資源。這樣的操作在一般情況下是沒什麼問題的,但是如果出現頻繁的操作就會造成一些效能上的問題,因為頻繁開啟關閉串連是個耗時的操作,所以就Java目前用得最多的是C3P0實現JDBC串連池,由於剛入手Java希望各位小夥伴海涵。

使用C3P0先得引包:c3p0-0.9.1.2.jar和mysql-connector-java-5.1.39-bin.jar

編寫C3P0設定檔c3p0-config.xml

<?xml version="1.0" encoding="UTF-8" ?><c3p0-config>    <default-config>        <property name="driverClass">com.mysql.jdbc.Driver</property>        <property name="jdbcUrl">jdbc:mysql://localhost:3306/test20180109</property>        <property name="user">root</property>        <property name="password">root</property>        <property name="initialPoolSize">5</property>        <property name="maxPoolSize">20</property>    </default-config>    <named-config name="lfriend">        <property name="driverClass">com.mysql.jdbc.Driver</property>        <property name="jdbcUrl">jdbc:mysql:///test20180109</property>        <property name="user">root</property>        <property name="password">root</property>    </named-config></c3p0-config>

編寫c3p0工具類

package me.lfriend.jdbc.utils;import com.mchange.v2.c3p0.ComboPooledDataSource;import javax.sql.DataSource;import java.sql.Connection;import java.sql.SQLException;public class C3P0Utils {    private static ComboPooledDataSource dataSource=new ComboPooledDataSource(‘lfriend‘);    public static DataSource getDataSource(){        return dataSource;    }    public static Connection getConnection(){        try {            return dataSource.getConnection();        } catch (SQLException e) {            throw new RuntimeException(e);        }    }}

編寫測試類別TestC3P0.java

package me.lfriend.jdbc.test;import com.mchange.v2.c3p0.ComboPooledDataSource;import me.lfriend.jdbc.utils.C3P0Utils;import me.lfriend.jdbc.utils.JDBCUtils_V3;import org.junit.Test;import java.sql.Connection;import java.sql.PreparedStatement;public class TestC3P0 {    @Test    public void testAddUser(){        Connection conn=null;        PreparedStatement pstmp=null;        //1.建立自訂串連池對象        ComboPooledDataSource dataSource=new ComboPooledDataSource();//載入預設配置        //ComboPooledDataSource dataSource=new ComboPooledDataSource("lfriend");//載入有名稱的配置        try {            //2.從池子中擷取串連            conn=dataSource.getConnection();            //3.編寫sql            String sql="insert into user values(null,?,?)";            //4.建立預先處理對象            pstmp=conn.prepareStatement(sql);            //5.設定參數            pstmp.setString(1,"piu");            pstmp.setString(2,"123456");            //6.執行查詢            int row=pstmp.executeUpdate();            if(row>0){                System.out.println("添加成功!");            }else{                System.out.println("添加失敗!");            }        }catch (Exception e){            throw new RuntimeException(e);        }finally {            JDBCUtils_V3.release(conn,pstmp,null);        }    }    /**     * 工具類使用C3P0     */    @Test    public void testAddUser1(){        Connection conn=null;        PreparedStatement pstmp=null;        try {            conn= C3P0Utils.getConnection();            //3.編寫sql            String sql="insert into user values(null,?,?)";            //4.建立預先處理對象            pstmp=conn.prepareStatement(sql);            //5.設定參數            pstmp.setString(1,"piu");            pstmp.setString(2,"123456");            //6.執行查詢            int row=pstmp.executeUpdate();            if(row>0){                System.out.println("添加成功!");            }else{                System.out.println("添加失敗!");            }        }catch (Exception e){            throw new RuntimeException(e);        }finally {            JDBCUtils_V3.release(conn,pstmp,null);        }    }}

C3P0的基本操作就是這樣,歡迎各位大佬指出問題!

Java使用C3P0資料庫連接池的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.