标签:bsp rgs comm code lib proxy int inf mysql
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); // com.mchange.v2.c3p0.impl.NewProxyConnection@41fecb8b } }
配置文件:
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); // com.mysql.jdbc.JDBC4Connection@79d8407f } }
配置文件:
注意:
c3p0需要导入jar包:
c3p0-x.x.x.x.jar
mchange-commons-java-x.x.xx.jar
druid需要导入jar包:
druid-x.x.x.jar
使用数据库连接池前一定别忘了导入数据库驱动包。
标签:bsp rgs comm code lib proxy int inf mysql
原文地址:https://www.cnblogs.com/roadlandscape/p/12192346.html