C3P0连接池的简单使用
一、初始化连接private static ComboPooledDataSource pool = new ComboPooledDataSource("mysql"); //此处填写参数代表使用这个,不填写使用默认
二、设定参数,如果填写了<?xml version="1.0" encoding="UTF-8"?>
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/db2
root
root
3
10
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/db2?useUnicode=TRUE
root
root
3
10
如果没有填写,先去xml去找默认的配置项,如果没有,在代码中查找设置的属性,如果没有则报错
代码中初始化设定参数:pool.setDriverClass("com.mysql.jdbc.Driver");
pool.setJdbcUrl("jdbc:mysql://localhost:3306/db2?useUnicode=TRUE&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=TRUE&useLegacyDatetimeCode=FALSE&serverTimezone=UTC&zeroDateTimeBehavior=convertToNull");
pool.setUser("root");
pool.setPassword("root");
pool.setInitialPoolSize(3); //初始化连接数
pool.setMaxPoolSize(10); //最大连接数
pool.setMaxIdleTime(100); //超时时间
三、得到连接Connection conn1 = pool.getConnection();
四、查看连接数Connection connection = pool.getConnection();
System.out.print("当前连接数:");
System.out.println(pool.getNumIdleConnections());