1. Importing the JAR Package
C3p0-0.9.1.jar
mchange-commons-java-0.2.3.4(Note: The jar package is a C3P0 database connection pool of auxiliary packages, no this package system will be reported when the boot ClassNotFoundException, This is the c3p0-0.9.2 version after the separation of the package, 0.9.1 when a package will be done)
2. Get the connection
C3P0 is implemented in three ways.
1. Write your own code to implement the data source
For example: Configure a property file under the Classpath, Config.properties, with the following:
Driverclass=xxx
Jdbcurl=xxx
User=xxx
Password=xxx
...
The code then implements the
Properties Props = new properties (); InputStream in = Thread.class.getResourceAsStream ("config.properties");p rops.load (in); In.close (); Combopooleddatasource CPDs = new Combopooleddatasource () Cpds.setdriverclass (Props.getproperty ("DriverClass")); Cpds.setjdbcurl (Props.getproperty ("Jdbcurl")) Cpds.setuser (Props.getproperty ("user")); Cpds.setpassword ( Props.getproperty ("password"));
2. Configure the default familiar files
A c3p0.properties file is provided under the Classpath (cannot be renamed)
The configuration is as follows:
C3p0.driverclass=com.mysql.jdbc.driver
C3p0.jdbcurl=jdbc:mysql://localhost/paper_tag
C3p0.user=root
C3p0.password=admin
3. Provide a c3p0-config.xml file under the path
<c3p0-config> <default-config> <property name= "user" >root</property> <property name= " Password ">java</property> <property name=" Driverclass ">com.mysql.jdbc.Driver</property> < Property Name= "Jdbcurl" >jdbc:mysql://localhost:3306/jdbc</property> <property name= "Initialpoolsize" >10</property> <property name= "MaxIdleTime" >30</property> <property name= "Maxpoolsize" > 100</property> <property name= "minpoolsize" >10</property> </default-config> < Named-config name= "MySource" > <property name= "user" >root</property> <property name= "Password" > java</property> <property name= "Driverclass" >com.mysql.jdbc.Driver</property> <property name= "Jdbcurl" >jdbc:mysql://localhost:3306/jdbc</property> <property name= "Initialpoolsize" >10</ property> <property name= "MaxIdleTime" >30</property> <property name= "maxpoolsize" >100</property> <property name= "Minpoolsize" >10</ Property>
</named-config></c3p0-config>
Here I am using the simplest of the second way.
Package C3p0;import Java.sql.connection;import Java.sql.sqlexception;import Com.mchange.v2.c3p0.combopooleddatasource;public class C3p0util {private static Combopooleddatasource ds = new Combopooleddatasource ();p ublic static Connection getconn () {try {return ds.getconnection ();} catch (SQLException e) { throw new RuntimeException (e);}}
For this configuration, if there is a c3p0.properties configuration file in the Classpath, the code does not need to set the connection information, direct new Combopooleddatasource (), he will automatically read the configuration file configuration. Of course, you can also use the C3p0-config.xml file to configure the connection information, using XML as configuration information, Combopooldatasource can also accept a string parameter, the name of this parameter is configured in the C3p0-config.xml file, which means that we can be in the X ML files can have multiple database source connection information, such as can be mysql,oracle.
C3p0 of the JDBC Connection pool