MysqlConn.java 5.75 KB
package com.glsk.connection;

import com.glsk.conf.ConfigurationManager;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.*;
import java.util.ArrayList;

public class MysqlConn {
    private static Connection connection = null;

    static {
        try {
//            Class.forName("com.mysql.jdbc.Driver");
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private MysqlConn() {
    }

    public static Connection getConnection(String url) {
        if (connection == null) {
            synchronized (MysqlConn.class) {
                if (connection == null) {
                    try {
                        connection = DriverManager.getConnection(url);
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
            }
        }
        try {
            if (!connection.isValid(1)) {
                close(connection);
                connection = DriverManager.getConnection(url);
            }
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    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 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 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 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 void close(Connection connection) {
        try {
            connection.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

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

    public static void main(String[] args) throws UnsupportedEncodingException {
//        dbc:mysql://192.168.3.222:3308/scrm_account?user=os_user&password=os#123&autoReconnect=true&autoReconnectForPools=true


        System.out.println("URL=" + ConfigurationManager.getProperty("mysql.url.xlyq_agi"));

        String password = "v#@JrWCEH9g3";
        String encodedPassword = URLEncoder.encode(password, "UTF-8");
        String url = "jdbc:mysql://rm-wz9uqix4n8s738dq18o.mysql.rds.aliyuncs.com:3306/xlyq_agi"
                + "?user=bool"
                + "&password=" + encodedPassword
                + "&useSSL=false"
                + "&serverTimezone=Asia/Shanghai"
                + "&allowPublicKeyRetrieval=true"
                + "&autoReconnect=true"
                + "&autoReconnectForPools=true";


        Connection conn = MysqlConn.getConnection(url);
//        for (int i = 0; i < 100; i++) {
//            String sql = "insert into t_user(username,password) values('name"+(100+i) + "','pw"+(100+i) + "')";
//            System.out.println(sql);
//            MysqlConn.executeUpdate(conn, sql);
//        }

        final StringBuffer buffer = new StringBuffer();
        MysqlConn.executeQuery(conn, "select theme,content_unit,post_content from xlyq_agi.fund_post\n" +
                "where post_date = '2025-11-15' limit 10", new QueryCallback() {
            @Override
            public void process(ResultSet rs) throws Exception {
                while (rs.next()) {
//                    System.out.println(rs.getString(1));
                    buffer.append(rs.getString(1));
                }
            }
        });

        System.out.println(buffer.toString());

        MysqlConn.close(conn);
    }

}