본문 바로가기

Side Project

#07. [Side Project 1] JSP 로그인, 게시판 만들기 (7) :: 글 수정

 

 

 

■ bbsupdate.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
<%@page import="dto.MemberDto"%>
<%@page import="dto.BbsDto"%>
<%@page import="dao.BbsDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>bbsupdate</title>
<style type="text/css">
.center{
    margin: auto;
    width: 60%;
    border: 3px solid #8AC007;
    padding: 10px;
}
input {
    size: 50;
}
</style>
</head>
<body>
 
<a href="logout.jsp">로그아웃</a>
 
<h1>글 수정</h1>
 
<%
String sseq = request.getParameter("seq");
int seq = Integer.parseInt(sseq.trim());
 
BbsDao dao = BbsDao.getInstance();
BbsDto bbs = dao.getBbs(seq);
%>
 
<%
Object ologin = session.getAttribute("login");
MemberDto mem = null;
mem = (MemberDto)ologin;
%>
 
<div class="center">
 
<form action="bbsupdateAf.jsp" method="post">
<input type="hidden" name="seq" value="<%=seq %>">
            
<table border="1">
<col width="200"><col width="500"> 
 
<tr>
    <td>아이디</td>
    <td>
        <input type="text" name="id" readonly="readonly" size="50" 
            value="<%=mem.getId() %>">         
    </td>    
</tr>
<tr>
    <td>제목</td>
    <td>
        <input type="text" name="title" size="50" value="<%=bbs.getTitle() %>">        
    </td>
</tr>
<tr>
    <td>내용</td>
    <td>
        <textarea rows="10" cols="50" name="content"><%=bbs.getContent() %></textarea>        
    </td>
</tr>
<tr>
    <td colspan="2">
        <input type="submit" value="글수정">
    </td>
</tr>
 
</table>
 
</form>
 
</div>
 
<a href="bbslist_css.jsp">글목록</a>
 
</body>
</html>
cs

 

 

■ bbsupdateAf.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
<%@page import="dao.BbsDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 
<%
request.setCharacterEncoding("utf-8");
%>    
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>bbsupdateAf.jsp</title>
</head>
<body>
 
<%
String sseq = request.getParameter("seq");
int seq = Integer.parseInt(sseq.trim());
 
String title = request.getParameter("title");
String content = request.getParameter("content");
 
BbsDao dao = BbsDao.getInstance();
boolean isS = dao.updateBbs(seq, title, content);
 
if(isS == true){
    %>
    <script type="text/javascript">
    alert("글 수정 성공");
    location.href = "bbslist.jsp";
    </script>    
    <%
}else{    
    %>
    <script type="text/javascript">
    alert("글 수정 실패");
    location.href = "bbslist.jsp";
    </script>
    <%
}    
%>
 
</body>
</html>
 
 
 
 
 
 
cs

 

■ BbsDao.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
public boolean updateBbs(int seq, String title, String content) {
        String sql = " UPDATE BBS SET "
                + " TITLE=?, CONTENT=? "
                + " WHERE SEQ=? ";
        
        Connection conn = null;
        PreparedStatement psmt = null;
        int count = 0;
        
        try {
            conn = DBConnection.getConnection();
            System.out.println("1/6 S updateBbs");
            
            psmt = conn.prepareStatement(sql);
            psmt.setString(1, title);
            psmt.setString(2, content);
            psmt.setInt(3, seq);
            
            System.out.println("2/6 S updateBbs");
            
            count = psmt.executeUpdate();
            System.out.println("3/6 S updateBbs");
            
        } catch (Exception e) {            
            e.printStackTrace();
        } finally{
            DBClose.close(psmt, conn, null);
            System.out.println("5/6 S updateBbs");
        }        
        
        return count>0?true:false;
    }
    
    // 글의 모든 갯수
    /*
    public int getAllBbs() {
        String sql = " SELECT COUNT(*) FROM BBS ";
        
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        
        int len = 0;
        
        try {
            conn = DBConnection.getConnection();
            psmt = conn.prepareStatement(sql);
            rs = psmt.executeQuery();
            if(rs.next()) {
                len = rs.getInt(1);
            }            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            DBClose.close(psmt, conn, rs);
        }
        return len;
    }
    */
cs