programming/jsp

JSP-자유게시판

히연쓰 2021. 5. 4. 17:33
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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@100;300;400;500;700;900&display=swap" rel="stylesheet">
<link rel="stylesheet" href="css/board_css.css">
<script src="js/jquery-3.6.0.min.js"></script>
<script src="js/join.js" defer></script>
</head>
<body>
    <div class="formBox">
        <h2>자유게시판</h2>
        <form action="board_write_action.jsp" id="joinForm" method="POST">
            <table>
                <colgroup>
                    <col style="width:150px">
                    <col style="width:850px">
                </colgroup>
                <tbody>
                    <tr>
                        <th>ID</th>
                        <td><input type="text" name="id" id="userID"></td>
                    </tr>
                    <tr>
                        <th>NAME</th>
                        <td><input type="text" name="name" id="userName" value=""></td>
                    </tr>
                    <tr>
                        <th>SUBJECT</th>
                        <td><input type="text" name="subject" id="subject" value=""></td>
                    </tr>
                    <tr>
                        <th>PASSWORD</th>
                        <td><input type="password" name="password" id="userPassword"></td>
                    </tr>
                    <tr>
                        <th>CONTENTS</th>
                        <td>
                            <textarea name="contents" id="contents"></textarea>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div class="btns">
                <input type="button" value="확인" id="join">
                <input type="reset" value="취소" id="cancel">
            </div>
        </form>
    </div>
    
</body>
</html>
 
cs

-> board_write.jsp

 

board_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
<%@ page import="com.heeyeon.model.BoardDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 
    <% 
        request.setCharacterEncoding("UTF-8");
    %>
    <jsp:useBean id="boardBean" class="com.heeyeon.model.BoardBean">
        <jsp:setProperty name="boardBean" property="*" />
    </jsp:useBean>
    <%
        BoardDao boardDao = new BoardDao();    
        int result = boardDao.insertBoard(boardBean);
        if(result>0) {
    %>
        <script>
            alert("글이 등록되었습니다");
            location.href="board_list.jsp";
        </script>
    <%
        } else {
    %>
        <script>
            alert("등록 실패");
            history.back();
        </script>
    <%
            
        }
    %>
</body>
</html>
 
cs

-> board_write_action.jsp

 

 board_write_action.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
89
90
91
92
93
94
<%@page import="com.heeyeon.model.BoardBean"%>
<%@page import="com.heeyeon.model.BoardDao"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@100;300;400;500;700;900&display=swap" rel="stylesheet">
<link rel="stylesheet" href="css/board_css.css">
</head>
<body>
    <!--     전체 게시물의 갯수를 알아야 한다.   34 -->
    <!--     한번에 보여줄 게시물의 갯수도 알야 한다.  10 -->
    
    <%
        //한 페이지에 보여질 게시글 수! 
        int pagePerCount = 10;
        //request로 넘어오는 페이지 번호  클릭한 페이지 번호 
        String clickedPage = request.getParameter("clickedPage");
        //값이 없을때 1로 세팅
        if(clickedPage == null) {
            clickedPage ="1";
        }
        //전체 게시글 
        int total = 0;
        //테이블의 no에 뿌려질 번호 원래는 getNo를 뿌리고 있는데 
        //중간에 삭제되거나 순서가 바뀌면 엉망이 되어서 보기 싫음
        int numbering = 0;
        int currentPage = Integer.parseInt(clickedPage);
        BoardDao boardDao = new BoardDao();
        //전체 게시글수를 boardDao의 getTotal메서드를 통해서 얻어옴
        total = boardDao.getTotal();
        int start = (currentPage - 1)*pagePerCount + 1;
        int end = currentPage*pagePerCount;
        
        ArrayList<BoardBean> boardList = boardDao.showAllBoard(start,end);
        numbering = total - (currentPage - 1)*pagePerCount;
        
    %>
    <div class="formBox" id="list">
        <h2>자유게시판</h2>
        <form action="" method="POST">
            <table>
                <colgroup>
                    <col style="width:50px">
                    <col style="width:150px">
                    <col style="width:150px">
                    <col style="width:950px">
                </colgroup>
                <thead>
                    <tr>
                        <th>NO</th>
                        <th>ID</th>
                        <th>NAME</th>
                        <th>SUBJECT</th>
                    </tr>
                </thead>
                <tbody>
                        <% 
                        for(int i=0;i<boardList.size();i++){
                            BoardBean boardBean = boardList.get(i);
                        %>
                                <tr>
                                    <%--<td><%=boardBean.getNo()%></td> --%>
                                    <td><%=(numbering - i)%></td>
                                    <td><%=boardBean.getId()%></td>
                                    <td><%=boardBean.getName()%></td>
                                    <td><%=boardBean.getSubject()%></td>
                                </tr>
                        <%    
                            }
                        %>
                    
                </tbody>
            </table>
            
            <div class="pagination">
                <a href="board_list.jsp?clickedPage=1">1</a>
                <a href="board_list.jsp?clickedPage=2">2</a>
                <a href="board_list.jsp?clickedPage=3">3</a>
                <a href="board_list.jsp?clickedPage=4">4</a>
            </div>
            <div class="btns">
                <a href="board_write.jsp" id="join">글쓰기</a>
            </div>
        </form>
    </div>
</body>
</html>
 
cs

-> board_list.jsp

 

board_list.jsp 결과

-> board_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
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
@charset "UTF-8";
 
{
    margin: 0;
    padding: 0;
}
 
body {
    font-family: "Noto Sans KR";
}
{
    text-decoration:none;
    color:inherit;
}
table {
    border: none;
    border-collapse: collapse;
    border-top: 1px solid #d9d9d9;
}
 
table tr th {
    background-color: #f8f8f8;
}
 
table tr th, table tr td {
    padding: 15px;
    border-bottom: 1px solid #d9d9d9;
}
 
table tr td input[type="text"], table tr td input[type="password"],
    table tr td select {
    height: 30px;
    border: 1px solid #d9d9d9;
    padding: 5px;
    border-radius: 3px;
    width: 50%;
}
 
table tr td select {
    width: 120px;
}
 
.formBox {
    width: 1000px;
    margin: 100px auto;
}
 
.formBox h2 {
    margin-bottom: 50px;
    text-align: center;
}
 
.formBox .btns {
    text-align: center;
    margin-top: 50px;
}
 
.formBox .btns * {
    display: inline-block;
    width: 150px;
    height: 50px;
    line-height:50px;
    border: none;
    border-radius: 5px;
    box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1);
    font-size: 16px;
    font-weight: 500;
    background-color: rgb(109, 109, 109);
    color: #fff;
}
 
.formBox #contents {
    width:100%;
    min-height:400px;
    resize: none;
    border: 1px solid #d9d9d9;
    padding: 5px;
}
 
.formBox .btns #join {
    background-color: rgb(1, 121, 177);
}
 
.formBox .pagination {
    margin-top: 50px;
    text-align: center;
}
.formBox .pagination a {
    display: inline-block;
    width:30px;
    height:30px;
    background-color: #000;
    color:#fff;
    font-size: 14px;
    line-height: 30px;
    border-radius: 3px;
}
 
 
#list tbody td {
    text-align: center;
}
#list tbody tr:hover {
    background-color:#000;
    color:#fff;    
}
 
cs

-> board_css.css

 

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
package com.heeyeon.model;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
 
public class BoardDao {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:xe";
    String id= "heeyeon0";
    String pw ="1234";
    
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    
    
    private void getConnection() {
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url,id,pw);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    private void close(ResultSet rs,PreparedStatement pstmt, Connection con) {
        try {
            if(rs!=null) rs.close();
            if(pstmt!=null) pstmt.close();
            if(con!=null) con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void close(PreparedStatement pstmt, Connection con) {
        try {
            if(pstmt!=null) pstmt.close();
            if(con!=null) con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public int insertBoard(BoardBean boardBean) {
        int result = 0;
        try {
            getConnection();
            String sql = "INSERT INTO BOARD03 VALUES (BOARD03_SEQ.NEXTVAL,?,?,?,?,?)";
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, boardBean.getId());
            pstmt.setString(2, boardBean.getName());
            pstmt.setString(3, boardBean.getSubject());
            pstmt.setString(4, boardBean.getPassword());
            pstmt.setString(5, boardBean.getContents());
            result = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(pstmt,con);
        }
        return result;
    }
    public ArrayList<BoardBean> showAllBoard(int start, int end) {
        ArrayList<BoardBean> boardList = new ArrayList<BoardBean>();
        try {
            getConnection();
            //String sql = "SELECT * FROM BOARD03";
            String sql ="SELECT * FROM "
                    + "(SELECT ROWNUM AS NUM, B.* FROM ( "
                    + "    SELECT * FROM BOARD03 ORDER BY ID DESC "
                    + ") B) "
                    + "WHERE NUM >= ? AND NUM <= ?";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, start);
            pstmt.setInt(2, end);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                BoardBean boardBean = new BoardBean();
                boardBean.setNo(rs.getInt("no"));
                boardBean.setId(rs.getString("id"));
                boardBean.setName(rs.getString("name"));
                boardBean.setSubject(rs.getString("subject"));
                boardBean.setPassword(rs.getString("password"));
                boardBean.setContents(rs.getString("contents"));
                boardList.add(boardBean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(rs,pstmt,con);
        }
        return boardList;
    }
    
    //전체 게시물 갯수 가져오기....
    public int getTotal() {
        int total = 0;
        try {
            getConnection();
            String sql = "SELECT COUNT(*) TOTAL FROM BOARD03";
            pstmt = con.prepareStatement(sql);
            rs = pstmt.executeQuery();
            if(rs.next()) {
                total = rs.getInt("TOTAL");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(rs,pstmt,con);
        }
        return total;
    }
}
 
cs

-> BoardDao

 

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
package com.heeyeon.model;
 
public class BoardBean {
    private int no;
    private String id;
    private String name;
    private String subject;
    private String password;
    private String contents;
 
    public BoardBean() {
        super();
    }
    public BoardBean(int no, String id, String name, String subject, String password, String contents) {
        super();
        this.no = no;
        this.id = id;
        this.name = name;
        this.subject = subject;
        this.password = password;
        this.contents = contents;
    }
    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    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 getContents() {
        return contents;
    }
    public void setContents(String contents) {
        this.contents = contents;
    }
    
    @Override
    public String toString() {
        return "boardBean [no=" + no + ", id=" + id + ", name=" + name + ", subject=" + subject + ", password="
                + password + ", contents=" + contents + "]";
    }
    
}
 
cs

-> BoardBean

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$("#joinForm #join").on("click",function(){
    if($("#userID").val().length<=0) {
        alert("ID는 필수 입력 사항입니다.");
        $("#userID").focus();
        return;
    }else if($("#userName").val().length<=0){
        alert("이름은 필수 입력 사항입니다.");
        $("#userName").focus();
    } else if($("#userPassword").val().length<=0){
        alert("password는 필수 입력 사항입니다.");
        $("#userPassword").focus();
        return;
    } else {
        $("#joinForm").submit();
    }
});
cs

-> join.js

 

 

처음에 화가 났는지 ....... oracle BOARD03
BOARD03_SEQ
자바 EE 파일

실행은 잘 되지만 학원만 오면 왜 저렇게 빨간 X가 많은지 .......... 

실행 잘되면 됐지 모..

 

복습합세다