标签:def artifact nbsp mysq dep driver host log coding
pom.xml
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.4</version> </dependency>
c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!--默认配置--> <default-config> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> <property name="maxStatements">200</property> </default-config> <!--配置连接池mysql--> <named-config name="mysql"> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/library_ssm?serverTimezone=GMT%2B8</property> <property name="user">root</property> <property name="password">000000</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> <property name="maxStatements">200</property> </named-config> </c3p0-config>
C3p0Util
import com.mchange.v2.c3p0.ComboPooledDataSource; import org.apache.log4j.Logger; import java.sql.Connection; public class C3p0Util { private static Logger logger = Logger.getLogger(C3p0Util.class); private static ComboPooledDataSource cpds = new ComboPooledDataSource("mysql"); public static Connection getConnection(){ Connection conn=null; try { conn= cpds.getConnection(); logger.debug("从连接池获取了连接"); }catch (Exception e){ e.printStackTrace(); logger.error("获取连接异常"); } return conn; } public static void releaseConnection(Connection conn){ try{ if(conn!=null){ conn.close(); logger.debug("释放了一个连接"); } }catch (Exception e){ e.printStackTrace(); logger.error("释放连接异常"); } } }
标签:def artifact nbsp mysq dep driver host log coding
原文地址:https://www.cnblogs.com/Lorentz-Z/p/11974283.html