<dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources>
c3p0.driverClass=com.mysql.jdbc.Driver c3p0.jdbcUrl=jdbc:mysql://localhost:3306/test c3p0.user=root c3p0.password=123456 c3p0.initialPoolSize=5 c3p0.minPoolSize=5 c3p0.maxPoolSize=20 c3p0.maxIdleTime=1800
public class C3P0Utils { private static ComboPooledDataSource dataSource = null; static { try { dataSource = new ComboPooledDataSource(); dataSource.setDriverClass(C3P0Properties.getProperty("c3p0.driverClass")); dataSource.setJdbcUrl(C3P0Properties.getProperty("c3p0.jdbcUrl")); dataSource.setUser(C3P0Properties.getProperty("c3p0.user")); dataSource.setPassword(C3P0Properties.getProperty("c3p0.password")); dataSource.setInitialPoolSize(Integer.parseInt(C3P0Properties.getProperty("c3p0.initialPoolSize"))); dataSource.setMinPoolSize(Integer.parseInt(C3P0Properties.getProperty("c3p0.minPoolSize"))); dataSource.setMaxPoolSize(Integer.parseInt(C3P0Properties.getProperty("c3p0.maxPoolSize"))); dataSource.setMaxIdleTime(Integer.parseInt(C3P0Properties.getProperty("c3p0.maxIdleTime"))); } catch (PropertyVetoException e) { throw new RuntimeException(e); } } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } }
public class C3P0Properties { private static Properties props = new Properties(); static { try { props.load(C3P0Properties.class.getClassLoader().getResourceAsStream("c3p0.properties")); } catch (IOException e) { throw new RuntimeException(e); } } public static String getProperty(String key) { return props.getProperty(key); } }
本文链接:http://task.lmcjl.com/news/9396.html