package com.songshu.mongo.tools; import lombok.extern.slf4j.Slf4j; import java.sql.*; import java.util.ArrayList; import java.util.Map; @Slf4j public class JDBCUtilMysql { private static Connection connection = null; private static Statement statement = null; private static ResultSet result = null; // //测试 // private static String url = "jdbc:mysql://192.168.145.30:3306/steedos2?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true"; // private static String username = "root"; // private static String password = "Peony@2022"; // private static String driver="com.mysql.jdbc.Driver"; // //测试 // private static String url = "jdbc:mysql://192.168.3.5:3306/zulin?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true"; // private static String username = "root"; // private static String password = "Peony@2021"; // private static String driver="com.mysql.jdbc.Driver"; //生产 private static String url = "jdbc:mysql://192.168.145.30:3306/tingche?useUnicode=true&characterEncoding=utf-8" + "&rewriteBatchedStatements=true" + "&loginTimeout=6000000"; private static String username = "tingchegl"; private static String password = "tingchegl@123"; private static String driver="com.mysql.jdbc.Driver"; static { try { //4.注册驱动 Class.forName(driver); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnection() { try { // log.info("开始连接mysql"); // log.info("url----------------》"+url); // log.info("username----------------》"+username); // log.info("password----------------》"+password); DriverManager.setLoginTimeout(6000000); connection = DriverManager.getConnection(url, username, password); } catch (SQLException e) { // TODO: handle exception } // log.info("连接mysql connection---------》"+connection); return connection; } public static Statement getStatement() { try { // log.info("创建Statement 对象"); // log.info("connection----------------》"+connection); statement = connection.createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // log.info("创建Statement 对象statement--------》"+statement); return statement; } /** * 查询 * * @param sql * @return */ public static ResultSet executeQuery(String sql) { try { result = statement.executeQuery(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } /** * 修改 * * @param sql * @return */ public static int executeUpdate(String sql) { try { int i = statement.executeUpdate(sql); return i; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 0; } public static void getClose() { if (connection != null) { try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } connection = null; } if (statement != null) { try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } statement = null; } if (result != null) { try { result.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); result = null; } } } /** * 查询sql * * @param sql * @return */ public static ArrayList executeQueryList(String sql) { ArrayList titleNameList = new ArrayList<>(); ArrayList datalist = new ArrayList(); try { result = statement.executeQuery(sql); int columnCount = result.getMetaData().getColumnCount(); for (int i = 0; i < columnCount; i++) { titleNameList.add(result.getMetaData().getColumnLabel((i + 1))); } while (result.next()) { for (String name : titleNameList) { datalist.add(MapFactory.create().put(name, result.getString(name)).getData()); } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return datalist; } }