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