19.05.10 JSP 게시판 BoardInfo 게시글 보기 (46강)
Back-End/JSP 2019. 5. 10. 11:45전체 게시글 보기BoardList.jsp |
↓
글보기 BoardDAO- getOneBoard( ) |
↓
하나의 게시글 보기 BoardInfo.jsp |
↓
답글 쓰기 BoardReWriteForm.jsp |
글 수정하기 BoardUpdateForm.jsp |
글 삭제하기 BoardDeleteForm.jsp |
-노란색 표시한 부분까지 구현-
BoardWriteForm.jsp (게시글쓰기)
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 | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> <center> <h2> 게시글 쓰기 </h2> <form action="BoardWriteProc.jsp" method="post"> <table width = "600" border="1" bordercolor = "gray" bgcolor = "skyblue"> <!-- bordercolor는 선색깔 지정 --> <tr height = "40"> <td align = "center" width = "150"> 작성자 </td> <!-- ref는 데이터베이스에서 처리하기 때문에 따로 받지 않는다. --> <td width = "450"> <input type = "text" name = "writer" size = "60"></td> </tr> <tr height = "40"> <td align = "center" width = "150"> 제목 </td> <td width = "450"> <input type = "text" name = "subject" size = "60"></td> </tr> <tr height = "40"> <td align = "center" width = "150"> 이메일 </td> <td width = "450"> <input type = "text" name = "email" size = "60"></td> </tr> <tr height = "40"> <td align = "center" width = "150"> 비밀번호 </td> <td width = "450"> <input type = "password" name = "password" size = "61"></td> </tr> <tr height = "40"> <td align = "center" width = "150"> 글내용 </td> <td width = "450"><textarea rows = "10" cols = "50" name = "content"></textarea></td> </tr> <tr height = "40"> <td align = "center" colspan = "2"> <input type = "submit" value = "글쓰기"> <input type = "reset" value = "다시작성"> <button onclick = "location.href='BoardList.jsp'">전체 게시글 보기</button> <!-- 클릭하면 BoardList.jsp페이지로 넘어가는 버튼--> </td> </tr> </table> </form> </center> </body> </html> | cs |
BoardWriteProc.jsp(게시글 처리)
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 | <%@page import="model.BoardDAO"%> <%@page import="model.BoardBean"%> <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> <% request.setCharacterEncoding("EUC-KR"); //한글깨짐 방지 문자셋 설정 %> <!-- 빈파일에서 게시글 작성한 데이터를 한번에 읽어들임 --> <!-- use빈을 사용하여 자바빈을 사용, id는 빈클래스를 지칭하는 참조 변수명 --> <!-- 빈파일에 있는 값들을 받는다. --> <!-- 빈파일에 없는 값들은 null로 들어온다 null값으로 들어온 것들은 데이터베이스에서 들어올 값들이니 걱정x --> <jsp:useBean id="boardbean" class="model.BoardBean"> <jsp:setProperty name="boardbean" property="*" /> </jsp:useBean> <% //데이터베이스 쪽으로 빈클래스를 넘겨줌 BoardDAO bdao = new BoardDAO(); //데이터 저장 메소드를 호출 bdao.insertBoard(boardbean); //게시글 저장후 전체 게시글 보기 response.sendRedirect("BoardList.jsp"); %> </body> </html> | cs |
BoardList.jsp(게시글 리스트)
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 | <%@page import="java.util.Vector"%> <%@page import="model.BoardDAO"%> <%@page import="model.BoardBean"%> <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> <% //전체 게시글의 내용을 jsp쪽으로 가져와야함 //길이를 정확히 알 수 없기 때문에 가변길이인 벡터로 받는다. BoardDAO bdao = new BoardDAO(); //전체게시글을 리턴 받아주는 소스 (나중엔 개수가 많아지므로 다시 수정할 예정) Vector<BoardBean> vec = bdao.getAllBoard(); %> <center> <h2> 전체 게시글 보기 </h2> <table width = "700" border = "1" bgcolor = "skyblue"> <tr height = "40"> <td width = "50" align = "center"> 번호 </td> <td width = "320" align = "center"> 제목 </td> <td width = "100" align = "center"> 작성자 </td> <td width = "150" align = "center"> 작성일 </td> <td width = "80" align = "center"> 조회수 </td> </tr> <% for(int i = 0; i<vec.size(); i++) { BoardBean bean = vec.get(i); //벡터에 저장되어있는 bean클래스를 하나씩 추출 %> <tr height = "40"> <td width = "50" align = "center"> <%=i+1%> </td> <!-- 숫자가 하나씩 올라가게 하기위해서 i+1을 해준다 --> <!-- getNum()을 쓰면 순서가 복잡해지기 때문 --> <td width = "320" align = "center"> <a href = "BoardInfo.jsp?num=<%=bean.getNum()%>"> <%=bean.getSubject()%></a> </td> <td width = "320" align = "center"> <a href = "test.jsp?num=<%=bean.getNum()%>"> <%=bean.getSubject()%></a> </td> <!-- BoardInfo.jsp파일(정보를 확인하기 위한파일)에 Num값을 넘겨준다 --> <td width = "100" align = "center"> <%=bean.getWriter() %></td> <td width = "150" align = "center"> <%=bean.getReg_date() %></td> <td width = "80" align = "center"> <%=bean.getReadcount() %> </td> </tr> <%} %> <tr height = "40"> <td align = "center" colspan = "5"><!-- 버튼을 클릭하면 BoardWriteForm.jsp파일로 넘어간다.--> <input type = "button" value = "글쓰기" onclick="location.href='BoardWriteForm.jsp'"></td> </tr> </table> </center> </body> </html> | cs |
BoardInfo.jsp(한개의 글 정보)
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 | <%@page import="model.BoardBean"%> <%@page import="model.BoardDAO"%> <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> <% int num = Integer.parseInt(request.getParameter("num").trim()); //리스트 파일에서 넘겨준 num값을 받는다. num이 테이블의 기본키이기 때문에 num으로 정보를 확인할수 있기 때문 //공백제거후 정수형으로 바뀜 (정수형으로 바꾸면 숫자를 넣기가 쉽기 때문에) //데이터베이스 접근 BoardDAO bdao = new BoardDAO(); //boardbean 타입으로 하나의 게시글을 리턴 받는다. BoardBean bean = bdao.getOneBoard(num); %> <center> <h2> 게시글 보기 </h2> <!-- 빈클레스가 가지고있는 값들을 뿌려준다. --> <table width = "600" border = "1" bgcolor = "skyblue"> <tr height= "40"> <td align = "center" width = "120">글 번호 </td> <td align="center" width = "180"> <%=bean.getNum() %> <td align = "center" width = "120">조회수 </td> <td align="center" width = "180"> <%=bean.getReadcount() %></td> </tr> <tr height= "40"> <td align = "center" width = "120">작성자 </td> <td align="center" width = "180"> <%=bean.getWriter() %> <td align = "center" width = "120">작성일 </td> <td align="center" width = "180"> <%=bean.getReg_date() %></td> </tr> <tr height= "40"> <td align = "center" width = "120" colspan = "2">이메일 </td> <td align="center" width = "180" colspan = "2"> <%=bean.getEmail() %></td> </tr> <tr height= "40"> <td align = "center" width = "120">제목 </td> <td align="center" colspan = "3"> <%=bean.getSubject() %></td> </tr> <tr height= "80"> <td align = "center" width = "120">글 내용 </td> <td align="center" colspan = "3"> <%=bean.getContent() %></td> </tr> <tr height= "40"> <td align="center" colspan = "4"> <input type = "button" value = "답글쓰기" onclick = "location.href='BoardReWriteForm.jsp?num=<%=bean.getNum()%>&ref=<%=bean.getRef()%>&re_step=<%=bean.getRe_step()%>&re_Level<%=bean.getRe_level()%>'"> <!-- 부모글의 ref, revel, num값을 다 알고 있어야된다.(리플달기이기 때문) --> <!-- 위에처럼 num값만 넘겨서 할수도 있다. int num = Integer.parseInt(request.getParameter("num").trim()); BoardDAO bdao = new BoardDAO(); BoardBean bean = bdao.getOneBoard(num); --> <input type = "button" value = "수정하기" onclick = "location.href = 'BoardUpdateForm.jsp?num=<%=bean.getNum()%>'"> <input type = "button" value = "삭제하기" onclick = "location.href = 'BoardDeleteForm.jsp?num=<%=bean.getNum()%>'"> <input type = "button" value = "목록보기" onclick = "location.href = 'BoardList.jsp?num=<%=bean.getNum()%>'"> </table> </center> </body> </html> | cs |
BoardBean.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 | package model; public class BoardBean { //빈클래스 생성 private int num; private String email; private String subject; private String password; private String reg_date; private String writer; private int ref; private int re_step; private int re_level; private int readcount; private String content; public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getReg_date() { return reg_date; } public void setReg_date(String reg_date) { this.reg_date = reg_date; } public String getWriter() { return writer; } public void setWriter(String writer) { this.writer = writer; } public int getRef() { return ref; } public void setRef(int ref) { this.ref = ref; } public int getRe_step() { return re_step; } public void setRe_step(int re_step) { this.re_step = re_step; } public int getRe_level() { return re_level; } public void setRe_level(int re_level) { this.re_level = re_level; } public int getReadcount() { return readcount; } public void setReadcount(int readcount) { this.readcount = readcount; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } } | cs |
BoardDAO.java(DB연결)
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 | package model; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Vector; import javax.naming.InitialContext; import javax.sql.DataSource; import javax.naming.Context; public class BoardDAO { // 데이터베이스에 연결하기 위한 설정 Connection con; PreparedStatement pstmt; ResultSet rs; // 데이터베이스의 커넥션들을 사용하도록 설정하는 메소드 public void getCon() { try { // 데이터베이스에 접속할때는 예외처리를 해주어야한다. Context initctx = new InitialContext(); // 외부에서 데이터를 읽어들어야 하기 때문에 Context 객체 생성 // 톰캣 서버에 정보를 담아놓은 곳으로 이동함 Context envctx = (Context) initctx.lookup("java:comp/env"); // lookup메소드를 이용해서 자료를 읽어오는 코드 DataSource ds = (DataSource) envctx.lookup("jdbc/pool"); // datasource 객체를 선언 , 오브젝트 타입이기 때문에 타입변환을 한다. con = ds.getConnection(); // 데이터 소스를 기준으로 커넥션을 연결함 } catch (Exception e) { e.printStackTrace(); // 어느부분이 잘못되었는지 알려주는 예외처리 } } // 하나의 새로운 게시글이 넘어와서 저장되는 메소드 public void insertBoard(BoardBean bean) { getCon(); // 빈클래스에 넘어오지 않았던 데이터들을 초기화 해주어야 한다. int ref = 0; // 글그룹을 의미한다, 쿼리를 실행시켜서 가장 큰 ref값을 가져온후 +1을 더해주면 된다. int re_step = 1; // 새글이고, 부모글은 글 스텝(Re_step), 글 레벨(Re_level)은 1이기 때문에 초기값을 1을 준다. int re_level = 1; // 새글이고, 부모글은 글 스텝(Re_step), 글 레벨(Re_level)은 1이기 때문에 초기값을 1을 준다. try { // 가장큰 ref값을 읽어오는 쿼리 준비함 (새로운 글은 ref가 가장크기 때문) String refsql = "select max(ref) from board"; // board테이블로부터 가장큰 ref를 검색 // 쿼리를 실행할 객체 pstmt = con.prepareStatement(refsql); // 쿼리 실행후 결과를 리턴 rs = pstmt.executeQuery(); if (rs.next()) { // 쿼리(rs)가 결과값이 있다면 실행하는 구문 ref = rs.getInt(1) + 1; // 최대값에 1을 더해서 글그룹을 설정. 새로운글은 ref숫자가 하나씩 올라가야 하기 때문 } // 실제로 게시글 전체값을 테이블에 저장 String sql = "insert into board values(board_seq.NEXTVAL,?,?,?,?,sysdate,?,?,?,0,?)"; // ?가 아니라 시퀀스를 사용해 // 시퀀스에 이미 들어가있는값의 // 다음값을 자동으로 매핑해서 // 넣어준다. pstmt = con.prepareStatement(sql); // ?에 값을 매핑한다. //0은 카운트값이기때문 0을 넣어주었다 pstmt.setString(1, bean.getWriter()); pstmt.setString(2, bean.getEmail()); pstmt.setString(3, bean.getSubject()); pstmt.setString(4, bean.getPassword()); pstmt.setInt(5, ref); pstmt.setInt(6, re_step); pstmt.setInt(7, re_level); pstmt.setString(8, bean.getContent()); // 쿼리를 실행하시오. pstmt.executeUpdate(); // 자원 반납 con.close(); } catch (Exception e) { e.printStackTrace(); } } //모든 게시글을 리턴해주는 메소드 작성 public Vector<BoardBean> getAllBoard() { //리턴할 객체 선언 Vector<BoardBean> v = new Vector<>(); getCon(); try { //쿼리 준비 String sql = "select * from board order by ref desc, re_step asc "; //게시판 글을 내림차순으로 정렬하기 위한 sql코드 //쿼리를 실행할 객체 선언 pstmt = con.prepareStatement(sql); //쿼리 실행후 결과 저장 rs = pstmt.executeQuery(); //데이터 개수가 몇개인지 모르기에 반복문을 이용하여 데이터를 추출 while(rs.next()) { //데이터를 패키징 시키기(가방 = Boardbean 클래스를 이용) 패키지 안에 넣는다는 의미 BoardBean bean = new BoardBean(); bean.setNum(rs.getInt(1)); bean.setWriter(rs.getString(2)); bean.setEmail(rs.getString(3)); bean.setSubject(rs.getString(4)); bean.setPassword(rs.getString(5)); bean.setReg_date(rs.getDate(6).toString()); //reg데이터가 String타입으로 변한다. bean.setRef(rs.getInt(7)); bean.setRe_step(rs.getInt(8)); bean.setRe_level(rs.getInt(9)); bean.setReadcount(rs.getInt(10)); bean.setContent(rs.getString(11)); //패키징한 데이터를 벡터에 저장 v.add(bean); } con.close(); } catch (Exception e) { e.printStackTrace(); } return v; } //하나의 게시글을 리턴하는 메소드 public BoardBean getOneBoard(int num) { //리턴타입 선언(객체 생성) BoardBean bean = new BoardBean(); getCon(); try { //조회수 증가 쿼리 String readsql = "update board set readcount = readcount+1 where num=?"; //읽은 게시글의 조회수를 늘리기 위한 sql문 pstmt = con.prepareStatement(readsql); pstmt.setInt(1, num); pstmt.execute(); //쿼리 준비 String sql = "select * from board where num=?"; //쿼리 실행 객체 pstmt = con.prepareStatement(sql); pstmt.setInt(1,num); //쿼리 실행후 결과를 리턴 rs = pstmt.executeQuery(); if(rs.next()) { bean.setNum(rs.getInt(1)); bean.setWriter(rs.getString(2)); bean.setEmail(rs.getString(3)); bean.setSubject(rs.getString(4)); bean.setPassword(rs.getString(5)); bean.setReg_date(rs.getDate(6).toString()); //reg데이터가 String타입으로 변한다. bean.setRef(rs.getInt(7)); bean.setRe_step(rs.getInt(8)); bean.setRe_level(rs.getInt(9)); bean.setReadcount(rs.getInt(10)); bean.setContent(rs.getString(11)); } con.close(); } catch (Exception e) { e.printStackTrace(); } return bean; } } | cs |
'Back-End > JSP' 카테고리의 다른 글
19.05.10 JSP 게시판 만들기-답글 리스트 구현 (48강) (0) | 2019.05.10 |
---|---|
19.05.10 JSP 게시판 만들기-답글쓰기 구현 (47강) (0) | 2019.05.10 |
19.05.09 계층형 게시판 (게시물 쓰기 구현)-책 (0) | 2019.05.09 |
19.05.09 JSP 게시판 - 전체 게시글 보기 구현 (~동영상 45강) (0) | 2019.05.09 |
19.05.08 JSP 게시판 - 시스템 구조 및 게시판 만들기 (~동영상 43강) (0) | 2019.05.08 |