C3P0 use of data sources

Source: Internet
Author: User

1.c3p0 use of data sources

    C3P0 is an open source JDBC connection pool that implements the data source and Jndi bindings, and supports the standard extensions of the JDBC3 specification and JDBC2. C3P0 has its own format file, as follows

<?xml version= "1.0" encoding= "UTF-8"?>
<c3p0-config>
<default-config>
<property name= "Driverclass" >com.mysql.jdbc.Driver</property>
<property name= "Jdbcurl" >jdbc:mysql:///day05</property>
<property name= "user" >root</property>
<property name= "password" >root</property>
<property name= "initialpoolsize" >10</property>
<property name= "MaxIdleTime" >30</property>
<property name= "maxpoolsize" >100</property>
<property name= "minpoolsize" >10</property>
</default-config>
</c3p0-config>

    1. Guide Jar Package:
    2. Establishing a C3P0 connection

Package Com.beiwo.day05.util;import Java.sql.connection;import Java.sql.resultset;import java.sql.SQLException; Import Java.sql.statement;import Javax.sql.datasource;import Com.mchange.v2.c3p0.combopooleddatasource;public Class C3p0util {//c3p0 data source private static Combopooleddatasource datasource=new Combopooleddatasource ();p ublic static DataSource Getdatasource () {return DataSource;} public static Connection Getconn () {try {return datasource.getconnection ();} catch (SQLException e) {throw new Runtimeexc Eption (e);}} public static Connection GetConn2 () {Connection conn=null;try {conn=datasource.getconnection ();} catch (Exception e) { E.printstacktrace ();} Return conn;} Release the connection public static void Realease (ResultSet rs,statement stmt,connection conn) {try {if (null!=rs) {Rs.close ()}} catch ( Exception e) {//Todo:handle Exception}try {if (null!=stmt) {stmt.close ();}} catch (Exception e) {//Todo:handle Exception }try {if (null!=conn) {conn.close ();}} catch (Exception e) {//Todo:handle Exception}}}

The use of test c3p0

Package Com.beiwo.day05.test;import Java.sql.connection;import Org.junit.test;import com.beiwo.day05.util.C3P0Util ;p Ublic class Testc3p0 {@Testpublic void testc3p0 () {Connection Connection = C3p0util.getconn (); System.out.println (Connection.getclass (). GetName ()); C3p0util.realease (NULL, NULL, connection);}}

C3P0 use of data sources

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.