package login;

import java.sql.*;
import java.util.*;

public class UserDB {
    private final static String DB_URL = "jdbc:mysql://10.19.38.253:3306/db_student";
    private final static String DB_USER = "admin";
    private final static String DB_PASSWORD = "1227496003131313xY";

    public List<User> getAllUsers() throws ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        List<User> userList = new ArrayList<>();
        try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery("SELECT N_USER_ID, VC_LOGIN_NAME, VC_PASSWORD FROM t_user WHERE VC_LOGIN_NAME != 'admin';")) {
            while (resultSet.next()) {
                int id = resultSet.getInt("N_USER_ID");
                String name = resultSet.getString("VC_LOGIN_NAME");
                String password = resultSet.getString("VC_PASSWORD");
                User user = new User(id, name, password);
                userList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }

    public void ShowCommonUsers() throws ClassNotFoundException {
        List<User> userList = getAllUsers();
        for (User user : userList) {
            System.out.println(user);
        }
    }
}
