package com.kxs.adminap.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;

public class DbConn { 
    public static List<Dictionary<String, Object>> Query(String sql, String url, String username, String password) {
        List<Dictionary<String, Object>> list = new ArrayList<>();
        try {
            //利用反射的方法加载驱动器
            Class.forName("com.mysql.cj.jdbc.Driver");
    
            //使用Drivermanager的getConnectionn方法获取连接对象
            Connection root = DriverManager.getConnection(url, username, password);
    
            //获取statement对象
            Statement statement = root.createStatement();
    
            //指定sql语句,要执行增删改可使用executeUpdate（）方法
            ResultSet rs = statement.executeQuery(sql);
            ResultSetMetaData md = rs.getMetaData();//获取键名
            int columnCount = md.getColumnCount();//获取列的数量
            while (rs.next()) {
                Dictionary<String, Object> rowData = new Hashtable<>();//声明Map
                for (int i = 1; i <= columnCount; i++) {
                    Object val = rs.getObject(i);
                    if(val == null) val = "";
                    rowData.put(md.getColumnName(i), val);//获取键名及值
                }
                list.add(rowData);
            }

            rs.close();
            statement.close();
            root.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static List<Dictionary<String, Object>> Query(String sql, List<Object> params, String url, String username, String password) {
        List<Dictionary<String, Object>> list = new ArrayList<>();
        try {
            //利用反射的方法加载驱动器
            Class.forName("com.mysql.cj.jdbc.Driver");
    
            //使用Drivermanager的getConnectionn方法获取连接对象
            Connection root = DriverManager.getConnection(url, username, password);

            //获取statement对象
            PreparedStatement statement = root.prepareStatement(sql); //两个问号表示这个SQL语句有两个参数
            int index = 0;
            for (var item : params) {
                index += 1;
                statement.setObject(index, item); //设第index个参数的值
            }
            statement.execute();
    
            ResultSet rs = statement.getResultSet();
            ResultSetMetaData md = rs.getMetaData();//获取键名
            int columnCount = md.getColumnCount();//获取列的数量
            while (rs.next()) {
                Dictionary<String, Object> rowData = new Hashtable<>();//声明Map
                for (int i = 1; i <= columnCount; i++) {
                    Object val = rs.getObject(i);
                    if(val == null) val = "";
                    rowData.put(md.getColumnName(i), val);//获取键名及值
                }
                list.add(rowData);
            }

            rs.close();
            statement.close();
            root.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static Dictionary<String, Object> QueryOne(String sql, String url, String username, String password) {
        Dictionary<String, Object> object = new Hashtable<>();
        try {
            //利用反射的方法加载驱动器
            Class.forName("com.mysql.cj.jdbc.Driver");
    
            //使用Drivermanager的getConnectionn方法获取连接对象
            Connection root = DriverManager.getConnection(url, username, password);
    
            //获取statement对象
            Statement statement = root.createStatement();
    
            //指定sql语句,要执行增删改可使用executeUpdate（）方法
            ResultSet rs = statement.executeQuery(sql);
            ResultSetMetaData md = rs.getMetaData();//获取键名
            if (rs.getRow() > 0) {
                Object val = rs.getObject(0);
                if(val == null) val = "";
                object.put(md.getColumnName(0), val);//获取键名及值
            }

            rs.close();
            statement.close();
            root.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }

    public static Dictionary<String, Object> QueryOne(String sql, List<Object> params, String url, String username, String password) {
        Dictionary<String, Object> object = new Hashtable<>();
        try {
            //利用反射的方法加载驱动器
            Class.forName("com.mysql.cj.jdbc.Driver");
    
            //使用Drivermanager的getConnectionn方法获取连接对象
            Connection root = DriverManager.getConnection(url, username, password);
    
            //获取statement对象
            PreparedStatement statement = root.prepareStatement(sql); //两个问号表示这个SQL语句有两个参数
            int index = 0;
            for (var item : params) {
                index += 1;
                statement.setObject(index, item); //设第index个参数的值
            }
            statement.execute();
    
            //指定sql语句,要执行增删改可使用executeUpdate（）方法
            ResultSet rs = statement.getResultSet();
            ResultSetMetaData md = rs.getMetaData();//获取键名
            if (rs.getRow() > 0) {
                Object val = rs.getObject(0);
                if(val == null) val = "";
                object.put(md.getColumnName(0), val);//获取键名及值
            }

            rs.close();
            statement.close();
            root.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }

    public static void Op(String sql, String url, String username, String password) {
        try {
            //利用反射的方法加载驱动器
            Class.forName("com.mysql.cj.jdbc.Driver");
    
            //使用Drivermanager的getConnectionn方法获取连接对象
            Connection root = DriverManager.getConnection(url, username, password);
    
            //获取statement对象
            Statement statement = root.createStatement();
    
            //指定sql语句,要执行增删改可使用executeUpdate（）方法
            statement.execute(sql);

            statement.close();
            root.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void Op(String sql, List<Object> params, String url, String username, String password) {
        try {
            //利用反射的方法加载驱动器
            Class.forName("com.mysql.cj.jdbc.Driver");
    
            //使用Drivermanager的getConnectionn方法获取连接对象
            Connection root = DriverManager.getConnection(url, username, password);
    
            //获取statement对象
            PreparedStatement statement = root.prepareStatement(sql); //两个问号表示这个SQL语句有两个参数
            int index = 0;
            for (var item : params) {
                index += 1;
                statement.setObject(index, item); //设第index个参数的值
            }
            statement.execute();

            statement.close();
            root.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}