< board 만들기 >
게시물번호 작성자 제목 내용 작성일시 조회수 추천수 ip 상태정보
숫자 문자 문자 문자 날짜 숫자 숫자 문자 숫자
8 10 40 CLOB date 5 5 16 2
char ==> 2000자 LOB ==> CLOB
varchar2 ==> 4000자 BLOB
BFILE
* 테이블 만들기!
create table lobtest
(id1 clob,
id2 blob,
id3 bfile);
(D:\app\jhta\oradata\orcl oracle 파일 저장 경로)
* 테이블 생성
CREATE TABLE BOARD
(BNO NUMBER(8),
WRITER VARCHAR2(10),
TITLE VARCHAR2(40),
CONTENTS CLOB,
REGDATE DATE,
HITS NUMBER(5),
IP CHAR(16),
STATUS NUMBER(2));
* 시퀀스 생성
CREATE SEQUENCE BOARD_BNO_SEQ
START WITH 1
INCREMENT BY 1
NOCACHE
NOCYCLE;
- [BoardVO] 생성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BoardVO {
int bno;
String writer;
String title;
String contents;
String regdate;
int hits;
String ip;
int status;
}
|
cs |
- [BoardDAO] 생성 : 생성자 / selectAll / addOne / selectOne / updateOne / getTotalcount
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
|
<%@page import="vo.BoardVO"%>
<%@page import="dao.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>
<style>
table{
width: 900px;
margin: auto;
border-bottom: 1px solid gray;
}
table, th, td{
border-top: 1px solid black;
border-collapse: collapse;
text-align: center;
}
</style>
</head>
<body>
<%
// 전체 게시물 건수를 출력?
BoardDAO dao = new BoardDAO();
// 총 게시물 수 ? :
int totalCount = dao.getTotalCount();
// 한 페이지 당 게시물 건수 : 20
int recordPerPage = 20;
// 삼항연산자 사용
int totalPage = (totalCount%recordPerPage==0)?totalCount/recordPerPage:totalCount/recordPerPage+1;
// 303/20 = 15
// 303/20+1 = 16
// 현재 페이지 번호
String cp = request.getParameter("cp");
int currentPage = 0;
if(cp != null){
currentPage = Integer.parseInt(cp);
} else {
currentPage = 1;
}
// 1페이지 : 시작번호 1: 끝번호 : 20
// 2페이지 : 시작번호 21: 끝번호 : 40
// 3페이지 : 시작번호 41: 끝번호 : 60
// 시작 번호
int startNo = (currentPage-1)*recordPerPage+1;
// 1: (1-1)*20+1 => 1
// 2: (2-1)*20+1 => 21
// 3: (3-1)*20+1 => 41
// 끝 번호
int endNo = currentPage*recordPerPage;
// 시작 페이지 번호
int startPage = 1;
// 끝 페이지 번호
int endPage = totalPage;
// 시작 페이지 미세 조정
if(currentPage<=5){
startPage = 1;
} else if(currentPage >= 6){
startPage = currentPage-4;
}
// 끝페이지 번호
if(totalPage - currentPage <= 5) {
endPage = totalPage;
} else if(totalPage-currentPage > 5) {
if(currentPage<=5){
if(totalPage>10){
endPage=10;
} else {
endPage = totalPage;
}
} else {
endPage = currentPage + 4;
}
}
out.println("<h5> 총 게시물 수 : " + totalCount + "</h5>");
out.println("<h5> 한 페이지 당 게시물 건수 : " + recordPerPage + "</h5>");
out.println("<h5> 총 페이지 수 : " + totalPage + "</h5>");
out.println("<h5> 현재 페이지 번호 : " + currentPage + "</h5>");
out.println("<h5> 시작번호 : " + startNo + "</h5>");
out.println("<h5> 끝번호 : " + endNo + "</h5>");
%>
<table>
<tr>
<th>게시물번호</th>
<th>제목</th>
<th>작성자</th>
<th>조회수</th>
</tr>
<%
/* BoardDAO dao = new BoardDAO(); */
ArrayList<BoardVO> list = dao.selectAll(startNo, endNo);
for(BoardVO vo : list){
%>
<tr>
<td><%=vo.getBno() %></td>
<td><a href="detail.jsp?bno=<%=vo.getBno() %>"><%=vo.getTitle() %></a></td>
<td><%=vo.getWriter() %></td>
<td><%=vo.getHits() %></td>
</tr>
<%
}
%>
<tr>
<td colspan="4">
<a href="write.jsp"><input type="button" value="글쓰기" />
</a>
</td>
</tr>
<tr>
<td colspan="4">
<%for (int i=startPage; i<= endPage; i++ ){
%>
<a href="list.jsp?cp=<%=i%>">[<%=i %>]</a>
<%
}
%>
</td>
</tr>
</table>
</body>
</html>
|
cs |
** select All **
- cmd => board 테이블에 데이터 많이 넣어 보기
INSERT INTO BOARD
VALUES (BOARD_BNO_SEQ.NEXTVAL, 'SCOTT', '1111', '2222', SYSDATE, 0, '127.0.0.1', 1);
=> <plsql> : oracle에서만 쓰이는 문법! insert 문을 반복문으로 실행하듯이
BEGIN
FOR i IN 1..300 LOOP
INSERT INTO BOARD
VALUES (BOARD_BNO_SEQ.NEXTVAL, 'SCOTT'||i, 'TITLE'||i, 'CONTENTS'||i,
SYSDATE, 0, '127.0.0.1', 1);
END LOOP;
END;
/
=> board에서 11번 ~ 20번 게시물 가져오기
SELECT rn, bno, writer, title, contents, regdate, hits, ip, status
FROM (SELECT ROWNUM rn, bno, writer, title, contents, regdate, hits, ip, status
FROM (SELECT bno, writer, title, contents, regdate, hits, ip, status
FROM BOARD
ORDER BY BNO DESC)
WHERE ROWNUM <= 20)
where rn >= 11;
>> 시작 번호와 끝 번호 지정해서 활용 가능
<SQL 참고용!>
* 급여가 많은 순으로 3명 출력
SELECT ROWNUM, EMPNO, ENAME, SAL
FROM (SELECT EMPNO, ENAME, SAL
FROM EMP
ORDER BY SAL DESC)
WHERE ROWNUM <= 3;
* 급여 적은 순으로 3명 출력
SELECT ROWNUM, EMPNO, ENAME, SAL
FROM (SELECT EMPNO, ENAME, SAL
FROM EMP
ORDER BY SAL ASC)
WHERE ROWNUM <= 3;
* 급여 4위부터 7위 구하려면? 또 뷰처럼.. ( VIEW는 몇위보다 작은 값~ 이렇게는 구해지지 않음...)
SELECT RN, EMPNO, ENAME, SAL
FROM( SELECT ROWNUM RN, EMPNO, ENAME, SAL
FROM (SELECT EMPNO, ENAME, SAL
FROM EMP
ORDER BY SAL DESC)
WHERE ROWNUM <=7)
WHERE RN>=4;
* 그럼, boad에서 최근 게시물 20개 가져오는 건?
SELECT rn, bno, writer, title, contents, regdate, hits, ip, status
FROM (SELECT bno, writer, title, contents, regdate, hits, ip, status
FROM BOARD
ORDER BY BNO DESC)
WHERE ROWNUM <= 20;
- [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
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
|
<%@page import="vo.BoardVO"%>
<%@page import="dao.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>
<style>
table{
width: 900px;
margin: auto;
border-bottom: 1px solid gray;
}
table, th, td{
border-top: 1px solid black;
border-collapse: collapse;
text-align: center;
}
</style>
</head>
<body>
<%
// 전체 게시물 건수를 출력?
BoardDAO dao = new BoardDAO();
// 총 게시물 수 ? :
int totalCount = dao.getTotalCount();
// 한 페이지 당 게시물 건수 : 20
int recordPerPage = 20;
// 삼항연산자 사용
int totalPage = (totalCount%recordPerPage==0)?totalCount/recordPerPage:totalCount/recordPerPage+1;
// 303/20 = 15
// 303/20+1 = 16
// 현재 페이지 번호
String cp = request.getParameter("cp");
int currentPage = 0;
if(cp != null){
currentPage = Integer.parseInt(cp);
} else {
currentPage = 1;
}
// 1페이지 : 시작번호 1: 끝번호 : 20
// 2페이지 : 시작번호 21: 끝번호 : 40
// 3페이지 : 시작번호 41: 끝번호 : 60
// 시작 번호
int startNo = (currentPage-1)*recordPerPage+1;
// 1: (1-1)*20+1 => 1
// 2: (2-1)*20+1 => 21
// 3: (3-1)*20+1 => 41
// 끝 번호
int endNo = currentPage*recordPerPage;
// 시작 페이지 번호
int startPage = 1;
// 끝 페이지 번호
int endPage = totalPage;
// 시작 페이지 미세 조정
if(currentPage<=5){
startPage = 1;
} else if(currentPage >= 6){
startPage = currentPage-4;
}
// 끝페이지 번호
if(totalPage - currentPage <= 5) {
endPage = totalPage;
} else if(totalPage-currentPage > 5) {
if(currentPage<=5){
if(totalPage>10){
endPage=10;
} else {
endPage = totalPage;
}
} else {
endPage = currentPage + 4;
}
}
out.println("<h5> 총 게시물 수 : " + totalCount + "</h5>");
out.println("<h5> 한 페이지 당 게시물 건수 : " + recordPerPage + "</h5>");
out.println("<h5> 총 페이지 수 : " + totalPage + "</h5>");
out.println("<h5> 현재 페이지 번호 : " + currentPage + "</h5>");
out.println("<h5> 시작번호 : " + startNo + "</h5>");
out.println("<h5> 끝번호 : " + endNo + "</h5>");
%>
<table>
<tr>
<th>게시물번호</th>
<th>제목</th>
<th>작성자</th>
<th>조회수</th>
</tr>
<%
/* BoardDAO dao = new BoardDAO(); */
ArrayList<BoardVO> list = dao.selectAll(startNo, endNo);
for(BoardVO vo : list){
%>
<tr>
<td><%=vo.getBno() %></td>
<td><a href="detail.jsp?bno=<%=vo.getBno() %>"><%=vo.getTitle() %></a></td>
<td><%=vo.getWriter() %></td>
<td><%=vo.getHits() %></td>
</tr>
<%
}
%>
<tr>
<td colspan="4">
<a href="write.jsp"><input type="button" value="글쓰기" />
</a>
</td>
</tr>
<tr>
<td colspan="4">
<%for (int i=startPage; i<= endPage; i++ ){
%>
<a href="list.jsp?cp=<%=i%>">[<%=i %>]</a>
<%
}
%>
</td>
</tr>
</table>
</body>
</html>
|
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
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>글쓰기</title>
</head>
<body>
<form action="writeOk.jsp" method="get">
<table>
<tr>
<th>작성자</th>
<td><input type="text" name="writer" id="" /></td>
</tr>
<tr>
<th>제목</th>
<td><input type="text" name="title" id="" /></td>
</tr>
<tr>
<th>내용</th>
<td><textarea name="contents" id="" cols="30" rows="10"></textarea></td>
</tr>
<tr>
<td colspan="2">
<input type="button" value="목록" />
<input type="submit" value="작성" />
<input type="reset" value="다시쓰기" />
</td>
</tr>
</table>
</form>
</body>
</html>
|
cs |
- [writeOk.jsp] : 작성된 글 확인 > list.jsp로 redirect
>> BoardDAO에 addOne 추가
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="vo.BoardVO"%>
<%@page import="dao.BoardDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// writeOk.jsp
// 1. 파라미터값 가져오기
String writer = request.getParameter("writer");
String title = request.getParameter("title");
String contents = request.getParameter("contents");
out.println("writer: "+writer);
String writer2 = request.getParameter("writer2");
out.println("writer2: "+writer2);
// 2. 이 값이 널이면 list.jsp 리다이렉트
if(writer == null || title== null || contents == null || writer.equals("") || title.equals("") || contents.equals("")){
response.sendRedirect("list.jsp");
} else if( writer != null && title!= null && contents != null){
// 3. 널이 아니면 dao 객체 생성
BoardDAO dao = new BoardDAO();
BoardVO vo = new BoardVO();
vo.setWriter(writer);
vo.setTitle(title);
vo.setContents(contents);
// ip
vo.setIp(request.getRemoteAddr());
// 4. dao.addOne(vo); // db에 저장
dao.addOne(vo);
// 5. list.jsp 리다이렉트
response.sendRedirect("list.jsp");
}
%>
|
cs |
- [detail.jsp] : 작성글에 대한 자세한 정보 출력
> BoardDAO에 selectOne 추가 + 수정, 삭제 버튼
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="vo.BoardVO"%>
<%@page import="dao.BoardDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>detail.jsp</title>
<style>
table{
width: 900px;
margin: auto;
border-bottom: 1px solid gray;
}
table, th, td{
border-top: 1px solid black;
border-collapse: collapse;
text-align: center;
}
</style>
</head>
<body>
<h1>detail.jsp</h1>
<%
// 1. 전달 받은 파라미터의 값 가져오기
String b = request.getParameter("bno");
String writer = request.getParameter("writer");
String title = request.getParameter("title");
String contents = request.getParameter("contents");
// 2. bno null이 아니면
if(b!=null){
// 3. 숫자로 형변환
int bno = Integer.parseInt(b);
// 4. dao 객체
BoardDAO dao = new BoardDAO();
// 5. dao를 통해서 지정한 게시물을 가져오기 (vo) : dao.selectOne(bno);
BoardVO vo = dao.selectOne(bno);
if(vo!=null){
// 6. 화면에 출력 (테이블 형태로 출력)
%>
<table>
<tr>
<th>작성자</th>
<td><%= vo.getWriter() %></td>
<th>조회수</th>
<td><%= vo.getHits() %></td>
<th>작성일시</th>
<td><%= vo.getRegdate() %></td>
</tr>
<tr>
<th>제목</th>
<td colspan="5"><input type="text"
name="title" value="<%=vo.getTitle() %>" /></td>
</tr>
<tr>
<th>내용</th>
<td colspan="5"><%=vo.getContents() %></td>
</tr>
<tr>
<td colspan="6">
<a href="list.jsp">
<input type="button" value="목록" /></a>
<a href="modifyForm.jsp?bno=<%=vo.getBno() %>"><input type="button" value="수정" /></a>
<a href="deleteOk.jsp?bno=<%= vo.getBno() %>"><input type="button" value="삭제" /></a>
</td>
</tr>
</table>
<%
} // if end
// 7. 자원반납 dao.close();
dao.close();
}
%>
</body>
</html>
|
cs |
- [modifyForm.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
|
<%@page import="vo.BoardVO"%>
<%@page import="dao.BoardDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>수정하기</title>
<style>
table{
width: 900px;
margin: auto;
border-bottom: 1px solid gray;
}
table, th, td{
border-top: 1px solid black;
border-collapse: collapse;
text-align: center;
}
</style>
</head>
<body>
<h1>detail.jsp</h1>
<%
// 1. 전달 받은 파라미터의 값 가져오기
String b = request.getParameter("bno");
String writer = request.getParameter("writer");
String title = request.getParameter("title");
String contents = request.getParameter("contents");
// 2. bno null이 아니면
if(b!=null){
// 3. 숫자로 형변환
int bno = Integer.parseInt(b);
// 4. dao 객체
BoardDAO dao = new BoardDAO();
// 5. dao를 통해서 지정한 게시물을 가져오기 (vo) : dao.selectOne(bno);
BoardVO vo = dao.selectOne(bno);
if(vo!=null){
// 6. 화면에 출력 (테이블 형태로 출력)
%>
<form action="modifyOk.jsp">
<table>
<tr>
<th>작성자</th>
<td><%= vo.getWriter() %></td>
<th>조회수</th>
<td><%= vo.getHits() %></td>
<th>작성일시</th>
<td><%= vo.getRegdate() %>
<input type="hidden" name="bno" value="<%=vo.getBno() %>" />
</td>
</tr>
<tr>
<th>제목</th>
<td colspan="5"><input type="text"
name="title" value="<%=vo.getTitle() %>" /></td>
</tr>
<tr>
<th>내용</th>
<td colspan="5"><textarea name="contents" id="" cols="30" rows="10"><%=vo.getContents() %></textarea></td>
</tr>
<tr>
<td colspan="6">
<a href="list.jsp">
<input type="button" value="목록" /></a>
<input type="submit" value="수정" />
</td>
</tr>
</table>
</form>
<%
} // if end
// 7. 자원반납 dao.close();
dao.close();
}
%>
</head>
<body>
</body>
</html>
|
cs |
- [modifyOk.jsp] : 수정되도록(list.jsp로 리다이렉트), 수정한 시간으로 기록되게
> BoardDAO에 updateOne 추가
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
|
<%@page import="vo.BoardVO"%>
<%@page import="dao.BoardDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// modifyOk.jsp
// 1. 파라미터 값 가져오기
String bno = request.getParameter("bno");
String title = request.getParameter("title");
String contents = request.getParameter("contents");
// 2. bno가 널이 아닐 때
if(bno != null) {
int b = Integer.parseInt(bno);
// 3. dao 객체
BoardDAO dao = new BoardDAO();
// 4. vo 객체
BoardVO vo = new BoardVO();
// 5. setter로 값 지정
vo.setBno(b);
vo.setTitle(title);
vo.setContents(contents);
// 6. dao.updateOne(vo);
dao.updateOne(vo);
// 7. list.jsp로 리다이렉트
response.sendRedirect("list.jsp");
}
%>
|
cs |
- [deleteOk.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
|
<%@page import="vo.BoardVO"%>
<%@page import="dao.BoardDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// 1. parameter 값 가져오기 : bno
String b = request.getParameter("bno");
// 2. null 아니면
if(b!= null) {
// 3. 숫자로 형변환
int bno = Integer.parseInt(b);
// 4. dao 객체
BoardDAO dao = new BoardDAO();
// 5. dao.deleteOne(bno);
dao.deleteOne(bno);
// 6. list.jsp redirect
response.sendRedirect("list.jsp");
}
%>
|
cs |
'web' 카테고리의 다른 글
[AJAX] 텍스트 입력해서 데이터 출력 / 이미지 변경 (0) | 2023.05.07 |
---|---|
[jquery] jquery / css 응용 (0) | 2023.05.07 |
[jsp] 글 작성 게시물 만들기 (0) | 2023.04.30 |
[jsp] 쇼핑몰 (제품 리스트/ 제품 상세 페이지/ 장바구니 만들기/ 우편번호 찾기) (0) | 2023.04.30 |
[jsp] 네이버 메인 페이지 만들기(토픽 별 페이지, 로그인 화면) (0) | 2023.04.30 |