※ 프로젝트 전체소스파일(model1-board.war) 첨부
project 환경
java : 1.8
tomcat : 9.0
eclipse : neon
mysql : 5.5
board 테이블 구조
프로젝트 구조 (이클립스 다이나믹 웹 프로젝트)
※ mysql -connector-java-5.1.40-bin.jar 파일은 http://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.40 나 http://dev.mysql.com/downloads/connector/j 둘중 하나에서 다운로드 받으면 됩니다.
Board.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package service; public class Board { private int boardNo; private String boardPw; private String boardTitle; private String boardContent; private String boardUser; private String boardDate; public int getBoardNo() { return boardNo; } public void setBoardNo(int boardNo) { this.boardNo = boardNo; } public String getBoardPw() { return boardPw; } public void setBoardPw(String boardPw) { this.boardPw = boardPw; } public String getBoardTitle() { return boardTitle; } public void setBoardTitle(String boardTitle) { this.boardTitle = boardTitle; } public String getBoardContent() { return boardContent; } public void setBoardContent(String boardContent) { this.boardContent = boardContent; } public String getBoardUser() { return boardUser; } public void setBoardUser(String boardUser) { this.boardUser = boardUser; } public String getBoardDate() { return boardDate; } public void setBoardDate(String boardDate) { this.boardDate = boardDate; } } | cs |
BoardDao.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | package service; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class BoardDao { private final String driverClassName = "com.mysql.jdbc.Driver"; private final String url = "jdbc:mysql://127.0.0.1:3306/injava?useUnicode=true&characterEncoding=euckr"; private final String username = "root"; private final String password = "java0000"; // 테이블 : board , 기능 : 데이터 수정 public int updateBoard(Board board) { int rowCount = 0; Connection connection = null; PreparedStatement statement = null; String sql = "UPDATE board SET board_title=?, board_content=? WHERE board_no=? AND board_pw=?"; try { connection = this.getConnection(); statement = connection.prepareStatement(sql); statement.setString(1,board.getBoardTitle()); statement.setString(2,board.getBoardContent()); statement.setInt(3,board.getBoardNo()); statement.setString(4,board.getBoardPw()); rowCount = statement.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { this.close(connection, statement, null); } return rowCount; } // 테이블 : board , 기능 : 데이터 삭제 public int deleteBoard(Board board) { int rowCount = 0; Connection connection = null; PreparedStatement statement = null; String sql = "DELETE FROM board WHERE board_no=? AND board_pw=?"; try { connection = this.getConnection(); statement = connection.prepareStatement(sql); statement.setInt(1,board.getBoardNo()); statement.setString(2,board.getBoardPw()); rowCount = statement.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { this.close(connection, statement, null); } return rowCount; } // 테이블 : board , 기능 : 하나의 데이터 가져오기 public Board selectBoardByKey(int boardNo) { Board board = null; Connection connection = null; PreparedStatement statement = null; ResultSet resultset = null; String sql = "SELECT board_title, board_content, board_user, board_date FROM board WHERE board_no=?"; try { connection = this.getConnection(); statement = connection.prepareStatement(sql); statement.setInt(1, boardNo); resultset = statement.executeQuery(); if(resultset.next()) { board = new Board(); board.setBoardNo(boardNo); board.setBoardTitle(resultset.getString("board_title")); board.setBoardContent(resultset.getString("board_content")); board.setBoardUser(resultset.getString("board_user")); board.setBoardDate(resultset.getString("board_date")); } } catch (Exception e) { e.printStackTrace(); } finally { this.close(connection, statement, resultset); } return board; } // 테이블 : board , 기능 : 한 페이지의 데이터 가져오기 public List<Board> selectBoardListPerPage(int beginRow, int pagePerRow) { List<Board> list = new ArrayList<Board>(); Connection connection = null; PreparedStatement statement = null; ResultSet resultset = null; String sql = "SELECT board_no, board_title, board_user, board_date FROM board ORDER BY board_date DESC LIMIT ?, ?"; try { connection = this.getConnection(); statement = connection.prepareStatement(sql); statement.setInt(1, beginRow); statement.setInt(2, pagePerRow); resultset = statement.executeQuery(); while(resultset.next()) { Board board = new Board(); board.setBoardNo(resultset.getInt("board_no")); board.setBoardTitle(resultset.getString("board_title")); board.setBoardUser(resultset.getString("board_user")); board.setBoardDate(resultset.getString("board_date")); list.add(board); } } catch (Exception e) { e.printStackTrace(); } finally { this.close(connection, statement, resultset); } return list; } // 테이블 : board , 기능 : 전체 로우 카운터 가져오기 public int selectTotalBoardCount() { int rowCount = 0; Connection connection = null; PreparedStatement statement = null; ResultSet resultset = null; String sql = "SELECT COUNT(*) FROM board"; try { connection = this.getConnection(); statement = connection.prepareStatement(sql); resultset = statement.executeQuery(); if(resultset.next()) { rowCount = resultset.getInt(1); } } catch (Exception e) { e.printStackTrace(); } finally { this.close(connection, statement, resultset); } return rowCount; } // 테이블 : board , 기능 : 데이터 입력하기 public int insertBoard(Board board) { int rowCount = 0; Connection connection = null; PreparedStatement statement = null; String sql = "INSERT INTO board(board_pw, board_title, board_content, board_user, board_date) values(?,?,?,?,now())"; try { connection = this.getConnection(); statement = connection.prepareStatement(sql); statement.setString(1,board.getBoardPw()); statement.setString(2,board.getBoardTitle()); statement.setString(3,board.getBoardContent()); statement.setString(4,board.getBoardUser()); rowCount = statement.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { this.close(connection, statement, null); } return rowCount; } private Connection getConnection() { Connection connection = null; try { Class.forName(this.driverClassName); connection = DriverManager.getConnection(this.url, this.username, this.password); } catch(Exception e) { e.printStackTrace(); } return connection; } private void close(Connection connection, Statement statement, ResultSet resultset) { if(resultset != null) { try { resultset.close(); } catch (SQLException e) { e.printStackTrace(); } } if(statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if(connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } | cs |
'P. 자바 미니 프로젝트 > 모델1 게시판' 카테고리의 다른 글
P.2.6 모델1 게시판 만들기6 - 글수정 (2) | 2016.11.30 |
---|---|
P.2.5 모델1 게시판 만들기5 - 글삭제 (0) | 2016.11.30 |
P.2.4 모델1 게시판 만들기4 - 글내용 (0) | 2016.11.30 |
P.2.3 모델1 게시판 만들기3 - 글목록 (2) | 2016.11.30 |
P.2.2 모델1 게시판 만들기2 - 글입력 (1) | 2016.11.30 |