| | |
| | | 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.BinLogConstants; |
| | | import org.springblade.binlog.constant.FromConstants; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import javax.annotation.Resource; |
| | | import java.sql.Connection; |
| | | import java.sql.DriverManager; |
| | | import java.sql.PreparedStatement; |
| | | import java.sql.SQLException; |
| | | import java.sql.*; |
| | | import java.util.Properties; |
| | | |
| | | /** |
| | | * mysql 客户端连接 |
| | |
| | | @Component |
| | | public class MysqlClient { |
| | | |
| | | //声明对象 |
| | | private static MysqlClient 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(); |
| | | |
| | | @Resource |
| | | private BinLogConstants binLogConstants; |
| | | 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"); |
| | | |
| | | /** |
| | | * 初始化 |
| | | */ |
| | | @PostConstruct |
| | | public void init(){ |
| | | mysqlClient = this; |
| | | mysqlClient.binLogConstants = this.binLogConstants; |
| | | HikariDataSource ds = new HikariDataSource(config); |
| | | return ds; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param sql |
| | | */ |
| | | public static void sqlConnect(String sql,Integer type){ |
| | | String driver = "com.mysql.cj.jdbc.Driver"; |
| | | String url = mysqlClient.binLogConstants.getFromUrl(); |
| | | String user = mysqlClient.binLogConstants.getFromUsername(); |
| | | String password = mysqlClient.binLogConstants.getFromPassword(); |
| | | Connection conn = null; |
| | | PreparedStatement ps = null; |
| | | Connection connection = null; |
| | | PreparedStatement preparedStatement = null; |
| | | ResultSet resultSet = null; |
| | | try { |
| | | Class.forName ( driver ); |
| | | conn = (Connection) DriverManager.getConnection ( url, user, password ); |
| | | if (!conn.isClosed ()) { |
| | | log.info( "数据库连接成功!" ); |
| | | ps = conn.prepareStatement ( sql ); |
| | | //判断是否为修改,删除 |
| | | if (type==1){ |
| | | //修改删除 |
| | | ps.executeUpdate(); |
| | | log.info( "数据已发送成功!" ); |
| | | }else { |
| | | //新增 |
| | | ps.execute(); |
| | | log.info( "数据已发送成功!" ); |
| | | } |
| | | } |
| | | } catch (ClassNotFoundException e) { |
| | | e.printStackTrace(); |
| | | connection = ds.getConnection(); |
| | | preparedStatement = connection.prepareStatement(sql); |
| | | preparedStatement.executeUpdate(); |
| | | } catch (SQLException e) { |
| | | e.printStackTrace (); |
| | | }finally { |
| | | try { |
| | | ps.close(); |
| | | conn.close(); |
| | | } catch (SQLException throwables) { |
| | | throwables.printStackTrace(); |
| | | if (resultSet != null) resultSet.close(); |
| | | if (preparedStatement != null) preparedStatement.close(); |
| | | if (connection != null) connection.close(); |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |