First, preface:
Both DBCP and C3PO can be used as data connection pools,
Second, import jar package:
Third, configuration Applicationcontext.xml file
Configure DBCP
<!--Create a data source - <BeanID= "DataSourceId"class= "Org.apache.commons.dbcp.BasicDataSource"> < Propertyname= "Driverclassname"value= "Com.mysql.jdbc.Driver"></ Property> < Propertyname= "url"value= "Jdbc:mysql://localhost:3306/ee19_spring_day02"></ Property> < Propertyname= "username"value= "root"></ Property> < Propertyname= "Password"value= "1234"></ Property> </Bean> <!--to create a template, you need to inject a data source - <BeanID= "Jdbctemplateid"class= "Org.springframework.jdbc.core.JdbcTemplate"> < Propertyname= "DataSource"ref= "DataSourceId"></ Property> </Bean> <!--Configure DAO - <BeanID= "Userdaoid"class= "com.itheima.c_dbcp." Userdao "> < Propertyname= "JdbcTemplate"ref= "Jdbctemplateid"></ Property> </Bean>
Configure C3P0
<!--Create a data source c3p0 - <BeanID= "DataSourceId"class= "Com.mchange.v2.c3p0.ComboPooledDataSource"> < Propertyname= "Driverclass"value= "Com.mysql.jdbc.Driver"></ Property> < Propertyname= "Jdbcurl"value= "Jdbc:mysql://localhost:3306/ee19_spring_day02"></ Property> < Propertyname= "User"value= "root"></ Property> < Propertyname= "Password"value= "1234"></ Property> </Bean>
Using Jdbcdaosupport
Spring configuration file
<!--Configure DAO * DAO to inherit Jdbcdaosupport, then only need to inject the data source, the underlying will automatically create the template - <BeanID= "Userdaoid"class= "Com.itheima.e_jdbcdaosupport." Userdao "> < Propertyname= "DataSource"ref= "DataSourceId"></ Property> </Bean>
DBCP and C3P0 use--not perfect