导入c3p0-0.9.5.2.jar、mchange-commons-java-0.2.11.jar、mysql-connector-java-8.0.17.jar
1.c3p0-config.xml:
<?xml version="1.0" encoding="utf-8" ?>
<c3p0-config>
<default-config>
<property name="user">root</property>
<property name="password"></property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/bookstore</property>
</default-config>
</c3p0-config>
2.utils:
public class utils {
private static DataSource dataSource = new ComboPooledDataSource();
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
public static Connection getConnection() throws SQLException {
Connection connection = threadLocal.get();
if (connection == null) {
connection = dataSource.getConnection();
threadLocal.set(connection);
}
return connection;
}
}