Java如何使用C3P0连接池呢?
下文笔者讲述java中C3P0连接池的使用方法分享,如下所示
C3P0的实现方法
1.引入C3P0所对应的jar包的坐标 2.设置C3P0的配置信息例:C3P0的使用示例
pom.xml <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.5</version> </dependency> 数据库相关的配置说明 acquireIncrement: 如果连接池中的连接被使用殆尽,将会以多少的数量进行增加,默认值为3。 initialPoolSize: 程序启动时连接池的数量,默认为3。 maxPoolSize: 最大连接数,默认为15. maxIdleTime: 几秒未使用的连接就会被丢弃,默认为0,不丢弃。 minPoolSize: 连接池中最小的数量 db.properties: DB.DRIVER_CLASS=com.mysql.cj.jdbc.Driver DB.DB_URL=jdbc:mysql://localhost:3306/testdb DB.DB_USER=root DB.DB_PASSWORD=admin DB.INITIAL_POOL_SIZE=5 DB.MAX_POOL_SIZE=5 //示例代码 import java.beans.PropertyVetoException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DSCreator { private static ComboPooledDataSource pooledDS; static { try { pooledDS = new ComboPooledDataSource(); Properties properties = new Properties(); // Loading properties file from classpath InputStream inputStream = DSCreator.class .getClassLoader() .getResourceAsStream("db.properties"); if(inputStream == null){ throw new IOException("File not found"); } properties.load(inputStream); pooledDS.setDriverClass(properties.getProperty("DB.DRIVER_CLASS")); pooledDS.setJdbcUrl(properties.getProperty("DB.DB_URL")); pooledDS.setUser(properties.getProperty("DB.DB_USER")); pooledDS.setPassword(properties.getProperty("DB.DB_PASSWORD")); pooledDS.setInitialPoolSize(Integer.parseInt(properties.getProperty("DB.INITIAL_POOL_SIZE"))); // Default anyway pooledDS.setAcquireIncrement(3); pooledDS.setMaxPoolSize(Integer.parseInt(properties.getProperty("DB.MAX_POOL_SIZE"))); }catch(IOException | PropertyVetoException e) { e.printStackTrace(); } } public static DataSource getDataSource() { return pooledDS; } } //Test 类 import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource; public class DSDemo { public static void main(String[] args) { DSDemo dsDemo = new DSDemo(); dsDemo.displayEmployeeById(16); } private void displayEmployeeById(int id){ Connection connection = null; String selectSQL = "SELECT * FROM EMPLOYEE WHERE id = ?"; PreparedStatement prepStmt = null; try { DataSource ds = DSCreator.getDataSource(); connection = ds.getConnection(); prepStmt = connection.prepareStatement(selectSQL); prepStmt.setInt(1, id); ResultSet rs = prepStmt.executeQuery(); while(rs.next()){ System.out.println("id: " + rs.getInt("id")); System.out.println("First Name: " + rs.getString("FIRST_NAME")); System.out.println("Last Name: " + rs.getString("LAST_NAME")); System.out.println("Department: " + rs.getString("DEPARTMENT")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(connection != null){ try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。