본문 바로가기

학원/JSP - 학원

게시판 2단계-1 (댓글, 대댓글)

728x90

board.js
0.00MB
delete.jsp
0.00MB
AnswerLine.gif
0.00MB
delete_ok.jsp
0.00MB
edit.jsp
0.00MB
edit_ok.jsp
0.00MB
list.jsp
0.00MB
show.jsp
0.00MB
write.jsp
0.00MB
write_ok.jsp
0.00MB

BoardDBBean.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package magic.board;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
 
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
 
import magic.member.MemberBean;
 
public class BoardDBBean {
    
private static BoardDBBean instance = new BoardDBBean();
    
    public static BoardDBBean getInstance() {
        return instance;
    }
    
    public Connection getConnection() throws Exception {
        Context ctx = new InitialContext();
        DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/oracle");
        return ds.getConnection();
    }
    public int insertBoard(BoardBean board) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql="";
        int re = -1;
        int number;
        int id = board.getB_id();
        int ref = board.getB_ref();
        int step = board.getB_step();
        int level = board.getB_level();
        try {
            con = getConnection();
            sql = "select max(b_id) from boardT";
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            
            if(rs.next()) {
                number = rs.getInt(1)+1;
            }else {
                number=1;
            }
            
            if (board.getB_id() != 0) {
                sql="update boardt set b_step=b_step+1 where b_ref=? and b_step>?";
                ps = con.prepareStatement(sql);
                ps.setInt(1, ref);                
                ps.setInt(2, step);
                ps.executeUpdate();
                step+=1;
                level+=1;
            } else {
                ref=number;
                step=0;
                level=0;
            }
    
            sql = "insert into boardT (b_id,b_name,b_email,b_title,b_content,b_date,b_pwd,b_ip,b_ref, b_step, b_level) "
                    + "values(?,?,?,?,?,?,?,?,?,?,?)";
            ps = con.prepareStatement(sql);
            ps.setInt(1, number);
            ps.setString(2, board.getB_name());
            ps.setString(3, board.getB_email());
            ps.setString(4, board.getB_title());
            ps.setString(5, board.getB_content());
            ps.setTimestamp(6, board.getB_date());
            ps.setString(7, board.getB_pwd());
            ps.setString(8, board.getB_ip());
            ps.setInt(9, ref);
            ps.setInt(10, step);
            ps.setInt(11, level);
            ps.executeUpdate();
            re = 1;
            ps.close();
            con.close();
            System.out.println("추가 성공");
        } catch (Exception e) {
            System.out.println("추가 실패");
            e.printStackTrace();
        }finally {
            try {
                if (ps != null) ps.close();
                if (con != null) con.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return re;
    }
    public ArrayList<BoardBean> listBoard(){
        ArrayList<BoardBean> boardList = new ArrayList<BoardBean>();
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;;
        String sql = "select * from boardt order by b_ref desc, b_step asc";
        
        try {
            con = getConnection();
            st = con.createStatement();
            rs = st.executeQuery(sql);
            
            while (rs.next()) {
                BoardBean board = new BoardBean();        
                board.setB_id(rs.getInt("b_id"));
                board.setB_name(rs.getString("b_name"));
                board.setB_email(rs.getString("b_email"));
                board.setB_title(rs.getString("b_title"));
                board.setB_content(rs.getString("b_content"));
                board.setB_date(rs.getTimestamp("b_date"));
                board.setB_hit(rs.getInt("b_hit"));
                board.setB_pwd(rs.getString("b_pwd"));
                board.setB_ip(rs.getString("b_ip"));
                board.setB_ref(rs.getInt("b_ref"));
                board.setB_step(rs.getInt("b_step"));
                board.setB_level(rs.getInt("b_level"));
                
                boardList.add(board);
            }
            rs.close();
            st.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs != null) rs.close();
                if(st != null) st.close();
                if(con != null) con.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return boardList;
    }
    
    public BoardBean getBoard(int id, boolean bo) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        BoardBean board = new BoardBean();
        
        String sql = "";
        
        try {
            con = getConnection();
            if(bo==true) {
            sql = "update boardt set b_hit=b_hit+1 where b_id=?";
            ps = con.prepareStatement(sql);
            ps.setInt(1, id);            
            ps.executeUpdate();
            
            sql = "select * from boardT where b_id=?";
            ps = con.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            }else {
            sql = "select * from boardT where b_id=?";
            ps = con.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            }
            if (rs.next()) {
                board.setB_id(rs.getInt("b_id"));
                board.setB_name(rs.getString("b_name"));
                board.setB_email(rs.getString("b_email"));
                board.setB_title(rs.getString("b_title"));
                board.setB_content(rs.getString("b_content"));
                board.setB_date(rs.getTimestamp("b_date"));
                board.setB_hit(rs.getInt("b_hit"));
                board.setB_pwd(rs.getString("b_pwd"));
                board.setB_ip(rs.getString("b_ip"));
                board.setB_ref(rs.getInt("b_ref"));
                board.setB_step(rs.getInt("b_step"));
                board.setB_level(rs.getInt("b_level"));
                
            }
            rs.close();
            ps.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return board;
    }
    
    public int deleteBoard(int id, String b_pwd) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int re=-1;
        String pwd="";
        String sql="";
        
        try {
            con = getConnection();
            sql = "select b_pwd from boardT where b_id=?";
            ps = con.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            
            if (rs.next()) {
                pwd = rs.getString(1);
                
                if (!pwd.equals(b_pwd)) {
                    re=0;
                } else {
                    sql="delete from boardT where b_id=?";
                    ps = con.prepareStatement(sql);
                    ps.setInt(1, id);
                    ps.executeUpdate();
                    re=1;
                }
            }
            rs.close();
            ps.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return re;    
    }
    
    public int editBoard(BoardBean board) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String pwd="";
        String sql="";
        int re = -1;
        
        try {
            con = getConnection();
            sql = "select b_pwd from boardT where b_id=?";
            ps = con.prepareStatement(sql);
            ps.setInt(1, board.getB_id());
            rs = ps.executeQuery();
            
            if(rs.next()) {
                pwd = rs.getString(1);
                if (!pwd.equals(board.getB_pwd())) {
                    re=0;
                } else {
                    sql = "update boardT set b_name=?, b_email=?, b_title=?, b_content=? where b_id=?";
                    ps = con.prepareStatement(sql);
                    ps.setString(1, board.getB_name());
                    ps.setString(2, board.getB_email());
                    ps.setString(3, board.getB_title());
                    ps.setString(4, board.getB_content());
                    ps.setInt(5, board.getB_id());
                    ps.executeUpdate();
                    re=1;
                }
            }
            rs.close();
            ps.close();
            con.close();            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return re;
    }
}
 
cs

 

write.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
70
71
72
73
74
75
76
77
78
79
80
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<jsp:useBean id="board" class="magic.board.BoardBean"></jsp:useBean>
<jsp:setProperty property="*" name="board"/>
<% 
    int b_id = 0, b_ref=1, b_step=0, b_level=0;
    String b_title="";
    if(request.getParameter("b_id"!= null){
        b_id = Integer.parseInt(request.getParameter("b_id"));        
    }
    
    BoardDBBean db = BoardDBBean.getInstance();
    board = db.getBoard(b_id, false);
    
    if(board != null){
        b_title = board.getB_title();
        b_ref = board.getB_ref();
        b_step = board.getB_step();
        b_level = board.getB_level();
    }
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
<script type="text/javascript" src="board.js" charset="utf-8"></script>
<style type="text/css">
</style>
</head>
<body>
    <h1 align="center">글 올 리 기</h1>
    <center>
    <table>
    <form action="write_ok.jsp" method="post" name="reg_form">
        <input type="hidden" name="b_id" value="<%= b_id%>">
        <input type="hidden" name="b_ref" value="<%= b_ref%>">
        <input type="hidden" name="b_step" value="<%= b_step%>">
        <input type="hidden" name="b_level" value="<%= b_level%>">
    <tr>    
        <td>
        작성자<input type="text" size="20" name="b_name">
        이메일<input type="text" size="20" name="b_email">
        </td>
    </tr>
    <tr>
        <td>글제목
            <%
                if(b_id==0){
            %>
                    <input type="text" size="50" name="b_title"></td>
                    <!-- 원래 글쓰기  -->
            <%
                }else{
            %>
                    <input type="text" size="50" name="b_title" value="[답변]:<%= b_title%>"></td>
                    <!-- 답들이면 답변을 붙이면서 글쓰기 됨  -->
            <%
                }
            %>
    </tr>
    <tr>
        <td><textarea rows="10" cols="70" name="b_content"></textarea></td>
    </tr>
    <tr>
        <td>암호<input type="password" size="30" name="b_pwd"></td>
    </tr>
    <tr>
        <td>
            <input type="button" value="글쓰기" onclick="check_ok()">
            <input type="reset" value="다시작성">
            <input type="button" value="글목록" onclick="location = 'list.jsp'">
        </td>
    </tr>
    </form>
    </table>
    </center>
</body>
</html>
cs

- 답변글로 달 때 필요한 컬럼들인 아이디, ref, step, level에게 초깃값을 부여해서 설정

- show에서 답변글 버튼을 누르면 b_id값을 넘기도록 해둔다.

- 넘어오는 값이 null이 아닌 경우 b_id에 show에서 넘겨준 값을 리퀘스트로 받아 저장

- getBoard메소드를 호출해서 매개변수에 b_id를 넣고, 조회수가 올라가지 않도록 false값을 넣어준다.

- 위의 값을 빈 객체에 넣어주고, 이 빈 객체가 null값이 아닐 경우 위에서 초기값을 설정한 컬럼들에 빈에서 가져온 값을 넣어준다.

- 위의 4개의 값들을 히든 타입의 인풋에 넣어서 저장

- 그리고 처음 글을 쓰는 거면 b_id의 값이 0이기 때문에 [답변]을 붙이지 않도록하고, 그게 아니라면 답글을 다는 것이기 때문에 [답변]이 붙어서 출력되도록 if-else문을 사용

list.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="java.util.ArrayList"%>
<%@page import="magic.member.MemberBean"%>
<%@page import="magic.board.BoardDBBean"%>
<%@page import="magic.board.BoardBean"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    BoardDBBean bd =  BoardDBBean.getInstance();
    ArrayList<BoardBean> boardList = bd.listBoard();
    int b_id, b_hit, b_level = 0;
    String b_name, b_email, b_title, b_content;
    Timestamp b_date;
    request.setCharacterEncoding("utf-8");
%>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
    <center>
        <h1>게시판에 등록된 글 목록 보기</h1>
        <table width="600">
            <tr>
                <td align="right">
                    <a href="write.jsp">글쓰기</a>
                </td>
            </tr>
        </table>
    </center>
    <center>
        <table border="1" width="800" cellspacing="0">
            <tr height="25">
                <td width="40" align="center">번호</td>
                <td width="350" align="center">글제목</td>
                <td width="120" align="center">작성자</td>
                <td width="120" align="center">작성일</td>
                <td width="50" align="center">조회수</td>
            </tr>
            <% 
                for(int i=0;i<boardList.size();i++){
                    BoardBean board = boardList.get(i);
                    b_id = board.getB_id();
                    b_name = board.getB_name();
                    b_email = board.getB_email();
                    b_title = board.getB_title();
                    b_content = board.getB_content();
                    b_date = board.getB_date();
                    b_hit = board.getB_hit();
                    b_level = board.getB_level();
            %>
            <tr height="25" bgcolor="#f7f7f7"
                onmouseover="this.style.backgroundColor='#eeeeef'"
                onmouseout="this.style.backgroundColor='#f7f7f7'">
                <td align="center"><%= b_id%></td>
                <td>
            <%
                if(b_level>0){
                    for(int j=0;j<b_level;j++){
            %>
                        &nbsp;
            <%
                    }
            %>
                <img src="../images/AnswerLine.gif" width="16" height="16">
            <%
                }
            %>
                <a href="show.jsp?b_id=<%= b_id%>"><%= b_title %></a>
                </td>
                <td align="center">
                    <a href="mailto:<%=b_email %>">
                        <%= b_name %>
                    </a>
                </td>
                <td align="center"><%=sdf.format(b_date) %></td>
                <td align="center"><%= b_hit %></td>
            </tr>
            <%
                }
            %>
        </table>
    </center>
</body>
</html>
cs

 

 

- 댓글, 대댓글은 level을 기준으로 입력되는 것

- 그래서 리스트에서 가져오는 값은 level만 가져오고, 이를 반복문을 통해서 댓글과 대댓글을 구분하여 들여쓰기

- 이미지는 반복문 들여쓰기 이후에 출력되도록 for문 밖에 위치

 

게시판_2단계.pptx
0.67MB

 

 

728x90

'학원 > JSP - 학원' 카테고리의 다른 글

파일 업로드, 내용 보기  (0) 2022.04.04
게시판 3단계  (0) 2022.04.04
게시판 1단계 10  (0) 2022.03.30
게시판 1단계 9  (0) 2022.03.30
게시판 1 단계 8  (0) 2022.03.30