<dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.5</version> </dependency> groovy implementation 'com.mchange:c3p0:0.9.5.5' <c3p0-config> <default-config> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydatabase</property> <property name="user">username</property> <property name="password">password</property> <property name="minPoolSize">5</property> <property name="maxPoolSize">20</property> <property name="acquireIncrement">5</property> <property name="maxIdleTime">1800</property> <property name="testConnectionOnCheckin">true</property> </default-config> </c3p0-config> import com.mchange.v2.c3p0.ComboPooledDataSource; import java.sql.Connection; import java.sql.SQLException; public class C3P0Example { private static ComboPooledDataSource dataSource; static { dataSource = new ComboPooledDataSource(); try { dataSource.setDriverClass("com.mysql.cj.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydatabase"); dataSource.setUser("username"); dataSource.setPassword("password"); dataSource.setMinPoolSize(5); dataSource.setMaxPoolSize(20); dataSource.setAcquireIncrement(5); dataSource.setMaxIdleTime(1800); dataSource.setTestConnectionOnCheckin(true); } catch (PropertyVetoException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } public static void main(String[] args) { Connection connection = null; try { connection = C3P0Example.getConnection(); // Perform database operations using the connection } catch (SQLException e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }


上一篇:
下一篇:
切换中文