ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ package cn.itcast.datasource.c3p0; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com */ import com.mchange.v2.c3p0.ComboPooledDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; /** * c3p0的演示 */ public class C3P0Demo1 { public static void main(String[] args) throws SQLException { //1.创建数据库连接池对象 DataSource ds = new ComboPooledDataSource(); //2.获取连接对象 Connection conn = ds.getConnection(); //3.打印 System.out.println(conn); //2.定义sql String sql = "insert into account(name,balance) values('钱六',256.58)"; PreparedStatement pstmt = conn.prepareStatement(sql); int count = pstmt.executeUpdate(); System.out.println(count); } } ~~~ ***** ~~~ package cn.itcast.datasource.c3p0; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com */ import com.mchange.v2.c3p0.ComboPooledDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; /** * c3p0演示 */ public class C3P0Demo2 { public static void main(String[] args) throws SQLException { testNamedConfig(); // //1.获取DataSource 使用默认配置 // DataSource ds = new ComboPooledDataSource(); // //获取连接 // for (int i = 1; i <=11 ; i++) { // Connection conn = ds.getConnection(); // System.out.println(i+":"+conn); // if(i==5){ // conn.close();//归还连接到连接池中 // } // } } public static void testNamedConfig() throws SQLException { //1.获取DataSource 使用默认配置 DataSource ds = new ComboPooledDataSource("otherc3p0"); //获取连接 for (int i = 1; i <=11 ; i++) { Connection conn = ds.getConnection(); System.out.println(i+":"+conn); if(i==5){ conn.close();//归还连接到连接池中 } } } } ~~~