数据库连接池

连接池概述

在这里插入图片描述在这里插入图片描述

在这里插入图片描述在这里插入图片描述

DBCP数据库连接池

dbcp.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb1
username=root
password=root

在这里插入图片描述

代码

在这里插入图片描述

package cn.tedu.jdbc.pool;

import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class DBCPDemo {
    public static void main(String[] args) {
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            //读取dbcp的配置文件
            Properties p=new Properties();
            p.load(new FileInputStream(new File(DBCPDemo.class.
                    getClassLoader().getResource("dbcp.properties").getPath())));
            //创建工厂对象
            BasicDataSourceFactory factory=new BasicDataSourceFactory();
            //获取连接池对象
            DataSource source = factory.createDataSource(p);

            //通过连接池对象获取连接
            conn = source.getConnection();
            ps = conn.prepareStatement("select * from exam");
            rs = ps.executeQuery();
            while (rs.next()){
                System.out.println(rs.getInt("id")+","+rs.getString("name"));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(rs!=null)
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }finally {
                    rs=null;
                }
            if(ps!=null)
                try {
                    ps.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }finally {
                    ps=null;
                }
            if(conn!=null)
                try {
                    //归还连接
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }finally {
                    conn=null;
                }
        }
    }
}

C3P0数据库连接池【不需要读取配置文件】

c3p0.properties

c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.jdbcUrl=jdbc:mysql://localhost:3306/mydb1
c3p0.user=root
c3p0.password=root

在这里插入图片描述

c3p0工具类

//配合c3p0连接池使用的工具类
public class C3P0Utiles {
    //私有化构造方法
    private C3P0Utiles() {
    }

    //创建c3p0连接池对象(全局统一)
    //配置文件名称为c3p0才能默认去读取配置文件的内容
    private static ComboPooledDataSource source = new ComboPooledDataSource();

    //定义静态方法---注册驱动以及获取数据库连接
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        //获取连接(连接池默认注册数据库驱动)
        return source.getConnection();
    }

    //定义静态方法---关闭资源
    public static void close(Connection conn, Statement stat, ResultSet rs) {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                rs = null;
            }
        if (stat != null)
            try {
                stat.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                stat = null;
            }
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                conn = null;
            }
    }
}

在这里插入图片描述

主方法

在这里插入图片描述

package cn.tedu.jdbc.pool;

import cn.tedu.jdbc.utiles.C3P0Utiles;
import cn.tedu.jdbc.utiles.JDBCUtils;
import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class C3P0Demo {
    public static void main(String[] args) {
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        //创建连接池对象(配置文件名称为c3p0才能默认去读取配置文件的内容)
        ComboPooledDataSource source=new ComboPooledDataSource();
        try {
            //取出连接
            //conn = source.getConnection();
            conn = C3P0Utiles.getConnection();
            ps = conn.prepareStatement("select * from exam");
            rs = ps.executeQuery();
            while (rs.next()){
                System.out.println(rs.getInt("id")+","+rs.getString("name"));
            }
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        }finally {
            //JDBCUtils.close(conn,ps,rs);
            C3P0Utiles.close(conn,ps,rs);
        }
    }
}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sparky*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值