package cn.it.datasource.c3p0;import com.mchange.v2.c3p0.ComboPooledDataSource;import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;/*** c3p0演示*/
public class Demo1 {public static void main(String[] args) throws SQLException {// 1.创建数据库连接池对象DataSource ds = new ComboPooledDataSource();// 2.获取连接对象Connection connection = ds.getConnection();System.out.println(connection);}
}
<c3p0-config><!-- 使用默认的配置读取连接池对象 --><default-config><!-- 连接参数 --><property name="driverClass">com.mysql.cj.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/blog</property><property name="user">root</property><property name="password">123456</property><!-- 连接池参数 --><property name="initialPoolSize">5</property><property name="maxPoolSize">10</property><property name="checkoutTimeout">3000</property></default-config><named-config name="otherc3p0"> <!-- 连接参数 --><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/day25</property><property name="user">root</property><property name="password">root</property><!-- 连接池参数 --><property name="initialPoolSize">5</property><property name="maxPoolSize">8</property><property name="checkoutTimeout">1000</property></named-config>
</c3p0-config>
package cn.it.datasource.c3p0;import com.mchange.v2.c3p0.ComboPooledDataSource;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class Demo3 {public static void main(String[] args) throws SQLException {// 1.获取DataSourceComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("laravelimg");// 2.获取连接for (int i = 1; i <= 5; i++) {Connection connection = comboPooledDataSource.getConnection();System.out.println(i+":"+connection);String sql = "select * from laravel_fileinfo where id > ? and `aname` = 'zol'";PreparedStatement preparedStatement = connection.prepareStatement(sql);preparedStatement.setInt(1,1);ResultSet resultSet = preparedStatement.executeQuery();while (resultSet.next()){System.out.println(resultSet.getString("url"));}connection.close();}}
}