Java三种连接池(druid、c3p0、dbcp)

DRUID

概述:druid:阿里出品,淘宝和支付宝专用数据库连接池,支持所有JDBC兼容的数据库,包括Oracle、MySql、Derby、Postgresql、SQL Server、H2等等,Druid针对Oracle和MySql做了特别优化。下载网址:http://repo1.maven.org/maven2/com/alibaba/druid/

1、创建durid工程
这里写图片描述
2、在工程下新建lib文件夹
3、将durid的jar包和mysql-connector-java的jar包复制到lib下
这里写图片描述
4、给两个jar包添加build path
Java三种连接池(druid、c3p0、dbcp)_第1张图片
5、直接通过代码实现连接,代码如下

public void test1(){
        DruidDataSource dataSource = new DruidDataSource();
        //获取驱动
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        //建立连接
        dataSource.setUrl("jdbc:mysql://localhost:3306/class38?serverTimezone=Asia/Shanghai");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        try {
            //获取连接
            DruidPooledConnection conn = dataSource.getConnection();
            PreparedStatement statement = conn.prepareStatement("insert into student values(?,?,?,?)");
            statement.setInt(1, 13);
            statement.setString(2, "小明");
            statement.setString(3, "数据库");
            statement.setInt(4, 150);
            int i = statement.executeUpdate();
            System.out.println(i);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

6、通过配置文件实现连接

在src下新建文件druid.properties
这里写图片描述
配置文件的内容如下:
这里写图片描述
连接代码如下:

@Test
    public void test2(){
        //建立工厂
        DruidDataSourceFactory factory = new DruidDataSourceFactory();
        Properties p = new Properties();
        InputStream in = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties");
        try {
            //读取配置文件中的信息
            p.load(in);
            DataSource dataSource = factory.createDataSource(p);
            Connection conn = dataSource.getConnection();
            PreparedStatement statement = conn.prepareStatement("select * from student");
            ResultSet result = statement.executeQuery();
            while(result.next()){
                System.out.println(result.getInt(1)+","+result.getString(2)+","+result.getString(3)+","+result.getInt(4));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

C3P0

概述:连接池作者是《星球大战》迷,C3P0就是其中的一个机器人,并且这个名称中包涵connection 和pool的单词字母,因此叫这个名字。c3p0有自动回收空闲连接功能

  1. 同上,新建工程,工程下新建lib,复制jar包,添加路径。
    Java三种连接池(druid、c3p0、dbcp)_第2张图片
  2. 直接用代码连接,代码如下:
    @Test
    public void test1(){
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        try {
            dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/class38?serverTimezone=Asia/Shanghai");
            dataSource.setUser("root");
            dataSource.setPassword("123456");
            Connection conn = dataSource.getConnection();
            PreparedStatement statement = conn.prepareStatement("delete from student where id=?");
            statement.setInt(1, 11);
            int i = statement.executeUpdate();
            System.out.println(i);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3.通过配置文件连接
在src下新建c3p0.properties。
注意:配置文件的内容每行前面都要加“c3p0.”,否则将无法识别。
Java三种连接池(druid、c3p0、dbcp)_第3张图片
这里写图片描述
代码如下:

@Test
    public void test2(){
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        try {
            Connection conn = dataSource.getConnection();
            PreparedStatement statement = conn.prepareStatement("insert into student values(?,?,?,?)");
            statement.setInt(1, 12);
            statement.setString(2, "常帅楠");
            statement.setString(3, "大数据");
            statement.setInt(4, 101);
            int i = statement.executeUpdate();
            System.out.println(i);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

DBCP

与前面的两种实现方式几乎一样
就不做太多解释了。直接看代码。
Java三种连接池(druid、c3p0、dbcp)_第4张图片
配置文件内容如下:
这里写图片描述
代码:

package cn.jixiang;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.junit.Test;

public class DBCPDemo {
    @Test
    public void test(){
        //创建连接池对象
        BasicDataSource dataSource = new BasicDataSource();
        //设置参数
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/class38?serverTimezone=Asia/Shanghai");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        try {
            //获取连接对象
            Connection conn = dataSource.getConnection();
            //生成预编译Statement对象
            PreparedStatement statement = conn.prepareStatement("insert into student values(null,?,?,?)");
            statement.setString(1, "帅帅");
            statement.setString(2, "大数据");
            statement.setInt(3, 100);

            //执行sql语句
            int i = statement.executeUpdate();
            System.out.println(i);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test2(){
        BasicDataSourceFactory factory = new BasicDataSourceFactory();
        Properties p = new Properties();
        try {
            InputStream in = DBCPDemo.class.getClassLoader().getResourceAsStream("dbcp.properties");
            //加载配置文件
            p.load(in);
            //创建一个对象并返回
            DataSource dataSource = factory.createDataSource(p);
            Connection conn = dataSource.getConnection();
            PreparedStatement statement = conn.prepareStatement("select * from student");
            ResultSet result = statement.executeQuery();
            while(result.next()){
                System.out.println(result.getInt(1)+","+result.getString(2)+","+result.getString(3)+","+result.getInt(4));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

你可能感兴趣的:(java)