vo
package example_10_23_board.vo;
import java.util.Date;
public class Board {
int no;
String title;
String writer;
String content;
int password;
int likeCount;
int disLikeCount;
Date createdDate;
public Board() {}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
public int getLikeCount() {
return likeCount;
}
public void setLikeCount(int likeCount) {
this.likeCount = likeCount;
}
public int getDisLikeCount() {
return disLikeCount;
}
public void setDisLikeCount(int disLikeCount) {
this.disLikeCount = disLikeCount;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
}
Exception
package example_10_23_board.exception;
public class BoardException extends RuntimeException {
private static final long serialVersionUID = 7078461549208308703L;
public BoardException(String message) {
super(message);
}
}
DAO
package example_10_23_board.dao;
import java.sql.SQLException;
import java.util.List;
import example_10_23_board.vo.Board;
public interface BoardDao{
/**
* 게시물을 전체 조회하는 기능이다.
* @return 게시물 전체 기능
* @throws SQLException DB Access 예외가 발생시 출력한다.
*/
List<Board> getAllPrint() throws SQLException;
/**
* 게시물을 번호로 입력받아서 상세 정보를 출력하는 기능이다.
* @param no 게시물 번호
* @return 조회된 게시물
* @throws SQLException DB Access 예외가 발생시 출력한다.
*/
Board getDetailPrintByNo(int no) throws SQLException;
/**
* 게시물을 제목으로 입력받아서 상세 정보를 출력하는 기능이다.
* @param title 게시물 제목
* @return 조회된 게시물
* @throws SQLException DB Access 예외가 발생시 출력한다.
*/
List<Board> getDetailPrintByTitle(String title) throws SQLException;
/**
* 게시물을 입력하는 기능이다.
* @param title 게시물 제목
* @param writer 게시물 작성자
* @param content 게시물 내용
* @param password 게시물 비밀번호
* @throws SQLException DB Access 예외가 발생시 출력한다.
*/
void insertBoard(String title, String writer, String content, int password) throws SQLException;
/**
* 게시물을 수정하는 기능이다.
* @param board 게시물 객체
* @throws SQLException DB Access 예외가 발생시 출력한다.
*/
void updateBoard(Board board) throws SQLException;
/**
* 게시물을 제거하는 기능이다.
* @param no 게시물 번호
* @param password 게시물 비밀번호
* @throws SQLException DB Access 예외가 발생시 출력한다.
*/
void removeBoard(int no, int password) throws SQLException;
}
package example_10_23_board.dao;
import static utils.ConnectionUtil.getConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import example_10_23_board.vo.Board;
public class BoardJdbcDao implements BoardDao {
@Override
public List<Board> getAllPrint() throws SQLException {
List<Board> boards = new ArrayList<>();
String sql ="select board_no, board_title, board_writer, board_content, "
+"board_like_count, board_dislike_count, board_created_date "
+"from tb_sample_boards "
+"order by board_no desc ";
Connection connection = getConnection();
PreparedStatement ptmt = connection.prepareStatement(sql);
ResultSet rs = ptmt.executeQuery();
while(rs.next()) {
Board borad = new Board();
borad.setNo(rs.getInt("board_no"));
borad.setTitle(rs.getString("board_title"));
borad.setWriter(rs.getString("board_writer"));
borad.setContent(rs.getString("board_content"));
borad.setLikeCount(rs.getInt("board_like_count"));
borad.setDisLikeCount(rs.getInt("board_disLike_count"));
borad.setCreatedDate(rs.getDate("board_created_date"));
boards.add(borad);
}
rs.close();
ptmt.close();
connection.close();
return boards;
}
@Override
public Board getDetailPrintByNo(int no) throws SQLException {
Board board = null;
String sql = "select board_no, board_title, board_writer, board_content, "
+"board_like_count, board_disLike_count, board_created_date, board_password "
+"from tb_sample_boards "
+"where board_no = ?"
+"order by board_no desc ";
Connection connection = getConnection();
PreparedStatement ptmt = connection.prepareStatement(sql);
ptmt.setInt(1, no);
ResultSet rs = ptmt.executeQuery();
if(rs.next()) {
board = new Board();
board.setNo(rs.getInt("board_no"));
board.setTitle(rs.getString("board_title"));
board.setWriter(rs.getString("board_writer"));
board.setContent(rs.getString("board_content"));
board.setLikeCount(rs.getInt("board_like_count"));
board.setDisLikeCount(rs.getInt("board_disLike_count"));
board.setCreatedDate(rs.getDate("board_created_date"));
board.setPassword(rs.getInt("board_password"));
}
rs.close();
ptmt.close();
connection.close();
return board;
}
@Override
public List<Board> getDetailPrintByTitle(String title) throws SQLException {
List<Board> boards = new ArrayList<>();
String sql = "select board_no, board_title, board_writer, board_content, "
+"board_like_count, board_disLike_count, board_created_date "
+"from tb_sample_boards "
+"where board_title = '%' || ? || '%' "
+"order by board_no desc ";
Connection connection = getConnection();
PreparedStatement ptmt = connection.prepareStatement(sql);
ptmt.setString(1, title);
ResultSet rs = ptmt.executeQuery();
if(rs.next()) {
Board borad = new Board();
borad.setNo(rs.getInt("board_no"));
borad.setTitle(rs.getString("board_title"));
borad.setWriter(rs.getString("board_writer"));
borad.setContent(rs.getString("board_content"));
borad.setLikeCount(rs.getInt("board_like_count"));
borad.setDisLikeCount(rs.getInt("board_disLike_count"));
borad.setCreatedDate(rs.getDate("board_created_date"));
boards.add(borad);
}
rs.close();
ptmt.close();
connection.close();
return boards;
}
@Override
public void insertBoard(String title, String writer, String content, int password) throws SQLException {
String sql = "insert into tb_sample_boards (board_no, board_title, board_writer, board_content, board_password) "
+ "values(board_seq.nextval,? ,? ,? ,? ) ";
Connection connection = getConnection();
PreparedStatement ptmt = connection.prepareStatement(sql);
ptmt.setString(1, title);
ptmt.setString(2, writer);
ptmt.setString(3, content);
ptmt.setInt(4,password);
ptmt.executeUpdate();
ptmt.close();
connection.close();
}
@Override
public void removeBoard(int no, int password) throws SQLException {
String sql = "delete from tb_sample_boards "
+ "where board_no = ? and board_password = ? ";
Connection connection = getConnection();
PreparedStatement ptmt = connection.prepareStatement(sql);
ptmt.setInt(1, no);
ptmt.setInt(2,password);
ptmt.executeUpdate();
ptmt.close();
connection.close();
}
@Override
public void updateBoard(Board board) throws SQLException {
String sql ="update tb_sample_boards "
+"set "
+"board_title = ?, "
+"board_writer = ?, "
+"board_content = ?, "
+"board_like_count = ?, "
+"board_disLike_count = ? "
+" where "
+ " board_no = ? ";
Connection connection = getConnection();
PreparedStatement ptmt = connection.prepareStatement(sql);
ptmt.setString(1,board.getTitle());
ptmt.setString(2, board.getWriter());
ptmt.setString(3, board.getContent());
ptmt.setInt(4, board.getLikeCount());
ptmt.setInt(5, board.getDisLikeCount());
ptmt.setInt(6, board.getNo());
ptmt.executeUpdate();
ptmt.close();
connection.close();
}
}
Service
package example_10_23_board.service;
import java.sql.SQLException;
import java.util.List;
import example_10_23_board.vo.Board;
public interface BoardService {
/**
* 게시글을 전체 조회하는 기능이다.
* @return 게시글 전체
* @throws SQLException DB Access 예외가 발생시 출력한다.
*/
List<Board> getAllPrint() throws SQLException;
/**
* 게시글을 제목으로 입력 받아서 상세 정보를 출력하고 좋아요, 싫어요를 입력하는 부분이다.
* @param title 게시물 제목
* @param likeCount 좋아요
* @param disLikeCount 싫어요
* @return 조회된 게시물
* @throws SQLExceptionDB Access 예외가 발생시 출력한다
*/
List<Board> getDetatilPrintByTitle(String title,int likeCount, int disLikeCount) throws SQLException;
/**
* 게시글을 번호로 입력받아서 상세 정보를 출력하고 좋아요, 싫어요를 입력하는 부분이다.
* @param no 게시물 번호
* @param likeCount 좋아요
* @param disLikeCount 싫어요
* @return 조회된 게시물
* @throws SQLException
*/
Board getDetatilPrintByNo(int no,int likeCount, int disLikeCount) throws SQLException;
/**
* 게시글을 추가하는 기능이다.
* @param title 게시물 제목
* @param writer 게시물 글쓴이
* @param content 게시물 내용
* @param password 게시물 비밀번호
* @throws SQLException DB Access 예외가 발생시 출력한다
*/
void addBord(String title, String writer, String content, int password) throws SQLException;
/**
* 게시물을 수정하는 기능이다.
* @param no 게시물 번호
* @param title 게시물 제목
* @param content 게시물 내용
* @param password 게시물 비밀번호
* @throws SQLException DB Access 예외가 발생시 출력한다
*/
void reviseBoard(int no, String title, String content, int password) throws SQLException;
/**
* 게시물을 지우는 기능이다.
* @param no 게시물 번호
* @param password 게시물 비밀번호
* @throws SQLException Access 예외가 발생시 출력한다
*/
void removeBoard(int no, int password) throws SQLException;
/**
* 게시물의 좋아요, 싫어요를 입력받고 수정하는 기능이다.
* @param likeCount 좋아요 갯수
* @param disLikeCount 싫어요 갯수
* @throws SQLException DB Access 예외가 발생시 출력한다
*/
void goodOrBadCount(int no, int likeCount, int disLikeCount) throws SQLException;
}
package example_10_23_board.service;
import java.sql.SQLException;
import java.util.List;
import example_10_23_board.dao.BoardDao;
import example_10_23_board.dao.BoardJdbcDao;
import example_10_23_board.exception.BoardException;
import example_10_23_board.vo.Board;
public class BoardServiceImpl implements BoardService {
private BoardDao boardDao = new BoardJdbcDao();
@Override
public List<Board> getAllPrint() throws SQLException {
List<Board> boards = boardDao.getAllPrint();
if(boards.isEmpty()) {
throw new BoardException("[### 오류] 게시물의 입력값이 존재하지 않습니다.");
} else {
return boards;
}
}
@Override
public void addBord(String title, String writer, String content, int password) throws SQLException {
boardDao.insertBoard(title, writer, content, password);
}
@Override
public void reviseBoard(int no, String title, String content, int password) throws SQLException {
Board findBoard = boardDao.getDetailPrintByNo(no);
System.out.println(findBoard.getNo());
System.out.println(password);
System.out.println(findBoard.getPassword());
if(findBoard.getPassword() == password) {
findBoard.setTitle(title);
findBoard.setContent(content);
boardDao.updateBoard(findBoard);
} else {
throw new BoardException("[오류]");
}
}
@Override
public void removeBoard(int no, int password) throws SQLException {
boardDao.removeBoard(no, password);
}
@Override
public List<Board> getDetatilPrintByTitle(String title,int likeCount, int disLikeCount) throws SQLException {
List<Board> boards = boardDao.getDetailPrintByTitle(title);
for(Board board : boards) {
goodOrBadCount(board.getNo(), likeCount, disLikeCount);
}
return boards;
}
@Override
public Board getDetatilPrintByNo(int no,int likeCount, int disLikeCount) throws SQLException {
Board board = boardDao.getDetailPrintByNo(no);
goodOrBadCount(no, likeCount, disLikeCount);
System.out.println(board.getPassword());
return board;
}
@Override
public void goodOrBadCount(int no, int likeCount, int disLikeCount) throws SQLException{
Board findBoard = boardDao.getDetailPrintByNo(no);
int goodCount= findBoard.getLikeCount() + likeCount;
int unlikeCount = findBoard.getDisLikeCount() + disLikeCount;
findBoard.setLikeCount(goodCount);
findBoard.setDisLikeCount(unlikeCount);
boardDao.updateBoard(findBoard);
if(unlikeCount >= 5) {
findBoard.setTitle("*************************");
boardDao.updateBoard(findBoard);
}
}
}
App
package example_10_23_board.app;
import static utils.KeyboardUtil.*;
import java.sql.SQLException;
import java.util.List;
import example_10_23_board.exception.BoardException;
import example_10_23_board.service.BoardService;
import example_10_23_board.service.BoardServiceImpl;
import example_10_23_board.vo.Board;
public class BoardApp {
private static BoardService boardService = new BoardServiceImpl();
public void menuDisplay() {
try{
System.out.println("----------------------------------------------------------");
System.out.println("1. 전체 조회 2.상세 조회 3. 등록하기 4. 수정하기 5. 삭제하기 0.종료");
System.out.println("----------------------------------------------------------");
System.out.println("메뉴 번호 입력 : ");
int menuNumber = readInt();
if(menuNumber == 1) {
getAllPrint();
} else if(menuNumber == 2) {
getDetailPrint();
} else if(menuNumber == 3) {
addBoard();
} else if(menuNumber == 4) {
reviseBoard();
} else if(menuNumber == 5) {
removeBoard();
} else if(menuNumber == 0) {
systemClose();
}
} catch (SQLException ex) {
System.out.println("[### 오류 ] 오류가 발생했습니다.");
ex.printStackTrace();
} catch (BoardException ex) {
System.out.println("[### 오류 ] 입력상의 오류가 발생했습니다.");
ex.printStackTrace();
}
menuDisplay();
}
public static void getAllPrint() throws SQLException {
System.out.println("[전체 게시글 조회 기능]");
System.out.println("전체 게시글을 조회합니다. ");
List<Board> boaders = boardService.getAllPrint();
BoardAllPrint(boaders);
System.out.println( );
}
private static void BoardAllPrint(List<Board> boaders) {
for(Board boader : boaders) {
printconentBoard(boader);
}
}
private static void printconentBoard(Board boader) {
System.out.print("게시물 번호 : " + boader.getNo());
System.out.print("게시물 제목 : " + boader.getTitle());
System.out.print("게시물 작성자 : " + boader.getWriter());
System.out.print("게시물 내용 : " + boader.getContent());
System.out.print("게시물 좋아요 개수 : " + boader.getLikeCount() +" 게시물 싫어요 : " + boader.getDisLikeCount());
System.out.println(" 게시물 입력일 : " + boader.getCreatedDate());
System.out.println(" ---------------------------------------- ");
}
public static void getDetailPrint() throws SQLException {
System.out.println("[상세정보 조회]");
// 좋아요/ 싫어요 기능도 구현
System.out.println("1. 게시물 번호로 검색 2. 게시물 제목으로 검색 ");
int searchNumber = readInt();
if(searchNumber == 1 ) {
System.out.println("[게시물 번호로 검색]");
System.out.println("[게시물 번호를 입력하세요]");
int boardNo = readInt();
System.out.println("[이 게시물의 평가를 원합니다.]");
System.out.println("[1. 좋아요 2. 싫어요 ]");
int likeUnLike = readInt();
if(likeUnLike == 1) {
int like = 1;
int unLike = 0;
Board board = boardService.getDetatilPrintByNo(boardNo,like,unLike);
printconentBoard(board);
System.out.println("좋아요가 1개가 추가 되었습니다. ");
} else if(likeUnLike == 2) {
int like = 0;
int unLike = 1;
Board board = boardService.getDetatilPrintByNo(boardNo,like,unLike);
printconentBoard(board);
System.out.println("싫어요가 1개가 추가 되었습니다. ");
}
} else if(searchNumber == 2 ) {
System.out.println("[게시물 제목로 검색]");
System.out.println("[게시물 제목를 입력하세요]");
String boardTitle = readString();
System.out.println("[이 게시물의 평가를 원합니다.]");
System.out.println("[1. 좋아요 2. 싫어요 ]");
int likeUnLike = readInt();
if(likeUnLike == 1) {
int like = 1;
int unLike = 0;
List<Board> boaders = boardService.getDetatilPrintByTitle(boardTitle, like, unLike);
BoardAllPrint(boaders);
} else if(likeUnLike == 2) {
int like = 0;
int unLike = 1;
List<Board> boaders = boardService.getDetatilPrintByTitle(boardTitle, like, unLike);
BoardAllPrint(boaders);
}
} else {
new BoardApp().menuDisplay();
}
}
public static void addBoard() throws SQLException {
System.out.println("[게시물 등록 기능]");
System.out.println("제목 입력 : ");
String title = readString();
System.out.println("작성자 이름 입력 : ");
String writer = readString();
System.out.println("내용 입력 : ");
String content = readString();
System.out.println("비밀 번호 입력 : ");
int password = readInt();
boardService.addBord(title, writer, content, password);
System.out.println("등록이 완료되었습니다.");
}
public static void reviseBoard() throws SQLException {
System.out.println("[게시물 수정 기능]");
System.out.println("게시물의 번호 입력 :");
int no = readInt();
System.out.println("게시물의 비밀번호 입력 :");
int password = readInt();
System.out.println("수정할 제목 입력 :");
String title = readString();
System.out.println("수정할 내용 입력 :");
String content = readString();
boardService.reviseBoard(no, title, content, password);
System.out.println("수정이 완료되었습니다. ");
}
public static void removeBoard() throws SQLException {
System.out.println("[게시물 삭제 기능]");
System.out.println("게시물의 번호 입력 : ");
int no = readInt();
System.out.println("비밀번호 입력 : ");
int password = readInt();
boardService.removeBoard(no, password);
System.out.println("삭제 완료되었습니다.");
}
public static void systemClose() throws SQLException {
System.out.println("[시스템 종료]");
System.exit(0);
}
public static void main(String[] args) {
new BoardApp().menuDisplay();
}
}