数据库连接池

文章目录

  • 连接池概述
  • DBCP数据库连接池
    • dbcp.properties
    • 代码
  • C3P0数据库连接池【不需要读取配置文件】
    • c3p0.properties
    • c3p0工具类
    • 主方法

连接池概述

数据库连接池_第1张图片数据库连接池_第2张图片

数据库连接池_第3张图片在这里插入图片描述

DBCP数据库连接池

dbcp.properties

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

数据库连接池_第4张图片

代码

数据库连接池_第5张图片

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

数据库连接池_第6张图片

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;
            }
    }
}

数据库连接池_第7张图片

主方法

数据库连接池_第8张图片

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);
        }
    }
}

你可能感兴趣的