Black Horse day11 C3P0 database connection pool

Source: Internet
Author: User

C3P0 Database Connection pool: We will use this database connection pool after development, very convenient. Only one configuration file is required, c3p0 default is to find the C3p0-config.xml file in the class bytecode file.

Steps to use:

1. Import the appropriate jar package to


2. Then use in class

Case 1:

Package Com.itheima.c3p0;import Java.sql.connection;import Java.sql.preparedstatement;import java.sql.ResultSet; Import Java.sql.sqlexception;import Com.mchange.v2.c3p0.combopooleddatasource;public class C3P0Demo1 {public static void Main (string[] args) {Connection con = null; PreparedStatement PS = null; ResultSet rs = null;try{//mode 1:combopooleddatasource datasource=new combopooleddatasource (); Datasource.setdriverclass ("Com.mysql.jdbc.Driver");d atasource.setjdbcurl ("Jdbc:mysql://localhost:3306/day11"); Datasource.setuser ("root");d Atasource.setpassword ("169500"); Con=datasource.getconnection ();p s = Con.preparestatement ("SELECT * from Account"), rs = Ps.executequery (); while (Rs.next ()) {String name = rs.getstring ("Name "); SYSTEM.OUT.PRINTLN (name);}} catch (Exception e) {e.printstacktrace (); throw new RuntimeException ();} finally {if (rs! = null) {try {rs.close ();} catch (SQLException e) {E.printstacktrace ();} finally {rs = null;}} if (PS! = null) {try {ps.close ();} catch (SQLException e) {E.prinTstacktrace ();} finally {PS = null;}} if (con! = null) {try {con.close ()} catch (SQLException e) {e.printstacktrace ();} finally {con = null;}}}}
Mode two: In the SRC directory to establish a configuration file named C3p0-config.xml file, using the C3P0 database connection pool class will automatically find in the class bytecode file this configuration file:

The configuration is 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:// localhost:3306/day11</property>    <property name= "user" >root</property>    <property Name= "Password" >169500</property>  </default-config></c3p0-config>
To use in a class:

Package Com.itheima.c3p0;import Java.sql.connection;import Java.sql.preparedstatement;import java.sql.ResultSet; Import Java.sql.sqlexception;import Com.mchange.v2.c3p0.combopooleddatasource;public class C3P0Demo2 {public static void Main (string[] args) {Connection con = null; PreparedStatement PS = null; ResultSet rs = null;try{//mode 1:combopooleddatasource datasource=new combopooleddatasource ();//the configuration file is searched by default con= Datasource.getconnection ();p s = con.preparestatement ("SELECT * from Account"), rs = Ps.executequery (); while (Rs.next ()) {String name = rs.getstring ("name"); SYSTEM.OUT.PRINTLN (name);}} catch (Exception e) {e.printstacktrace (); throw new RuntimeException ();} finally {if (rs! = null) {try {rs.close ();} catch (SQLException e) {E.printstacktrace ();} finally {rs = null;}} if (PS! = null) {try {ps.close ()} catch (SQLException e) {e.printstacktrace ();} finally {PS = null;}} if (con! = null) {try {con.close ()} catch (SQLException e) {e.printstacktrace ();} finally {con = null;}}}}
Operation Result:






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Black Horse day11 C3P0 database connection pool

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.