JDBC Connection pool (data source)

Source: Internet
Author: User
Tags connection pooling

Custom connection pooling: The original connected Close method is transformed into a data source using the decorative design mode: http://www.cnblogs.com/tongxuping/p/6832518.html;

Open Source database connection pool:

C3P0 Connection Pool:

Basic steps:

1. Import the development package
2. Create a database connection pool
Combopooleddatasource CPDs = new Combopooleddatasource ();
3. Configure Basic connection Information
A) Set the parameters by the Setters method
b) Configure basic connection information through the C3p0-config.xml file
c) Configure basic connection information through the C3p0.properties file

Example code:

1  PackagePool;2 3 4 Importjava.sql.Connection;5 Importjava.sql.PreparedStatement;6 7 Importjdbcutils.jdbcutils;8 9 ImportCom.mchange.v2.c3p0.ComboPooledDataSource;Ten  One /* A * C3P0 is an open source is a data source - * C3P0 is called the database link pool, which is used to manage the acquisition and storage of database links.  - * There are two ways to do this: the * Method One: Set the basic information of JDBC directly after creating the object, such as: Database driver, database URL, login database user name and password, and then manipulate the database. - * Mode two: Build an XML or properties configuration file, there is no need to set the basic information of the database when the configuration file exists. Direct access to database connections, corresponding operations to the database -  */ -  Public classC3P0 { +      Public Static voidMain (string[] args) { -          +Connection conn =NULL; APreparedStatement ps=NULL; at         //get the Datesource that is the data source through Combopooleddatasource, from which you can get a link to the database.  -Combopooleddatasource pool =NewCombopooleddatasource (); -         //Combopooleddatasource pool = new Combopooleddatasource ("Config1"); -         /* - * The parameter indicates that you need to use CONFIG1 configuration information for database operation information -          */ in         Try { -             /*Pool.setdriverclass ("Com.mysql.jdbc.Driver"); to Pool.setjdbcurl ("JDBC:MYSQL:///MYDB5"); + pool.setuser ("root"); - Pool.setpassword ("admin");*/ the              *conn =pool.getconnection (); $String sql= "Delete from Tb_batch where id>?";Panax NotoginsengPS =conn.preparestatement (SQL); -Ps.setint (1, 20); the             introws =ps.executeupdate (); + System.out.println (rows); A              the              +              -}Catch(Exception e) { $ e.printstacktrace (); $}finally{ -JDBCUTILS.CLOSERESOU (Conn, PS,NULL); -         } the     } -}

XML configuration: The XML file name must be: C3p0-config.xml, otherwise it is not the configuration information; name cannot be written at random.

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <C3p0-config>3     <Default-config>4         <!--setting -5         <!--C3p0 reads this block by default -6         < Propertyname= "Driverclass">Com.mysql.jdbc.Driver</ Property>7         < Propertyname= "Jdbcurl">Jdbc:mysql:///mydb5</ Property>8         < Propertyname= "User">Root</ Property>9         < Propertyname= "Password">Admin</ Property>Ten     </Default-config> One  A  -     <Named-configname= "CONFIG1">  -         <!--   - the         < Propertyname= "Driverclass">Com.mysql.jdbc.Driver</ Property> -         < Propertyname= "Jdbcurl">Jdbc:mysql:///mydb1</ Property> -         < Propertyname= "User">Admin</ Property> -         < Propertyname= "Password">123</ Property> +     </Named-config> - </C3p0-config>

Properties Configuration:

1 c3p0.driverclass=com.mysql.jdbc.Driver2 c3p0.jdbcurl=jdbc:mysql:/// MYDB13 c3p0.user=root4 c3p0.password=root

JDBC Connection pool (data source)

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.