package com.fgy.datasource.c3p0; import com.mchange.v2.c3p0.ComboPooledDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; /** * c3p0的使用 */ public class C3P0Demo { public static void main(String[] args) throws SQLException { // 使用默认配置 // DataSource dataSource = new ComboPooledDataSource(); // 使用指定名称配置 DataSource dataSource = new ComboPooledDataSource("otherc3p0"); Connection conn = dataSource.getConnection(); System.out.println(conn); // [email protected] } }
配置文件:
package com.fgy.datasource.druid; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.InputStream; import java.sql.Connection; import java.util.Properties; /** * Druid的使用 */ public class DruidDemo { public static void main(String[] args) throws Exception { Properties properties = new Properties(); InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties"); properties.load(is); DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); Connection conn = dataSource.getConnection(); System.out.println(conn); // [email protected] } }
配置文件:
注意:
c3p0需要导入jar包:
c3p0-x.x.x.x.jar
mchange-commons-java-x.x.xx.jar
druid需要导入jar包:
druid-x.x.x.jar
使用数据库连接池前一定别忘了导入数据库驱动包。
原文地址:https://www.cnblogs.com/roadlandscape/p/12192346.html
时间: 2024-10-19 09:03:18