MysqlMutiConn.java 4.18 KB
package com.glsk.util;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Mysql连接,可以跨机器使用数据库连接
 */

public class MysqlMutiConn {
    private static HashMap<String, Connection> mutiConnection = new HashMap<>();

    public static void addConnection(String name, String url) {
        try {
            Connection connection = DriverManager.getConnection(url);
            mutiConnection.put(name, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(String name) {
        return mutiConnection.get(name);
    }

    public static void close(Connection connection) {
        try {
            connection.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static void executeUpdate(Connection connection, String sql) {
        PreparedStatement pstmt = null;
        try {
            connection.setAutoCommit(false);
            pstmt = connection.prepareStatement(sql);
            pstmt.executeUpdate();
            connection.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                pstmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void executeQuery(Connection connection, String sql, QueryCallback callback) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = connection.prepareStatement(sql);
            rs = pstmt.executeQuery();
            callback.process(rs);
            rs.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static ArrayList<ArrayList<String>> executeQuery(Connection connection, String sql, int columns) {
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            ArrayList<ArrayList<String>> result = new ArrayList<>();
            while (rs.next()) {
                String temp = "";
                ArrayList<String> colList = new ArrayList<>();
                for (int i = 1; i <= columns; i++) {
                    colList.add(rs.getString(i));
                }
                result.add(colList);
            }
            rs.close();
            ps.close();
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    // 流式查询
    public static void executeFlowQuery(Connection connection, String sql, QueryCallback callback) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            pstmt.setFetchSize(Integer.MIN_VALUE);
            pstmt.setFetchDirection(ResultSet.FETCH_REVERSE);
            rs = pstmt.executeQuery();
            callback.process(rs);
            rs.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static interface QueryCallback {
        void process(ResultSet rs) throws Exception;
    }


    public static void main(String[] args) {
        MysqlMutiConn.addConnection("querySession", "jdbc:mysql://192.168.1.39:3306/dj_smartcarlife?user=insurance_user&password=123456&useSSL=false&serverTimezone=UTC&characterEncoding=utf-8");
        MysqlMutiConn.addConnection("insertSession", "jdbc:mysql://192.168.3.227:3306/dj_smartcarlife?user=dev_admin&password=1f7e1ed2&useSSL=false&serverTimezone=UTC&characterEncoding=utf-8");

        Connection querySession = MysqlMutiConn.getConnection("querySession");
        Connection insertSession = MysqlMutiConn.getConnection("insertSession");

        String sql01="select order_code from dj_smartcarlife.dj_car_life_pay_order ";
        MysqlMutiConn.executeQuery(querySession, sql01, 1);



    }
}