简答c3p0配置
1.下载c3p0的jar文件和数据库连接的jar文件导入到工程
2.配置文件的信息
<c3p0-config> <!-- 使用默认的配置读取连接池对象 --> <default-config> <!-- 连接参数 --> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/test_db?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&rewriteBatchedStatements=TRUE</property> <property name="user">root</property> <property name="password">88888888</property> <!-- 连接池参数 --> <property name="initialPoolSize">5</property> <property name="maxPoolSize">10</property> <property name="checkoutTimeout">3000</property> </default-config> <named-config name="otherc3p0"> <!-- 连接参数 --> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/test_db</property> <property name="user">root</property> <property name="password">88888888</property> <!-- 连接池参数 --> <property name="initialPoolSize">5</property> <property name="maxPoolSize">8</property> <property name="checkoutTimeout">1000</property> </named-config> </c3p0-config>
3.连接的代码配置
// 1.创建数据库连接池对象 DataSource dataSource = new ComboPooledDataSource(); // DataSource dataSource = new ComboPooledDataSource("otherc3p0"); Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; // 2.创建数据库的连接池 try { connection = dataSource.getConnection(); statement = connection.prepareStatement("select * from students where id = ?"); statement.setInt(1,1); resultSet = statement.executeQuery(); while (resultSet.next()){ System.out.println(resultSet.getString("name")); } System.out.println(connection); } catch (SQLException e) { e.printStackTrace(); } finally { try { resultSet.close(); statement.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); } }