c3p0的使用(配置文件和xml形式 )

目录

 

1、首先要导入c3p0依赖

2.xml形式

1.首先要创建一个xml,其名字一定是c3p0-config.xml(代码中自动寻找)

2.测试

3、结果

3.配置文件形式

1、配置文件 c3p0.properties(名字不是固定)

2、测试代码(读取文件的方法)


1、首先要导入c3p0依赖

<dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1</version>
        </dependency>

jdbc依赖

<dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>5.1.25</version>

2.xml形式

1.首先要创建一个xml,其名字一定是c3p0-config.xml(代码中自动寻找)

<c3p0-config>
    <!--使用默认的配置读取数据库连接池对象 -->
    <default-config>
        <!--  连接参数 -->
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/website?serverTimezone=Asia/Shanghai</property>
        <property name="user">root</property>
        <property name="password">123456</property>

        <!-- 连接池参数 -->
        <!--初始化申请的连接数量-->
        <property name="initialPoolSize">5</property>
        <!--最大的连接数量-->
        <property name="maxPoolSize">10</property>
        <!--超时时间-->
        <property name="checkoutTimeout">3000</property>
    </default-config>

<!--    <named-config name="otherc3p0">-->
<!--        &lt;!&ndash;  连接参数 &ndash;&gt;-->
<!--        <property name="driverClass">com.mysql.jdbc.Driver</property>-->
<!--        <property name="jdbcUrl">jdbc:mysql://localhost:3306/hs_test?serverTimezone=Asia/Shanghai</property>-->
<!--        <property name="user">root</property>-->
<!--        <property name="password">root</property>-->

<!--        &lt;!&ndash; 连接池参数 &ndash;&gt;-->
<!--        <property name="initialPoolSize">5</property>-->
<!--        <property name="maxPoolSize">8</property>-->
<!--        <property name="checkoutTimeout">1000</property>-->
<!--    </named-config>-->
</c3p0-config>

2.测试

//1.创建数据库连接池对象

DataSource ds = new ComboPooledDataSource();

//2. 获取连接对象

Connection conn = ds.getConnection();后面的代码和jdbc就接通了

  public static void main(String[] args) throws SQLException {
        DataSource dataSource = new ComboPooledDataSource();

        Connection conn = dataSource.getConnection();

        String sql="select * from user";
        PreparedStatement pre=conn.prepareStatement(sql);
       ResultSet res= pre.executeQuery();
       while(res.next()){
           System.out.print(res.getString(1)+"   ");
           System.out.println(res.getString(2));
       }
    }

3、结果

查询出数据库的数据,不做展示

3.配置文件形式

1、配置文件 c3p0.properties(名字不是固定)

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/website?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=123456

# 初始化连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000

2、测试代码(读取文件的方法)

ComboPooledDataSource ds  = new ComboPooledDataSource();
        ResourceBundle rb = ResourceBundle.getBundle("c3p0");
        ds.setDriverClass(rb.getString("driverClass"));
        ds.setJdbcUrl(rb.getString("url"));
        ds.setUser(rb.getString("username"));
        ds.setPassword(rb.getString("password"));
        ds.setInitialPoolSize(Integer.parseInt(rb.getString("initialSize")));
        ds.setMaxPoolSize(Integer.parseInt(rb.getString("maxActive")));
        ds.setCheckoutTimeout(Integer.parseInt(rb.getString("maxWait")));

        Connection conn=ds.getConnection();
        String sql="select * from user";
        PreparedStatement pre=conn.prepareStatement(sql);
        ResultSet res=pre.executeQuery();
        while(res.next()){
            System.out.print(res.getString(1)+"  ");
            System.out.println(res.getString(2));
        }
        conn.close();
        pre.close();
        res.close();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值