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