一、概览
结构图:
jar包的位置:WEB-INF/lib 和 src
配置文件位置:src
二、配置
c3p0 和 DBCP 连接池都有两种方法配置:
- 配置文件配置
c3p0配置文件:c3p0-config.xml
dbcp配置文件:dbcpconfig.properties - 直接在代码中配置
public class C3P0Demo {
public static DataSource getDataSourceWithC3P0(){
ComboPooledDataSource c3p0 = new ComboPooledDataSource();
try {
c3p0.setDriverClass("com.mysql.cj.jdbc.Driver");
} catch (PropertyVetoException e) {
e.printStackTrace();
}
c3p0.setJdbcUrl("jdbc:mysql://localhost:3306/db_stu?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai");
c3p0.setUser("root");
c3p0.setPassword("root");
return c3p0 ;
}
public static DataSource getDataSourceWithC3P0ByXml(){
ComboPooledDataSource c3p0 = new ComboPooledDataSource("yanqun");
return c3p0 ;
}
public static void main(String[] args)throws Exception {
System.out.println(getDataSourceWithC3P0().getConnection());
System.out.println(getDataSourceWithC3P0ByXml().getConnection());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<!-- 如果要研究某个xml中可以设置哪些属性。找相关类的 属性 或者setXxx()-->
<property name="user">root</property>
<property name="password">root</property>
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db_stu?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai</property>
<property name="checkoutTimeout">20000</property>
</default-config>
<named-config name="yanqun">
<property name="user">root</property>
<property name="password">root</property>
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db_stu?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai</property>
<property name="checkoutTimeout">20000</property>
</named-config>
</c3p0-config>
说明:jdbcUrl一栏中的原来的&
改用&
public class DbcpDemo {
public static DataSource getDataSourceWIthDBCP(){
BasicDataSource dbcp = new BasicDataSource();
dbcp.setDriverClassName("com.mysql.cj.jdbc.Driver");
dbcp.setUrl("jdbc:mysql://localhost:3306/db_stu?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai");
dbcp.setUsername("root");
dbcp.setPassword("root");
dbcp.setInitialSize(20);
dbcp.setMaxActive(10);
return dbcp;
}
public static DataSource getDataSourceWIthDBCPByProperties() throws Exception{
DataSource dbcp = null ;
Properties props = new Properties();
InputStream input = new DbcpDemo().getClass().getClassLoader().getResourceAsStream("dbcpconfig.properties");
props.load( input );
dbcp = BasicDataSourceFactory.createDataSource(props ) ;
return dbcp;
}
public static void main(String[] args) throws Exception {
DataSource ds = getDataSourceWIthDBCP();
DataSource ds1 = getDataSourceWIthDBCPByProperties();
System.out.println(ds);
System.out.println(ds1);
}
}
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/db_stu?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username=root
password=root
initialSize=10