java使用Hikari连接池

 

DatabaseConnectionPool.java

package org.hxl.rds;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class DatabaseConnectionPool {


    private static HikariDataSource dataSource;

    static {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://192.168.1.14:3306/db_admin?useSSL=false");
        config.setUsername("root");
        config.setPassword("123456");
        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

        dataSource = new HikariDataSource(config);
    }

    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
}

 

InsertDataMysql.java

package org.hxl.rds;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class InsertDataMysql {

    public static void InsertData() throws SQLException {
        Connection conn = DatabaseConnectionPool.getConnection();
        PreparedStatement pstmt = null;

        try {
            conn.setAutoCommit(false);
            // 如果连接成功,则输出信息
            if (conn != null) {
                System.out.println("Connected to the database successfully!");
            } else {
                System.out.println("Failed to make connection!");
            }

            String sql = "insert into user (name, email) VALUES (?, ?)";
            int count = 0;

            pstmt = conn.prepareStatement(sql);

            for (int i = 0; i < 1000; i++) {
                String name = "hxltest"+ i;
                String email = name + "qq.com";
                pstmt.setString(1, name);
                pstmt.setString(2, email);
                int affectedRows = pstmt.executeUpdate();
                count++;

                if (count % 100 == 0){
                    conn.commit();
                }
            }
            conn.commit();
            // 关闭连接(可选)
            conn.close();

        } catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Date startDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = sdf.format(startDate);
        System.out.println("开始时间:" + startTime);

        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            final int threadNum = i;
            executorService.execute(() -> {
                try {
                    InsertData();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            });
        }

        executorService.shutdown();

        Date endDate = new Date();
        String endTime = sdf.format(endDate);
        System.out.println("结束时间:" + endTime);
    }
}

 

posted @ 2025-06-13 14:54  slnngk  阅读(29)  评论(0)    收藏  举报