1. Import Package: C3P0 and Mchange Package
2, the Code implementation method:
1 PackageHelloworld.pools;2 3 ImportCom.mchange.v2.c3p0.ComboPooledDataSource;4 Importorg.springframework.jdbc.core.JdbcTemplate;5 Importjava.beans.PropertyVetoException;6 7 /**8 * C3P0 Connection Pool Usage Method-code9 * Import Package: C3P0 and Mchange packageTen */ One Public classC3p0codeimpl { A Public Static voidMain (string[] args) { -Combopooleddatasource DataSource =NewCombopooleddatasource (); - Try { theDatasource.setdriverclass ("Com.mysql.jdbc.Driver"); -Datasource.setjdbcurl ("Jdbc:mysql://10.15.1.200:3306/gxrdb"); -Datasource.setuser ("root"); -Datasource.setpassword ("root"); +}Catch(propertyvetoexception e) { - e.printstacktrace (); + } A at //set up a data source -JdbcTemplate JdbcTemplate =NewJdbcTemplate (dataSource); - - //invoke a method in the JdbcTemplate object to implement the Operation -String sql = "INSERT into user value (?,?,?)"; - //table structure: ID (int, self-increment), name (varchar), age (int) in introws = jdbctemplate.update (sql,NULL, "Tom2", 25); -System.out.println ("Insert line number:" +rows); to } +}
3. How to implement spring configuration
Beans.xml
1 <Beansxmlns= "Http://www.springframework.org/schema/beans"2 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"3 xmlns:contexnt= "Http://www.springframework.org/schema/context"4 xsi:schemalocation= "Http://www.springframework.org/schema/beans5 http://www.springframework.org/schema/beans/spring-beans.xsd6 Http://www.springframework.org/schema/context7 http://www.springframework.org/schema/context/spring-context-2.5.xsd ">8 9 <!--Configuring the C3P0 connection pool -Ten <BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource"> One <!--Injection Properties - A < Propertyname= "Driverclass"value= "Com.mysql.jdbc.Driver"></ Property> - < Propertyname= "Jdbcurl"value= "Jdbc:mysql://10.15.1.200:3306/gxrdb"></ Property> - < Propertyname= "User"value= "root"></ Property> the < Propertyname= "Password"value= "root"></ Property> - </Bean> - - + </Beans>
Spring's C3P0 Connection pool configuration and usage