Skip to content

DB를 통한 Login 개선 #41

@sumannam

Description

@sumannam

현재는 처음에 DB에 접근해서 모든 계정을 가지고 오는 방식임.

Main.java

    private static boolean performLogin() {
        while (true) {
            System.out.println("\n========= CSV 로그인 시스템 =========");
            System.out.print("아이디: ");
            String id = sc.nextLine();
            System.out.print("비밀번호: ");
            String pw = sc.nextLine();

            if (manager.login(id, pw)) return true;
            System.out.println("[오류] 아이디 또는 비밀번호가 틀렸습니다.");
        }
    }

LibraryManager.java

    public void initialize() {
        this.bookMap = repository.loadBooks();
        this.userList = repository.loadUsers();
        // ID 카운트 동기화
        for (Integer id : bookMap.keySet()) {
            if (id > bookCount) bookCount = id;
        }
    }


    public boolean login(String id, String pw) {
        for (User user : userList) {
            if (user.getUserId().equals(id) && user.getPassword().equals(pw)) {
                this.currentUser = user;
                return true;
            }
        }
        return false;
    }

LibraryRepository.java

    public List<User> loadUsers() {
        List<User> userList = new ArrayList<>();
        String sql = "SELECT * FROM users";

        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {

            while (rs.next()) {
                userList.add(new User(
                        rs.getString("user_id"),
                        rs.getString("password"),
                        rs.getString("type")
                ));
            }
        } catch (SQLException e) {
            System.err.println("[오류] 사용자 로드 실패: " + e.getMessage());
        }
        return userList;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Status

    Backlog

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions