首先:配置xml文件
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
创建c3p0数据源
// 连接
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/one");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("admin123");
// 打印输出,测试连接效果
Connection connection = comboPooledDataSource.getConnection();
System.out.println(connection);
connection.close();
创建druid数据源
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
druidDataSource.setUrl("jdbc:mysql://localhost:3306/one");
druidDataSource.setUsername("root");
druidDataSource.setPassword("admin123");
// 打印输出,测试连接效果
Connection connection = druidDataSource.getConnection();
System.out.println(connection);
connection.close();