Need to create a c3p0-config.xml configuration file
* C3P0 configuration file
* 1. Configuration file name: C3p0-config.xml
* 2. The location of the configuration file must be under the Classpath
Copy
Modify a file
Capitalize the first letter to lowercase
Configuration file Setup Complete
Remember to import the database connection jar package
C3P0 jar Package
Code
1 Packagetest03;2 3 Importjava.beans.PropertyVetoException;4 Importjava.sql.Connection;5 ImportJava.sql.ResultSet;6 Importjava.sql.SQLException;7 8 ImportCom.mchange.v2.c3p0.ComboPooledDataSource;9 Ten /** One * C3P0 Connection data source A * @authorStar - * - */ the Public classC3p0test { - Public Static voidMain (string[] args)throwsException { - //test1 (); - test2 (); + } - + Private Static voidTest1 ()throwsSQLException, propertyvetoexception { A //TODO auto-generated Method Stub at //Create a connection pool -Combopooleddatasource pool =NewCombopooleddatasource (); - - //four parameters of the set connection - // -Pool.setdriverclass ("Com.mysql.jdbc.Driver"); inPool.setjdbcurl ("Jdbc:mysql:///day01"); -Pool.setuser ("root"); toPool.setpassword ("root"); + // - //Get Connections theConnection conn =pool.getconnection (); *String sql = "SELECT * from Stu"; $ResultSet rs =conn.createstatement (). executeQuery (SQL);Panax Notoginseng while(Rs.next ()) { -System.out.println (Rs.getint (1) + "" +rs.getstring (2)); the } + rs.close (); A conn.close (); the } + //using configuration Files - /* $ * C3P0 configuration file $ * 1. Configuration file name: C3p0-config.xml - * 2. The location of the configuration file must be under the Classpath - */ the Private Static voidTest2 ()throwsException { - //TODO auto-generated Method StubWuyi //c3p0 Creating a connection pool object theCombopooleddatasource pool =NewCombopooleddatasource (); - //Get Connections WuConnection conn =pool.getconnection (); -String sql = "SELECT * from Stu"; AboutResultSet rs =conn.createstatement (). executeQuery (SQL); $ while(Rs.next ()) { -System.out.println (Rs.getint (1) + "" +rs.getstring (2)); - } - rs.close (); A conn.close (); + the } - $}
c3p0 Connection Pool
1 <C3p0-config>2 <Default-config>3 < Propertyname= "Driverclass">Com.mysql.jdbc.Driver</ Property>4 < Propertyname= "Jdbcurl">Jdbc:mysql:///day01</ Property>5 < Propertyname= "User">Root</ Property>6 < Propertyname= "Password">Root</ Property>7 8 9 </Default-config>Ten </C3p0-config>
C3p0-config.xml
C3p0 Creating a Connection pool object
Combopooleddatasource pool = new Combopooleddatasource ();
C3p0 the writing of the JDBC Connection pool XML configuration file