zrj
2024-07-03 0b1e9e70818f0e3eb32dd6c029d42d93236ecdc6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package org.springblade.binlog.client;
 
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springblade.binlog.constant.FromConstants;
import org.springframework.stereotype.Component;
 
import java.sql.*;
import java.util.Properties;
 
/**
 * mysql 客户端连接
 */
@Slf4j
@Component
public class MysqlClient {
 
    private static String url = "jdbc:mysql://127.0.0.1:3308/srjw?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true";
    private static String username = "root";
    private static String password = "root";
 
    private static final HikariDataSource ds = createDataSource();
 
    public static HikariDataSource createDataSource() {
        Properties properties = System.getProperties();
//        String profile = properties.getProperty("blade.env");
        String profile = properties.getProperty("spring.profiles.active");
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(FromConstants.setUrl(profile));
        config.setUsername(FromConstants.setUsername(profile));
        config.setPassword(FromConstants.setPassword(profile));
        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
 
        HikariDataSource ds = new HikariDataSource(config);
        return ds;
    }
 
    /**
     * sql 连接
     * @param sql
     */
    public static void sqlConnect(String sql,Integer type){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = ds.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                if (preparedStatement != null) preparedStatement.close();
                if (connection != null) connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
 
    /**
     * 连接mysql数据库 新增
     * @param sql
     */
    public static void insert(String sql) {
        sqlConnect(sql,2);
    }
 
    /**
     * 连接mysql数据库 修改
     * @param sql
     */
    public static void update(String sql) {
        sqlConnect(sql,1);
    }
 
    /**
     * 连接mysql数据库 删除
     * @param sql
     */
    public static void delete(String sql) {
        sqlConnect(sql,1);
    }
}