JX405기_비트/mysql

Day12-2 게시판 관리 프로그램 MVC 패턴으로 MySQL연결

_하루살이_ 2023. 2. 7. 14:30

 

UserDTO

package model;

public class UserDTO {
    private int id;
    private String username;
    private String password;
    private String nickname;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public boolean equals(Object o) {
        if (o instanceof UserDTO) {
            UserDTO u = (UserDTO) o;
            return id == u.id;
        }
        return false;
    }

    public UserDTO(UserDTO origin) {
        id = origin.id;
        username = origin.username;
        password = origin.password;
        nickname = origin.password;
    }

    public UserDTO() {

    }

    public String toString(){ // 해당 객체의 값을 간략하기 출력하기 위함
        return "{" +
                "id: " + id + ", " +
                "username: " + username + ", " +
                "password: " + password + ", " +
                "nickname: " + nickname +
                "}";
    }

}

BoardDTO

package model;

import java.util.Comparator;
import java.util.Date;

public class BoardDTO implements Comparable<BoardDTO> {
    private int id;
    private String title;
    private int writerId;
    private String writerNickname;
    private String content;
    private Date entryDate;
    private Date modifyDate;

    public Date getEntryDate() {
        return entryDate;
    }

    public void setEntryDate(Date entryDate) {
        this.entryDate = entryDate;
    }

    public Date getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }

    public int getWriterId() {
        return writerId;
    }

    public void setWriterId(int writerId) {
        this.writerId = writerId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getWriterNickname() {
        return writerNickname;
    }

    public void setWriterNickname(String writerNickname) {
        this.writerNickname = writerNickname;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }


    public boolean equals(Object o) {
        if (o instanceof BoardDTO) {
            BoardDTO b = (BoardDTO) o;
            return id == b.id;
        }
        return false;
    }

    public BoardDTO(BoardDTO origin) {
        id = origin.id;
        title = origin.title;
        writerId = origin.writerId;
        writerNickname = origin.writerNickname;
        content = origin.content;
        entryDate = origin.entryDate;
        modifyDate = origin.modifyDate;
    }

    public BoardDTO() {

    }

    public BoardDTO(int id) {
        this.id = id;
    }

    @Override
    public int compareTo(BoardDTO b) {
        return this.id - b.id;
    }


}

ReplyDTO

package model;

import java.util.Date;

public class ReplyDTO {
    private int id;
    private String content;
    private int boardId;
    private int writerId;
    private String writerNickname;

    private Date entryDate;
    private Date modifyDate;

    public Date getEntryDate() {
        return entryDate;
    }

    public void setEntryDate(Date entryDate) {
        this.entryDate = entryDate;
    }

    public Date getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public int getBoardId() {
        return boardId;
    }

    public void setBoardId(int boardId) {
        this.boardId = boardId;
    }

    public int getWriterId() {
        return writerId;
    }

    public void setWriterId(int writerId) {
        this.writerId = writerId;
    }

    public String getWriterNickname() {
        return writerNickname;
    }

    public void setWriterNickname(String writerNickname) {
        this.writerNickname = writerNickname;
    }

    public ReplyDTO(){

    }

    public ReplyDTO(ReplyDTO original){
        this.id = original.id;
        this.content = original.content;
        this.boardId = original.boardId;
        this.writerId = original.writerId;
        this.writerNickname = original.writerNickname;
        this.entryDate = original.entryDate;
        this.modifyDate = original.modifyDate;

    }

    public boolean equals(Object o){
        if (o instanceof ReplyDTO){
            ReplyDTO r = (ReplyDTO) o;
            return id == r.id;
        }
        return false;
    }

    public ReplyDTO(int id){
        this.id = id;
    }


}

 

UserController

package dbController;

import model.UserDTO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserController {
    private Connection connection;

    public UserController(Connection connection){
        this.connection = connection;
    }

    public boolean insert(UserDTO userDTO){
        String query = "INSERT INTO `user`(`username`, `password`, `nickname`) VALUES (?, ?, ?)";

        try {
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.setString(1, userDTO.getUsername());
            pstmt.setString(2, userDTO.getPassword());
            pstmt.setString(3, userDTO.getNickname());

            pstmt.executeUpdate();

            pstmt.close();
        } catch (SQLException e) {
            return false;
        }
        return true;
    }

    public UserDTO auth(String username, String password){
        String query = "SELECT * FROM `user` WHERE `username` =? AND `password` = ?";
        try {
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.setString(1, username);
            pstmt.setString(2, password);

            ResultSet resultSet = pstmt.executeQuery();

            if (resultSet.next()){
                UserDTO userDTO = new UserDTO();
                userDTO.setId(resultSet.getInt("id"));
                userDTO.setUsername(resultSet.getString("username"));
                userDTO.setNickname(resultSet.getString("nickname"));

                return userDTO;
            }

            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    public void update(UserDTO userDTO){
        String query = "UPDATE `user` SET `password` = ?, `nickname` = ? WHERE `id` = ?";
        try {
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.setString(1, userDTO.getPassword());
            pstmt.setString(2, userDTO.getNickname());
            pstmt.setInt(3, userDTO.getId());

            pstmt.executeUpdate();

            pstmt.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(int id){
        String query = "DELETE FROM `user` WHERE `id` = ?";

        try {
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.setInt(1, id);

            pstmt.executeUpdate();

            pstmt.close();

        } catch (SQLException e){
            e.printStackTrace();
        }
    }

    public UserDTO selectOne(int id){
        UserDTO u = null;
        String query = "SELECT * FROM `user` WHERE `id` = ?";

        try {
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.setInt(1, id);

            ResultSet resultSet = pstmt.executeQuery();
            if (resultSet.next()){
                u = new UserDTO();
                u.setId(resultSet.getInt("id"));
                u.setNickname(resultSet.getString("nickname"));
            }

            resultSet.close();
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return u;
    }
}

BoardController

package dbController;

import model.BoardDTO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.PropertyResourceBundle;

public class BoardController {
    private Connection connection;

    public BoardController(Connection connection){
        this.connection = connection;
    }

    public void insert(BoardDTO boardDTO){
        String query = "INSERT INTO `board`(`title`, `content`, `writerId`, `entry_date`, `modify_date`) VALUES (?, ?, ?, NOW(), NOW())";

        try {
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.setString(1, boardDTO.getTitle());
            pstmt.setString(2, boardDTO.getContent());
            pstmt.setInt(3, boardDTO.getWriterId());

            pstmt.executeUpdate();

            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public ArrayList<BoardDTO> selectAll(){
        ArrayList<BoardDTO> list = new ArrayList<>();

        String query = "SELECT * FROM `board` ORDER BY `board`.`id` DESC";

        try {
            PreparedStatement pstmt = connection.prepareStatement(query);
            ResultSet resultSet = pstmt.executeQuery();

            while (resultSet.next()){
                BoardDTO b = new BoardDTO();
                b.setId(resultSet.getInt("id"));
                b.setTitle(resultSet.getString("title"));
                b.setContent(resultSet.getString("content"));
                b.setWriterId(resultSet.getInt("writerId"));
                b.setEntryDate(resultSet.getTimestamp("entry_date"));
                b.setModifyDate(resultSet.getTimestamp("modify_date"));

                list.add(b);
            }

            resultSet.close();
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    public BoardDTO selectOne(int id){
        BoardDTO boardDTO = null;
        String query = "SELECT * FROM `board` WHERE `id` = ?";
        try {
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.setInt(1, id);

            ResultSet resultSet = pstmt.executeQuery();

            if (resultSet.next()){
                boardDTO = new BoardDTO();
                boardDTO.setId(resultSet.getInt("id"));
                boardDTO.setTitle(resultSet.getString("title"));
                boardDTO.setContent(resultSet.getString("content"));
                boardDTO.setWriterId(resultSet.getInt("writerId"));
                boardDTO.setEntryDate(resultSet.getTimestamp("entry_date"));
                boardDTO.setModifyDate(resultSet.getTimestamp("modify_date"));
            }

            resultSet.close();
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return boardDTO;
    }

    public void update(BoardDTO boardDTO){
        String query = "UPDATE `board` SET `title` = ?, `content` = ? , `modify_date` = NOW() WHERE `id` = ?";

        try {
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.setString(1, boardDTO.getTitle());
            pstmt.setString(2, boardDTO.getContent());
            pstmt.setInt(3, boardDTO.getId());

            pstmt.executeUpdate();

            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(int id){
        String query = "DELETE FROM `board` WHERE `id` = ?";

        try {
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.setInt(1, id);

            pstmt.executeUpdate();

            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

ReplyController

package dbController;

import model.ReplyDTO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

public class ReplyController {
    private Connection connection;
    public ReplyController(Connection connection){
        this.connection = connection;

    }

    public void insert(ReplyDTO replyDTO){
        String query = "INSERT INTO `reply` (`content`, `board_id`, `writer_id`, `entry_date`) VALUES (?, ?, ?, NOW())";

        try {
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.setString(1, replyDTO.getContent());
            pstmt.setInt(2, replyDTO.getBoardId());
            pstmt.setInt(3, replyDTO.getWriterId());

            pstmt.executeUpdate();

            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 댓글 리스트 불러오기
    public ArrayList<ReplyDTO> selectAll(int boardId){
        ArrayList<ReplyDTO> temp = new ArrayList<>();
        String query = "SELECT * FROM `reply` WHERE `board_id` = ?";
        try {
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.setInt(1, boardId);
            ResultSet resultSet = pstmt.executeQuery();

            while (resultSet.next()){
                ReplyDTO r = new ReplyDTO();
                r.setId(resultSet.getInt("id"));
                r.setContent(resultSet.getString("content"));
                r.setBoardId(resultSet.getInt("board_id"));
                r.setWriterId(resultSet.getInt("writer_id"));
                r.setEntryDate(resultSet.getTimestamp("entry_date"));
                r.setModifyDate(resultSet.getTimestamp("modify_date"));

                temp.add(r);
            }

            pstmt.close();
        } catch (SQLException e){
            e.printStackTrace();
        }

        return temp;
    }

    // 특정 댓글 목록 불러오기
    public ReplyDTO selectOne(int id){
        ReplyDTO r = null;
        String query = "SELECT * FROM `reply` WHERE `id` = ?";
        try {
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.setInt(1, id);
            ResultSet resultSet = pstmt.executeQuery();
            if (resultSet.next()){
                r = new ReplyDTO();
                r.setId(resultSet.getInt("id"));
                r.setContent(resultSet.getString("content"));
                r.setBoardId(resultSet.getInt("board_id"));
                r.setWriterId(resultSet.getInt("writer_id"));
                r.setEntryDate(resultSet.getTimestamp("entry_date"));
                r.setModifyDate(resultSet.getTimestamp("modify_date"));

            }

            resultSet.close();
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return r;
    }

    public void update(ReplyDTO replyDTO){
        String query = "UPDATE `reply` SET `content` = ?, `modify_date` = NOW() WHERE `id` = ?";
        try {
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.setString(1, replyDTO.getContent());
            pstmt.setInt(2, replyDTO.getId());

            pstmt.executeUpdate();

            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(int id){
        String query = "DELETE FROM `reply` WHERE `id` = ?";
        try {
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.setInt(1, id);
            pstmt.executeUpdate();

            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

 

UserViewer

package dbViewer;

import dbConn.ConnectionMaker;
import dbController.UserController;
import model.UserDTO;
import util.ScannerUtil;

import java.sql.Connection;
import java.util.Scanner;

public class UserViewer {
    private final Scanner SCANNER;
    private Connection connection;
    private UserDTO logIn;

    public UserViewer(ConnectionMaker connectionMaker) {
        SCANNER = new Scanner(System.in);
        connection = connectionMaker.makeConnection();
    }

    public void showIndex() {
        String message = "1. 로그인 2. 회원가입 3. 종료";
        while (true) {
            int userChoice = ScannerUtil.nextInt(SCANNER, message);
            if (userChoice == 1) {
                auth();
                if (logIn != null){
                    showMenu();
                }
            } else if (userChoice == 2) {
                register();
            } else if (userChoice == 3) {
                System.out.println("사용해주셔서 감사합니다.");
                break;
            }
        }
    }

    private void register() {
        String message;
        message = "사용하실 아이디를 입력해주세요.";

        UserDTO u = new UserDTO();
        u.setUsername(ScannerUtil.nextLine(SCANNER, message));

        message = "사용하실 비밀번호를 입력해주세요.";
        u.setPassword(ScannerUtil.nextLine(SCANNER, message));

        message = "사용하실 닉네임을 입력해주세요.";
        u.setNickname(ScannerUtil.nextLine(SCANNER, message));

        UserController userController = new UserController(connection);
        if (!userController.insert(u)){
            System.out.println("중복된 아이디입니다.");
            message = "새로운 아이디로 가입을 시도하시겠습니까? Y/N";
            String yesNo = ScannerUtil.nextLine(SCANNER, message);
            if (yesNo.equalsIgnoreCase("Y")){
                register();
            }
        } else {
            userController.insert(u);
        }

    }

    private void auth() {
        String message;
        message = "아이디를 입력해주세요.";
        String username = ScannerUtil.nextLine(SCANNER, message);

        message = "비밀번호를 입력해주세요.";
        String password = ScannerUtil.nextLine(SCANNER, message);

        UserController userController = new UserController(connection);

        logIn = userController.auth(username, password);

        if (logIn == null) {
            System.out.println("로그인 정보가 정확하지 않습니다.");
        }
    }

    private void showMenu() {
        String message = "1. 게시판으로 2. 회원 정보 관리 3. 로그아웃";
        while (logIn != null) {
            int userChoice = ScannerUtil.nextInt(SCANNER, message);
            if (userChoice == 1) {
                BoardViewer boardViewer = new BoardViewer(SCANNER, connection, logIn);
                boardViewer.showMenu();
            } else if (userChoice == 2) {
                printOne();
            } else if (userChoice == 3) {
                logIn = null;
                System.out.println("정상적으로 로그아웃되었습니다.");
            }
        }
    }

    private void printOne() {
        System.out.println("회원 번호: " + logIn.getId());
        System.out.println("회원 닉네임: " + logIn.getNickname());
        System.out.println("---------------------------------------------");
        String message = "1. 수정 2. 탈퇴 3. 뒤로가기";
        int userChoice = ScannerUtil.nextInt(SCANNER, message);
        if (userChoice == 1) {
            update();
        } else if (userChoice == 2) {
            delete();
        }
    }

    private void update() {

        String message = "새로운 비밀번호를 입력해주세요.";
        String newPassword = ScannerUtil.nextLine(SCANNER, message);

        message = "새로운 닉네임을 입력해주세요.";
        String newNickname = ScannerUtil.nextLine(SCANNER, message);

        message = "기존 비밀번호를 입력해주세요.";
        String oldPassword = ScannerUtil.nextLine(SCANNER, message);

        UserController userController = new UserController(connection);

        if (userController.auth(logIn.getUsername(), oldPassword) != null) {
            logIn.setNickname(newNickname);
            logIn.setPassword(newPassword);

            userController.update(logIn);
        } else {
            System.out.println("회원 정보 변경에 실패하였습니다.");
        }
    }

    private void delete() {
        String message = "정말로 삭제하시겠습니까? Y/N";
        String yesNo = ScannerUtil.nextLine(SCANNER, message);

        if (yesNo.equalsIgnoreCase("Y")) {
            message = "비밀번호를 입력해주세요.";
            String password = ScannerUtil.nextLine(SCANNER, message);

            UserController userController = new UserController(connection);

            if (userController.auth(logIn.getUsername(), password) != null) {
                userController.delete(logIn.getId());
                logIn = null;
            }
        }
    }
}

BoardViewer

package dbViewer;

import dbConn.ConnectionMaker;
import dbController.BoardController;
import dbController.UserController;
import model.BoardDTO;
import model.UserDTO;
import util.ScannerUtil;

import java.sql.Connection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Scanner;

public class BoardViewer {
    private BoardController boardController;
    private final Scanner SCANNER;
    private Connection connection;
    private final String DATE_FORMAT = "yy/MM/dd HH:mm:ss";
    private UserDTO logIn;

    public BoardViewer(Scanner scanner, Connection connection, UserDTO logIn) {
        this.connection = connection;
        boardController = new BoardController(this.connection);
        this.logIn = logIn;
        SCANNER = scanner;
    }

    public void showMenu() {
        String message = "1. 새 글 작성하기 2. 글 목록 보기 3. 종료";
        while (true) {
            int userChoice = ScannerUtil.nextInt(SCANNER, message);
            if (userChoice == 1) {
                writeBoard();
            } else if (userChoice == 2) {
                printList();
            } else if (userChoice == 3) {
                System.out.println("사용해주셔서 감사합니다.");
                break;
            }
        }
    }

    private void writeBoard() {
        BoardDTO boardDTO = new BoardDTO();

        boardDTO.setWriterId(logIn.getId());

        String message = "글의 제목을 입력해주세요.";
        boardDTO.setTitle(ScannerUtil.nextLine(SCANNER, message));

        message = "글의 내용을 입력해주세요.";
        boardDTO.setContent(ScannerUtil.nextLine(SCANNER, message));

        boardController.insert(boardDTO);
    }

    private void printList() {
        ArrayList<BoardDTO> list = boardController.selectAll();

        if (list.isEmpty()) {
            System.out.println("아직 등록된 글이 없습니다.");
        } else {
            DateFormat df = new SimpleDateFormat(DATE_FORMAT);
            for (BoardDTO b : list) {
                System.out.printf("%d. %s - %s\n", b.getId(), b.getTitle(), df.format(b.getEntryDate()));
            }

            String message = "상세보기할 글의 번호나 뒤로 가실려면 0을 입력해주세요.";
            int userChoice = ScannerUtil.nextInt(SCANNER, message);

            while (userChoice != 0 && !list.contains(new BoardDTO(userChoice))) {
                System.out.println("잘못 입력하셨습니다.");
                userChoice = ScannerUtil.nextInt(SCANNER, message);
            }

            if (userChoice != 0) {
                printOne(userChoice);
            }
        }
    }

    private void printOne(int id) {
        UserController userController = new UserController(connection);
        ReplyViewer replyViewer = new ReplyViewer(SCANNER, connection, logIn);

        DateFormat df = new SimpleDateFormat(DATE_FORMAT);
        BoardDTO boardDTO = boardController.selectOne(id);
        System.out.println("=================================================");
        System.out.println(boardDTO.getTitle());
        System.out.println("-------------------------------------------------");
        System.out.println("글 번호: " + boardDTO.getId());
        System.out.println("글 작성자: " + userController.selectOne(boardDTO.getWriterId()).getNickname());
        System.out.println("작성일: " + df.format(boardDTO.getEntryDate()));
        System.out.println("작성일: "+ df.format(boardDTO.getModifyDate()));
        System.out.println("-------------------------------------------------");
        System.out.println(boardDTO.getContent());
        System.out.println("-------------------------------------------------");
        System.out.println("댓글");
        System.out.println("-------------------------------------------------");
        replyViewer.printAll(id);
        System.out.println("=================================================");
        String message;
        int userChoice;

        if (boardDTO.getWriterId() == logIn.getId()) {
            message = "1. 수정 2. 삭제 3. 댓글 메뉴 4. 뒤로 가기";
            userChoice = ScannerUtil.nextInt(SCANNER, message, 1, 4);
        } else {
            message = "3. 댓글 메뉴 4. 뒤로 가기";
            userChoice = ScannerUtil.nextInt(SCANNER, message, 3, 4);
        }

        if (userChoice == 1) {
            update(id);
        } else if (userChoice == 2) {
            delete(id);
        } else if (userChoice == 3) {
            replyViewer.showMenu(id);
            printOne(id);
        } else if (userChoice == 4) {
            printList();
        }
    }

    private void update(int id) {
        BoardDTO b = boardController.selectOne(id);

        String message = "새로운 제목을 입력해주세요.";
        b.setTitle(ScannerUtil.nextLine(SCANNER, message));

        message = "새로운 내용을 입력해주세요.";
        b.setContent(ScannerUtil.nextLine(SCANNER, message));

        boardController.update(b);
    }

    private void delete(int id) {
        String message = "정말로 삭제하시겠습니까? Y/N";
        String yesNo = ScannerUtil.nextLine(SCANNER, message);
        if (yesNo.equalsIgnoreCase("Y")) {
            boardController.delete(id);
            printList();
        } else {
            printOne(id);
        }
    }
}

ReplyViewer

package dbViewer;

import dbController.ReplyController;
import dbController.UserController;
import model.ReplyDTO;
import model.UserDTO;
import util.ScannerUtil;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Scanner;

public class ReplyViewer {
    private Scanner SCANNER;
    private ReplyController replyController;
    private UserDTO logIn;
    private Connection connection;

    public ReplyViewer(Scanner scanner, Connection connection, UserDTO logIn){
        this.SCANNER = scanner;
        this.connection = connection;
        replyController = new ReplyController(this.connection);
        this.logIn = logIn;
    }

    public void printAll(int boardId){
        UserController userController = new UserController(connection);
        ArrayList<ReplyDTO> list = replyController.selectAll(boardId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyMd H:m:s");
        for (ReplyDTO r : list){
            String date = sdf.format(r.getEntryDate());
            if (r.getModifyDate() != null) {
                date = sdf.format(r.getModifyDate());
            }

            System.out.printf("%d. %s(%s) : %s\n", r.getId(), userController.selectOne(r.getWriterId()).getNickname(), date, r.getContent());

        }
    }

    public void showMenu(int boardId){
        String message = "1. 댓글 등록 2. 댓글 수정 3. 댓글 삭제 4. 뒤로 가기";
        int userChoice = ScannerUtil.nextInt(SCANNER, message);
        if (userChoice == 1) {
            writeReply(boardId);
        } else if (userChoice != 4){
            message = "수정/삭제할 댓글 번호나 뒤로 가실려면 0를 입력해주세요.";
            int targetId = ScannerUtil.nextInt(SCANNER, message);
            while (targetId != 0 && replyController.selectOne(targetId) == null){
                System.out.println("잘못 입력하셨습니다.");
                targetId = ScannerUtil.nextInt(SCANNER, message);
            }

            if (userChoice == 2) {
                updateReply(targetId, boardId);
            } else if (userChoice == 3) {
                deleteReply(targetId, boardId);
            }
        }
    }

    private void writeReply(int boardId){
        ReplyDTO r = new ReplyDTO();
        r.setBoardId(boardId);
        r.setWriterId(logIn.getId());
        r.setWriterNickname(logIn.getNickname());

        String message = "댓글 내용을 입력해주세요.";
        r.setContent(ScannerUtil.nextLine(SCANNER, message));

        replyController.insert(r);
    }


    private void deleteReply(int id, int boardId){
        ReplyDTO r = replyController.selectOne(id);
        if (r !=null && r.getBoardId() == boardId && r.getWriterId() == logIn.getId()){
            String message = "정말로 삭제하시겠습니까? Y/N";
            String yesNo = ScannerUtil.nextLine(SCANNER, message);
            if (yesNo.equalsIgnoreCase("Y")){
                replyController.delete(id);
            }
        }
    }


    private void updateReply(int id, int boardId){
        ReplyDTO r = replyController.selectOne(id);
        if (r !=null && r.getBoardId() == boardId && r.getWriterId() == logIn.getId()){
            String message = "새로운 내용을 입력해주세요.";
            r.setContent(ScannerUtil.nextLine(SCANNER,message));

            replyController.update(r);

        }
    }

}