Druid和HikariCP的基本测试

Druid和HikariCP的配置方法

  • Druid
    • HikariCP
      • Test

Druid

public class Druid {
/*
# 配置数据库的连接参数
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mxd
username=root
password=**
​
# 配置连接池的参数
initialSize=5
maxActive=10
maxWait=3000
maxIdle=6
minIdle=3
*/
    public static DruidDataSource dataSource;
    //1.初始化Druid连接池
    static {
        //第二种方式:使用软编码通过配置文件初始化DBCP
        try {
            Properties properties = new Properties();
            //通过类加载器加载配置文件
            InputStream inputStream = Druid.class.getClassLoader().getResourceAsStream("db.properties");
            properties.load(inputStream);
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //2.获取连接
    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //3.关闭连接
    public static void closeAll(Connection connection, PreparedStatement statement, ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

HikariCP

public class HikariCP {
/*
driverClassName=com.mysql.cj.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/mxd
dataSource.user=root
dataSource.password=
#jdbc:mysql://localhost:3306/mxd
#dataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
#dataSource.databaseName=mxd
#dataSource.portNumber=3306
#dataSource.serverName=localhost
*/
    public static HikariDataSource dataSource=null;
    static {
        try {
          //软连接方法
            HikariConfig   config=new HikariConfig("/HikariCp.properties");
        //硬连接方法
           /*    HikariConfig config = new HikariConfig();
           config.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/mxd");//mysql
            config.setDriverClassName("com.mysql.cj.jdbc.Driver");
            config.setUsername("root");
            config.setPassword("@#MA847547125**");*/
            dataSource=new HikariDataSource(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //2.获取连接
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
    //3.关闭连接
    public static void closeAll(Connection connection, PreparedStatement statement, ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Test

import DBUtils.Druid;
import DBUtils.HikariCP;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtilsTest {
    @Test
    public void DruidTest() throws SQLException {
        String sql="select * from person";
       Connection conn=Druid.getConnection();
       PreparedStatement preparedStatement=conn.prepareStatement(sql);
        ResultSet resultSet=preparedStatement.executeQuery();
        while (resultSet.next()){
            System.out.println(resultSet.getInt(1)+resultSet.getString(2)+resultSet.getInt(3));
        }
        Druid.closeAll(conn,preparedStatement,resultSet);
    }
    @Test
    public void HikariCPTest() throws SQLException {
        System.out.println(1);
        String sql="select * from person";
        System.out.println(2);
        Connection connection= HikariCP.getConnection();
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        ResultSet resultSet=preparedStatement.executeQuery();
        while (resultSet.next()){
            System.out.println(resultSet.getInt(1)+resultSet.getString(2)+resultSet.getInt(3));
        }
        HikariCP.closeAll(connection,preparedStatement,resultSet);
    }
}

你可能感兴趣的:(Druid和HikariCP的基本测试)