2.7 数据库连接池 C3P0
c3p0-0.9.5.5
mchange-commons-java-0.2.20
C3P0 的配置 是 xml 文件,所以我们需要 写一份 xml 文件的配置。
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config><default-config><property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&charaterEncoding=utf8&useSSL=true&serverTimezone=UTC</property><property name="user">root</property><property name="password">123123</property><property name="acquireIncrement">5</property><property name="initialPoolSize">10</property><property name="minPoolSize">5</property><property name="maxPoolSize">20</property></default-config><named-config name="MySQL"><property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&charaterEncoding=utf8&useSSL=true&serverTimezone=UTC</property><property name="user">root</property><property name="password">123123</property><property name="acquireIncrement">5</property><property name="initialPoolSize">10</property><property name="minPoolSize">5</property><property name="maxPoolSize">20</property></named-config>
</c3p0-config>
下面的 named-config 和上面的 default-config 表示两个配置内容,如果你想使用 下面的 配置内容,就 必须 标注为 “MySQL”,如果你只想用默认的配置内容,则可以什么都不用提供。
//什么都不写,那么就用默认的配置内容。 dataSource = new ComboPooledDataSource();
package com.muquanyu.lesson03;import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;public class JdbcUtils_C3P0 {
//private static ComboPooledDataSource dataSource = null;private static DataSource dataSource = null;static {
try {
//代码版配置/*dataSource = new ComboPooledDataSource();dataSource.setDriverClass();dataSource.setUser();dataSource.setPassword();dataSource.setJdbcUrl();dataSource.setMaxPoolSize();dataSource.setMinPoolSize();*///创建数据源 (工厂模式)--> 创建//什么都不写,那么就用默认的配置内容。dataSource = new ComboPooledDataSource();} catch (Exception e) {
e.printStackTrace();}}//获取连接public static Connection getConnection() throws SQLException {
return dataSource.getConnection();}//释放资源public static void release(Connection conn, Statement st, ResultSet rs) {
if (rs != null) {
try {
rs.close();} catch (SQLException e) {
e.printStackTrace();}}if (st != null) {
try {
st.close();} catch (SQLException e) {
e.printStackTrace();}}if (conn != null) {
try {
conn.close();} catch (SQLException e) {
e.printStackTrace();}}}
}
package com.muquanyu.lesson03;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class C3P0Demo {
public static void main(String[] args) throws SQLException {
Connection connection = null;Statement statement = null;ResultSet resultSet = null;try {
connection = JdbcUtils_C3P0.getConnection(); //获取到数据库对象statement = connection.createStatement(); // 获取到 SQL 执行对象String sql = "insert into `users`(`id`,`NAME`,`PASSWORD`,`email`,`birthday`)" +" values('4','罗翔','159159','159159@23.com','1985-8-1')";int num = statement.executeUpdate(sql);if (num > 0) {
System.out.println("插入数据成功!");}} catch (SQLException e) {
e.printStackTrace();}String sql = "select * from `users`";resultSet = statement.executeQuery(sql);while (resultSet.next()) {
System.out.println(resultSet.getInt("id") + "|" + resultSet.getString("NAME")+ "|" + resultSet.getObject("PASSWORD") + "|" + resultSet.getString("email")+ "|" + resultSet.getDate("birthday"));}JdbcUtils_C3P0.release(connection, statement, resultSet);}
}
到了后期,你会发现 很多开源的强大的 jar包,别人写的轮子和库 都是 apache 的!