*数据库连接池的基本思想:为数据库建立一个缓冲池,预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需要从缓冲池中取出一个,使用完毕后再放回
*数据库连接池负责分配、管理和释放数据库连接,它语序应用程序重复使用一个现有的数据库连接,而不是重新建立一个。
*导入jar包

*进入c3p0doc文档,查询配置方法

*在src下创建c3p0-config.xml
- <c3p0-config>
- <named-config name="helloC3P0">
-
- <property name="driverClass">com.mysql.cj.jdbc.Driverproperty>
- <property name="jdbcUrl">jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=falseproperty>
- <property name="user">rootproperty>
- <property name="password">123456property>
-
- <property name="acquireIncrement">5property>
- <property name="initialPoolSize">10property>
- <property name="minPoolSize">10property>
- <property name="maxPoolSize">100property>
- <property name="maxStatements">50property>
- <property name="maxStatementsPerConnection">5property>
-
- named-config>
- c3p0-config>
*使用c3p0数据库连接池获取连接
- public void testGetConnection2() throws SQLException {
- ComboPooledDataSource cpds = new ComboPooledDataSource("helloC3P0");
- Connection conn = cpds.getConnection();
- System.out.println(conn);
- }
*导入jar包

*在src下创建dbcp.properties配置文件
- driverClassNmae=com.mysql.cj.jdbc.Driver
- url=jdbc:mysql://@localhost:3306/test?serverTimezone=UTC&useSSL=false
- username=root
- password=168465
*使用DBCP数据库连接池获取连接
- public void testGetConnection2() throws Exception {
- Properties pros=new Properties();
- InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("dbcp.properties");
- pros.load(is);
- DataSource source = BasicDataSourceFactory.createDataSource(pros);
- Connection conn = source.getConnection();
- System.out.println(conn);
- }
*导入jar包
![]()
*在src下创建druid.properties配置文件
- username=root
- password=321684635
- url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
- driverClassName=com.mysql.cj.jdbc.Driver
*使用Druid数据库连接池获取连接
- public void getConnection() throws Exception {
- Properties pros = new Properties();
- InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
- pros.load(is);
- DataSource source = DruidDataSourceFactory.createDataSource(pros);
- Connection conn = source.getConnection();
- System.out.println(conn);
- }
*使用Druid的JDBCUtiles
- private static DataSource source;
- static {
- try {
- Properties pros=new Properties();
- InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
- pros.load(is);
- source= DruidDataSourceFactory.createDataSource(pros);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static Connection getConnection() throws SQLException {
- return source.getConnection();
- }