mysql(26) : mysql8轻量java工具类

maven依赖

        
            mysql
            mysql-connector-java
            8.0.18
        


import java.sql.*;
import java.util.LinkedList;
import java.util.List;

/**
 * @Auther: liyue
 * @Date: 2021/4/21 18:10
 * @Description:
 */
public class MysqlUtil {

    private static final String driver = "com.mysql.cj.jdbc.Driver";
    // 获取mysql连接地址
    private static final String url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf-8&serverTimezone=GMT%2b8&allowMultiQueries=true";
    // 数据名称
    private static final String username = "root";
    // 数据库密码
    private static final String password = "123456";

    private static Connection conn;

    static {
        try {
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            System.err.println("连接池获取失败");
            throw new RuntimeException(e);
        }
    }


    public static void execute(String sql) {
        try (Statement statement = conn.createStatement();) {
            Class.forName(driver);
            statement.execute(sql);
        } catch (Exception e) {
            System.err.println(sql);
            throw new RuntimeException(e);
        }
    }


    public static void close() {
        try {
            conn.close();
        } catch (Exception e) {
            System.err.println("连接关闭失败");
            throw new RuntimeException(e);
        }
    }

    public List> query(String sql) {
        List> lists = new LinkedList<>();
        long startTime = System.currentTimeMillis();
        try (PreparedStatement ps = conn.prepareStatement(sql);
             ResultSet rs = ps.executeQuery()) {
            List titles = new LinkedList<>();
            ResultSetMetaData rsmd = rs.getMetaData();
            final int columnCount = rsmd.getColumnCount();

            // 结果为空时直接返回,方便后续处理。关于如何判空参见下面链接
            boolean rsIsEmpty = !rs.isBeforeFirst();
            if (rsIsEmpty) {
                return lists;
            }
            // 添加列名方便后续处理
            for (int i = 1; i <= columnCount; i++) {
                titles.add(rsmd.getColumnName(i));
            }
            lists.add(titles);

            while (rs.next()) {
                List list = new LinkedList<>();
                for (int i = 1; i <= columnCount; i++) {
                    list.add((rs.getString(i) == null || rs.getString(i).equals("")) ? "" : rs.getString(i).trim());
                }
                lists.add(list);
            }
        } catch (SQLException e) {
            System.err.println("查询sql失败,sql:" + sql);
            throw new RuntimeException(e);

        }
        long endTime = System.currentTimeMillis();
        System.out.println("查询sql:" + sql + ", 运行时长:" + (endTime - startTime) + " ms, 数据量: " + lists.size());
        return lists;
    }

    /**
     *         
     *             mysql
     *             mysql-connector-java
     *             8.0.18
     *         
     */
}

你可能感兴趣的:(mysql,工具类,mysql封装)