JDBC使用连接池配置数据库连接1

C3P0数据库连接池

1.写c3p0-config.xml配置文件

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<c3p0-config>
    <!-- Mysql数据库连接的设置 -->
    <default-config>
        <!--Mysql数据库的驱动类-->
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <!--链接数据库的字符串-->
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/test3</property>
        <!--数据库的用户名-->
        <property name="user">root</property>
        <!--数据库的密码-->
        <property name="password">990128</property>
        <!-- 初始化连接数量 -->
        <property name="initialPoolSize">10</property>
        <!-- 最大的连接延迟 -->
        <property name="maxIdleTime">30</property>
        <!-- 最大连接数量10 和 最小连接数量1 -->
        <property name="maxPoolSize">10</property>
        <property name="minPoolSize">1</property>
    </default-config>
</c3p0-config>

2.写C3P0的工具类(需要导包)

1.导包C3P0的jar包
在这里插入图片描述
链接:https://pan.baidu.com/s/1YLliDdzLCoFZnhpBrQdUSg
提取码:sut2

细节:补充一个jar包,DButils框架的jar包,可以简化数据库操作的代码!!!
链接:https://pan.baidu.com/s/1nGPT0JbVwKmm97tzyIUbsQ
提取码:qosv
在这里插入图片描述

2.导入C3P0的jar包,写工具类
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.测试

在这里插入图片描述
1.总的工具类代码

package util;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.*;

/**
 *
 * @author wh
 */
public class C3P0Util {
    /**得到数据源*/
    private static DataSource dataSource = new ComboPooledDataSource();

    /**
     * 使用xml配置文件
     * todo 从数据源(连接池)获取连接对象
     */
    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("系统繁忙");
        }
    }

    /**关闭连接*/
    public static void close(Statement statement, Connection connection) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public static void close(ResultSet resultSet, Statement statement, Connection connection) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 这样配置依赖太强了,不推荐
     * 使用xml文件配置
     */
    /**
     * static {
     * try {
     * //Mysql数据库连接
     * dataSource.setDriverClass("com.mysql.jdbc.Driver");//
     * dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test3");
     * dataSource.setUser("root");
     * dataSource.setPassword("990128");
     * //初始化10个连接
     * dataSource.setInitialPoolSize(10);
     * } catch (PropertyVetoException e) {
     * e.printStackTrace();
     * }
     * <p>
     * }
     */

    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = C3P0Util.getConnection();
            System.out.println("连接:"+connection);
            //
            String sql = "insert into dataSource(driverClass,jdbcURL,user,password)" +
                    " values(?,?,?,?)";
            //预编译
            statement = connection.prepareStatement(sql);
            //参数赋值
            statement.setString(1, "com.mysql.jdbc.Driver");
            statement.setString(2, "jdbc:mysql://localhost:3306/test3");
            statement.setString(3, "root");
            statement.setString(4, "123");
            //执行
            statement.executeUpdate();
            System.out.println("执行成功!");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            //关闭资源
            C3P0Util.close(statement, connection);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值