- 引入依赖
pom.xml 注意导的包 c3p0 有两个包(都要有)
<dependencies>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
01.第一种方式:(不要配置文件)
public static void main(String[] args) throws SQLException, PropertyVetoException {
ComboPooledDataSource sc=new ComboPooledDataSource();
sc.setDriverClass("com.mysql.jdbc.Driver");
sc.setJdbcUrl("jdbc:mysql://localhost:3306/test");
sc.setUser("root");
sc.setPassword("root");
sc.setInitialPoolSize(10);
Connection conn=sc.getConnection();
System.out.println(conn);
}
02.第二种方式(通过配置文件)
01.配置文件名 c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<named-config name="hellc3p0">
<!-- 提供获取连接的4个基本信息 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///test</property>
<property name="user">root</property>
<property name="password">root</property>
<!-- 进行数据库连接池管理的基本信息 -->
<!-- 当数据库连接池中的连接数不够时,c3p0一次性向数据库服务器申请的连接数 -->
<property name="acquireIncrement">5</property>
<!-- c3p0数据库连接池中初始化时的连接数 -->
<property name="initialPoolSize">10</property>
<!-- c3p0数据库连接池维护的最少连接数 -->
<property name="minPoolSize">10</property>
<!-- c3p0数据库连接池维护的最多的连接数 -->
<property name="maxPoolSize">100</property>
<!-- c3p0数据库连接池最多维护的Statement的个数 -->
<property name="maxStatements">50</property>
<!-- 每个连接中可以最多使用的Statement的个数 -->
<property name="maxStatementsPerConnection">2</property>
</named-config>
</c3p0-config>
02.java测试代码
public static void main(String[] args) throws SQLException {
ComboPooledDataSource sc=new ComboPooledDataSource("hellc3p0");
Connection conn=sc.getConnection();
System.out.println(conn);
}
03.第三种方式配置类
01.配置类
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.beans.PropertyVetoException;
@Configuration
public class SpringConf {
@Bean
public ComboPooledDataSource user02() throws PropertyVetoException {
ComboPooledDataSource sc=new ComboPooledDataSource();
sc.setJdbcUrl("jdbc:mysql://216.127.*.*:3166/mall_pms?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false");
sc.setDriverClass("com.mysql.cj.jdbc.Driver");
sc.setUser("root");
sc.setPassword("root");
sc.setInitialPoolSize(10);
return sc;
}
}
02.测试
@SpringBootTest
class DemoestApplicationTests {
@Autowired
SpringConf springConf;
@Test
void contextLoads() throws PropertyVetoException, SQLException {
ComboPooledDataSource comboPooledDataSource = springConf.user02();
Connection connection = comboPooledDataSource.getConnection();
System.err.println("---"+connection);
System.err.println(springConf.user02());
PreparedStatement preparedStatement = connection.prepareStatement("select * from pms_brand where brand_id=1");
ResultSet resultSet = preparedStatement.executeQuery(
);
while (resultSet.next()){
System.err.println(resultSet.getString(2));
}
}
}