<property name="c3p0.driverClass">com.mysql.jdbc.Driver</property> <property name="c3p0.jdbcUrl">jdbc:mysql://localhost:3306/mydatabase</property> <property name="c3p0.user">username</property> <property name="c3p0.password">password</property> <property name="c3p0.initialPoolSize">5</property> <property name="c3p0.minPoolSize">5</property> <property name="c3p0.maxPoolSize">20</property> <property name="c3p0.maxIdleTime">1800</property> import com.mchange.v2.c3p0.ComboPooledDataSource; import java.sql.Connection; import java.sql.SQLException; public class C3P0Example { private static ComboPooledDataSource dataSource; static { try { dataSource = new ComboPooledDataSource(); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydatabase"); dataSource.setUser("username"); dataSource.setPassword("password"); dataSource.setInitialPoolSize(5); dataSource.setMinPoolSize(5); dataSource.setMaxPoolSize(20); dataSource.setMaxIdleTime(1800); } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } public static void releaseConnection(Connection connection) throws SQLException { if (connection != null) { connection.close(); } } public static void main(String[] args) { Connection connection = null; try { connection = C3P0Example.getConnection(); } catch (SQLException e) { e.printStackTrace(); } finally { try { C3P0Example.releaseConnection(connection); } catch (SQLException e) { e.printStackTrace(); } } } }


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