Java methods for using C3P0 database connection pooling

Source: Internet
Author: User
Tags connection pooling

Because I do not want to continue to spend more time in PHP, so start Java, from zero learn to use C3P0 to implement JDBC connection pool, improve database processing ability, speed up performance. In general, the process of manipulating a database is to release resources------execute operations, connect to a database. This kind of operation in general is not a problem, but if the frequent operation will cause some performance problems, because the frequent open close connection is a time-consuming operation, so the Java currently used most is C3P0 implementation JDBC Connection pool, because just start Java hope that the small partner Haihan.

Using C3P0 Primer: C3p0-0.9.1.2.jar and Mysql-connector-java-5.1.39-bin.jar

Write the c3p0 configuration file 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>

Writing the C3P0 tool class

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);        }    }}

Writing test class 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. Create a custom Connection pool object Combopooleddatasource datasource=new combopooleddatasource ();//load Default configuration//combopooleddatasource Datasource=new Combopooleddatasource ("Lfriend");//load the configuration with the name try {//2. Get the connection from the pool CONN=DATASOURC            E.getconnection ();            3. Write SQL String sql= "insert into user values (null,?,?)";            4. Create a Preprocessing object pstmp=conn.preparestatement (SQL);            5. Set the parameter pstmp.setstring (1, "piu");            Pstmp.setstring (2, "123456");            6. Execute Query int row=pstmp.executeupdate (); if (row>0) {SYSTEM.OUT.PRINTLN ("added successfully!            "); }else{System.out.println ("Add failed!            ");        }}catch (Exception e) {throw new RuntimeException (e);        }finally {jdbcutils_v3.release (conn,pstmp,null);        }}/** * Tool class using C3P0 */@Test public void TestAddUser1 () {Connection conn=null;        PreparedStatement Pstmp=null;            try {conn= c3p0utils.getconnection ();            3. Write SQL String sql= "insert into user values (null,?,?)";            4. Create a Preprocessing object pstmp=conn.preparestatement (SQL);            5. Set the parameter pstmp.setstring (1, "piu");            Pstmp.setstring (2, "123456");            6. Execute Query int row=pstmp.executeupdate (); if (row>0) {System.out.println ("added successfully!            "); }else{System.out.println ("Add failed!            ");        }}catch (Exception e) {throw new RuntimeException (e); }finally {Jdbcutils_v3.release (Conn,pstmp,null); }    }}

C3P0 Basic operation is like this, welcome to the big guy point out the problem!

Java methods for using C3P0 database connection pooling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.