DruidとHikariCPの基本テスト

21486 ワード

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