반응형
model1 BoardTO BoardDAO paging model1 List 페이지 표현을 위한 TO BoardListTo BoardTO BoardDAO
ModelEx01.paging1.BoardDAO
package paging1;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
// 각 페이지당 할일
public class BoardDAO {
// jsp 처리할 일을 메소드화
// jsp 페이지와 1 : 1
private DataSource datasource = null;
public BoardDAO() {
// 데이터 소스를 얻어 냄
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
this.datasource = (DataSource)envCtx.lookup("jdbc/oracle1");
} catch (NamingException e) {
System.out.println("[에러] : " + e.getMessage());
}
}
public void boardWrite() {
// 할 일 없음
}
public int boardWriteOk(BoardTO to) {
Connection conn = null;
PreparedStatement pstmt = null;
int flag = 1;
try {
conn = datasource.getConnection();
String sql = "insert into board1 values (board_seq.nextval, ?, ?, ?, ?, ?, 0, ?, sysdate)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, to.getSubject());
pstmt.setString(2, to.getWriter());
pstmt.setString(3, to.getMail());
pstmt.setString(4, to.getPassword());
pstmt.setString(5, to.getContent());
pstmt.setString(6, to.getWip());
int result = pstmt.executeUpdate();
if(result == 1) {
flag = 0;
}
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(pstmt != null) try {pstmt.close();} catch(SQLException e) {};
if(conn != null) try {conn.close();} catch(SQLException e) {};
}
return flag;
}
public BoardListTO boardList(BoardListTO listTO) { // 20180725
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int cpage = listTO.getCpage(); // 20180725
int recordPerPage = listTO.getrecordPerPage(); // 20180725
int blockPerPage = listTO.getblockPerPage(); // 20180725
//ArrayList<BoardTO> lists = new ArrayList<>(); // 20180725
try {
conn = datasource.getConnection();
String sql = "select seq, subject, writer, to_char(wdate, 'YYYY/MM/DD') wdate, hit, trunc(sysdate-wdate) wgap from board1 order by seq desc";
pstmt = conn.prepareStatement(sql);
pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = pstmt.executeQuery();
// 커서를 맨 마지막행으로 이동
rs.last();
// 데이터 개수가 나옴
listTO.setTotalRecord(rs.getRow()); // 20180725
rs.beforeFirst();
listTO.setTotalPage(((listTO.getTotalRecord() - 1)/recordPerPage) + 1); // 20180725
int skip = (cpage - 1) * recordPerPage; // 20180725
if(skip != 0) rs.absolute(skip); // 20180725
ArrayList<BoardTO> boardLists = new ArrayList<>(); // 20180725
//while(rs.next()) { // 20180725
for(int i=0; i<recordPerPage && rs.next(); i++) { // 20180725
BoardTO to = new BoardTO();
to.setSeq(rs.getString("seq"));
to.setSubject(rs.getString("subject"));
to.setWriter(rs.getString("writer"));
to.setWdate(rs.getString("wdate"));
to.setHit(rs.getString("hit"));
to.setWgap(rs.getInt("wgap"));
//lists.add(to); // 20180725
boardLists.add(to); // 20180725
}
listTO.setBoardLists(boardLists); // 20180725
listTO.setStartBlock(((cpage -1)/blockPerPage)*blockPerPage + 1); // 20180725
listTO.setEndBlock(((cpage -1)/blockPerPage)*blockPerPage + blockPerPage); tring("writer"));
to.setSubject(rs.getString("subject"));
to.setContent(rs.getString("content") == null ? "" : rs.getString("content").replaceAll("\n", "<br />"));
to.setMail(rs.getString("mail"));
int result = pstmt.executeUpdate();
if(result == 1) {
// 정상
flag = 0;
} else if (result == 0) {
// 비밀번호 오류
flag = 1;
}
}
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(rs != null) try {rs.close();} catch(SQLException e) {};
if(pstmt != null) try {pstmt.close();} catch(SQLException e) {};
if(conn != null) try {conn.close();} catch(SQLException e) {};
}
return flag;
}
public void boardDelete() {
// 할 일 없음
}
public int boardDeleteOk(BoardTO to) {
Connection conn = null;
PreparedStatement pstmt = null;
int flag = 2;
try {
conn = datasource.getConnection();
String sql = "delete from board1 where seq = ? and password = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, to.getSeq());
pstmt.setString(2, to.getPassword());
int result = pstmt.executeUpdate();
if(result == 1) {
// 정상
flag = 0;
} else if (result == 0) {
// 비밀번호 오류
flag = 1;
}
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(pstmt != null) try {pstmt.close();} catch(SQLException e) {};
if(conn != null) try {conn.close();} catch(SQLException e) {};
}
return flag;
}
}
ModelEx01.paging1.BoardTO
package paging1;
// 데이터
public class BoardTO {
// data - table / select(list + view)
private String seq;
private String subject;
private String writer;
private String mail;
private String[] email;
private String password;
private String content;
private String hit;
private String wip;
private String wdate;
private int wgap;
public String getSeq() {
return seq;
}
public void setSeq(String seq) {
this.seq = seq;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getHit() {
return hit;
}
public void setHit(String hit) {
this.hit = hit;
}
public String getWip() {
return wip;
}
public void setWip(String wip) {
this.wip = wip;
}
public String getWdate() {
return wdate;
}
public void setWdate(String wdate) {
this.wdate = wdate;
}
public int getWgap() {
return wgap;
}
public void setWgap(int wgap) {
this.wgap = wgap;
}
}
ModelEx01.paging1.BoardListTO
package paging1;
import java.util.ArrayList;
public class BoardListTO {
private int cpage;
private int recordPerPage;
private int blockPerPage;
private int totalPage;
private int totalRecord;
private int startBlock;
private int endBlock;
private ArrayList<BoardTO> boardLists;
public BoardListTO() {
this.cpage = 1;
this.recordPerPage = 10;
this.blockPerPage = 5;
this.totalPage = 1;
this.totalRecord = 0;
}
public int getCpage() {
return cpage;
}
public void setCpage(int cpage) {
this.cpage = cpage;
}
public int getrecordPerPage() {
return recordPerPage;
}
public void setrecordPerPage(int recordPerPage) {
this.recordPerPage = recordPerPage;
}
public int getblockPerPage() {
return blockPerPage;
}
public void setblockPerPage(int blockPerPage) {
this.blockPerPage = blockPerPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
}
public int getStartBlock() {
return startBlock;
}
public void setStartBlock(int startBlock) {
this.startBlock = startBlock;
}
public int getEndBlock() {
return endBlock;
}
public void setEndBlock(int endBlock) {
this.endBlock = endBlock;
}
public ArrayList<BoardTO> getBoardLists() {
return boardLists;
}
public void setBoardLists(ArrayList<BoardTO> boardLists) {
this.boardLists = boardLists;
}
}
ModelEx01.paging1.board_list1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- Java 전처리 -->
<%@ page import="paging1.BoardTO" %> <!-- 20180725 -->
<%@ page import="paging1.BoardListTO" %> <!-- 20180725 -->
<%@ page import="paging1.BoardDAO" %> <!-- 20180725 -->
<%@ page import="java.util.ArrayList" %> <!-- 20180725 -->
<%
request.setCharacterEncoding("utf-8");
int cpage = 1;
if(request.getParameter("cpage") != null && !request.getParameter("cpage").equals("")) {
cpage = Integer.parseInt(request.getParameter("cpage"));
}
BoardListTO listTO = new BoardListTO(); // 20180725
listTO.setCpage(cpage); // 20180725
BoardDAO dao = new BoardDAO(); // 20180725
listTO = dao.boardList(listTO); // 20180725
//int recordPerPage = 10; // 20180725
int totalRecord = listTO.getTotalRecord(); // 20180725
int totalPage = listTO.getTotalPage(); // 20180725
int blockPerPage = listTO.getblockPerPage(); // 한 페이지당 5개 // 20180725
int startBlock = listTO.getStartBlock(); // 20180725
int endBlock = listTO.getEndBlock(); // 20180725
//Connection conn = null; // 20180725
//PreparedStatement pstmt = null; // 20180725
//ResultSet rs = null; // 20180725
ArrayList<BoardTO> boardlLists = listTO.getBoardLists(); // 20180725
StringBuffer sb = new StringBuffer();
/*
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource datasource = (DataSource)envCtx.lookup("jdbc/oracle1");
conn = datasource.getConnection();
String sql = "select seq, subject, writer, to_char(wdate, 'YYYY/MM/DD') wdate, hit, trunc(sysdate-wdate) wgap from board1 order by seq desc";
pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = pstmt.executeQuery();
// 커서를 맨 마지막행으로 이동
rs.last();
// 데이터 개수가 나옴
totalRecord = rs.getRow();
rs.beforeFirst();
// 전체 페이지 수 구하는 공식
totalPage = ((totalRecord -1) / recordPerPage) + 1;
int skip = (cpage -1) * recordPerPage;
// absolute - 읽을 위치(커서 위치) 지정
if(skip != 0) {
rs.absolute(skip);
}
// i<recordPerPage && rs.next() - recordPerPage 이하이면서 동시에 다음 페이지를 읽을 준비
*/ // 20180725
//for(int i=0; i<recordPerPage && rs.next(); i++) {
for(BoardTO to : boardlLists) {
String seq = to.getSeq();
String subject = to.getSubject();
String writer = to.getWriter();
String wdate = to.getWdate();
String hit = to.getHit();
Integer wgap = to.getWgap();
sb.append("<tr>");
sb.append(" <td> </td>");
//sb.append(" <td>").append(rs.getString("seq")).append("</td>");
sb.append(" <td>" + seq + "</td>");
if(wgap == 0 ) {
sb.append(" <td class='left'><a href='./board_view1.jsp?cpage=" + cpage + "&seq=" + seq + "'>" + subject + "</a> <img src='../images/icon_hot.gif' alt='HOT'></td>");
} else {
sb.append(" <td class='left'><a href='./board_view1.jsp?cpage=" + cpage + "&seq=" + seq + "'>" + subject + "</a></td>");
}
sb.append(" <td>" + writer + "</td>");
sb.append(" <td>" + wdate + "</td>");
sb.append(" <td>" + hit + "</td>");
sb.append(" <td> </td>");
sb.append("</tr>");
}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="../css/board_list.css">
</head>
<body>
<!-- 상단 디자인 -->
<div class="con_title">
<h3>게시판 + 1;
//int endBlock = ((cpage -1) / blockPerPage)*blockPerPage + blockPerPage;
if(endBlock >= totalPage) {
endBlock = totalPage;
}
%>
<%
if(startBlock == 1) {
out.println("<span class='on'><<</span>");
} else {
out.println("<span class='off'><a href='./board_list1.jsp?cpage=" + (startBlock - blockPerPage) +"'><< </a></span>");
out.println(" ");
}
%>
<%
if(cpage == 1) {
out.println("<span class='on'><</span>");
} else {
out.println("<span class='off'><a href='./board_list1.jsp?cpage=" + (cpage-1) + "'>< </a></span>");
out.println(" ");
}
%>
<%
for(int i=startBlock; i<=endBlock; i++) {
if(cpage == i) {
out.println("<span class='on'>[ " + i + " ]</span>");
} else {
out.println("<span class='off'><a href='./board_list1.jsp?cpage=" + i + "'>" + i + "</a></span>");
}
}
%>
<%
if(cpage == totalPage) {
out.println("<span class='on'>></span>");
} else {
out.println("<span class='off'><a href='./board_list1.jsp?cpage=" + (cpage+1) + "'>> </a></span>");
out.println(" ");
}
%>
<%
if(endBlock == totalPage) {
out.println("<span class='on'>>></span>");
} else {
out.println("<span class='off'><a href='./board_list1.jsp?cpage=" + (startBlock + blockPerPage) +"'>>> </a></span>");
out.println(" ");
}
%>
</div>
</div>
<!--//페이지넘버-->
</div>
</div>
<!--//하단 디자인 -->
</body>
</html>
답글 기능이 들어간 게시판을 구현하기
답변글(reply)
글
-> 답글
리스트
자세히보기
수정
삭제
답글(별도의 페이지)
쓰기
* 댓글(comment)
---------------------------------------------------------------------------
seq - 고유번호 (글쓰기)
grp - 그룹번호 부모글 seq 받아서 사용
grps - 같은 그룹내에서 글의 순서
1. 부모글의 grps보다 큰 grps 번호는 무조건 1씩 증가하고
2. 자신은 부모글 + 1
grpl - 글 깊이
1. 부모글 grpl + 1
seq grp grps grpl
모글1 w 1 1 0 0
모글2 w 2 2 0 0
1-1 r 3 1 4 1
1-2 r 4 1 2 1
1-3 r 5 1 1 1
1-1-1 r 6 1 6 2
1-1-2 r 7 1 5 2
1-2-1 r 8 1 3 2
order by grp desc, grps asc
모글2 w 2 2 0 0
모글1 w 1 1 0 0
1-3 r 5 1 1 1
1-2 r 4 1 2 1
1-2-1 r 8 1 3 2
1-1 r 3 1 4 1
1-1-2 r 7 1 5 2
1-1-1 r 6 1 6 2
--------------------------------------------------------------------------
답글 기능이 있는 게시판을 만들기 위해서 해당 기능을 구현할 수 있기 위해 테이블을 아래와 같이 새로 만들어 준다
SQL> create table rep_board1 (
2 seq number not null primary key,
3 grp number not null,
4 grps number not null,
5 grpl number not null,
6 subject varchar2(150) not null,
7 writer varchar2(12) not null,
8 mail varchar2(50),
9 password varchar2(12) not null,
10 content varchar2(2000),
11 hit number not null,
12 wip varchar2(15) not null,
13 wdate date not null
14 );
테이블이 생성되었습니다.
SQL> desc rep_board1
이름 널? 유형
----------------------------------------- -------- ----------------------------
SEQ NOT NULL NUMBER
GRP NOT NULL NUMBER
GRPS NOT NULL NUMBER
GRPL NOT NULL NUMBER
SUBJECT NOT NULL VARCHAR2(150)
WRITER NOT NULL VARCHAR2(12)
MAIL VARCHAR2(50)
PASSWORD NOT NULL VARCHAR2(12)
CONTENT VARCHAR2(2000)
HIT NOT NULL NUMBER
WIP NOT NULL VARCHAR2(15)
WDATE NOT NULL DATE
BoardEx01.reply1.board_write_ok
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.SQLException" %>
<%
request.setCharacterEncoding("utf-8");
String subject = request.getParameter("subject");
String writer = request.getParameter("writer");
String mail = request.getParameter("mail1") + "@" + request.getParameter("mail2");
String password = request.getParameter("password");
String content = request.getParameter("content");
String wip = request.getRemoteAddr();
Connection conn = null;
PreparedStatement pstmt = null;
int flag = 1;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource datasource = (DataSource)envCtx.lookup("jdbc/oracle1");
conn = datasource.getConnection();
String sql = "insert into rep_board1 values (board_seq.nextval, board_seq.currval, 0, 0, ?, ?, ?, ?, ?, 0, ?, sysdate)"; // 20180725
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, subject);
pstmt.setString(2, writer);
pstmt.setString(3, mail);
pstmt.setString(4, password);
pstmt.setString(5, content);
pstmt.setString(6, wip);
int result = pstmt.executeUpdate();
if(result == 1) {
flag = 0;
}
} catch(NamingException e) {
System.out.println("[에러] " + e.getMessage());
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
out.println("<script type='text/javascript'>");
if(flag == 0) {
out.println("alert('글쓰기에 성공했습니다.');");
out.println("location.href='./board_list1.jsp';");
} else if(flag == 1) {
out.println("alert('글쓰기에 실패했습니다.');");
out.println("history.back();");
}
out.println("</script>");
%>
BoardEx01.reply1.board_list1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- Java 전처리 -->
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%
request.setCharacterEncoding("utf-8");
int cpage = 1;
if(request.getParameter("cpage") != null && !request.getParameter("cpage").equals("")) {
cpage = Integer.parseInt(request.getParameter("cpage"));
}
int recordPerPage = 10;
int totalRecord = 0;
int totalPage = 1;
int blockPerPage = 5; // 한 페이지당 5개
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuffer sb = new StringBuffer();
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource datasource = (DataSource)envCtx.lookup("jdbc/oracle1");
conn = datasource.getConnection();
String sql = "select seq, subject, writer, to_char(wdate, 'YYYY/MM/DD') wdate, hit, trunc(sysdate-wdate) wgap from rep_board1 order by seq desc"; //20180725
pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = pstmt.executeQuery();
// 커서를 맨 마지막행으로 이동
rs.last();
// 데이터 개수가 나옴
totalRecord = rs.getRow();
rs.beforeFirst();
// 전체 페이지 수 구하는 공식
totalPage = ((totalRecord -1) / recordPerPage) + 1;
int skip = (cpage -1) * recordPerPage;
// absolute - 읽을 위치(커서 위치) 지정
if(skip != 0) {
rs.absolute(skip);
}
// i<recordPerPage && rs.next() - recordPerPage 이하이면서 동시에 다음 페이지를 읽을 준비
for(int i=0; i<recordPerPage && rs.next(); i++) {
sb.append("<tr>");
sb.append(" <td> </td>");
//sb.append(" <td>").append(rs.getString("seq")).append("</td>");
sb.append(" <td>" + rs.getString("seq") + "</td>");
if(rs.getInt("wgap") == 0 ) {
sb.append(" <td class='left'><a href='./board_view1.jsp?cpage=" + cpage + "&seq=" + rs.getString("seq") + "'>" + rs.getString("subject") + "</a> <img src='../images/icon_hot.gif' alt='HOT'></td>");
} else {
sb.append(" <td class='left'><a href='./board_view1.jsp?cpage=" + cpage + "&seq=" + rs.getString("seq") + "'>" + rs.getString("subject") + "</a></td>");
}
sb.append(" <td>" + rs.getString("writer") + "</td>");
sb.append(" <td>" + rs.getString("wdate") + "</td>");
sb.append(" <td>" + rs.getString("hit") + "</td>");
sb.append(" <td> </td>");
sb.append("</tr>");
}
} catch(NamingException e) {
System.out.println("[에러] " + e.getMessage());
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewpo</th>
<th width="17%">등록일</th>
<th width="5%">조회</th>
<th width="3%"> </th>
</tr>
<tr>
<!-- 시작 -->
<%= sb %>
<!-- 끝 -->
</tr>
</table>
</div>
<!--//게시판-->
<div class="align_right">
<button type="button" class="btn_write btn_txt01" style="cursor: pointer;" onclick="location.href='board_write1.jsp?cpage=<%=cpage %>'">쓰기</button>
</div>
<!--페이지넘버-->
<div class="paginate_regular">
<div class="board_pagetab" align="absmiddle">
<%
int startBlock = ((cpage -1) / blockPerPage)*blockPerPage + 1;
int endBlock = ((cpage -1) / blockPerPage)*blockPerPage + blockPerPage;
if(endBlock >= totalPage) {
endBlock = totalPage;
}
%>
<%
if(startBlock == 1) {
out.println("<span class='on'><<</span>");
} else {
out.println("<span class='off'><a href='./board_list1.jsp?cpage=" + (startBlock - blockPerPage) +"'><< </a></span>");
out.println(" ");
}
%>
<%
if(cpage == 1) {
out.println("<span class='on'><</span>");
} else {
out.println("<span class='off'><a href='./board_list1.jsp?cpage=" + (cpage-1) + "'>< </a></span>");
out.println(" ");
}
%>
<%
for(int i=startBlock; i<=endBlock; i++) {
if(cpage == i) {
out.println("<span class='on'>[ " + i + " ]</span>");
} else {
out.println("<span class='off'><a href='./board_list1.jsp?cpage=" + i + "'>" + i + "</a></span>");
}
}
%>
<%
if(cpage == totalPage) {
out.println("<span class='on'>></span>");
} else {
out.println("<span class='off'><a href='./board_list1.jsp?cpage=" + (cpage+1) + "'>> </a></span>");
out.println(" ");
}
%>
<%
if(endBlock == totalPage) {
out.println("<span class='on'>>></span>");
} else {
out.println("<span class='off'><a href='./board_list1.jsp?cpage=" + (startBlock + blockPerPage) +"'>>> </a></span>");
out.println(" ");
}
%>
</div>
</div>
<!--//페이지넘버-->
</div>
</div>
<!--//하단 디자인 -->
</body>
</html>
원래 데이터는 하나도 없었고 1234로 방금 만들어 줬음
SQL> select seq, grp, grps, grpl from rep_board1;
SEQ GRP GRPS GRPL
---------- ---------- ---------- ----------
362 362 0 0
SQL> select seq, grp, grps, grpl from rep_board1;
SEQ GRP GRPS GRPL
---------- ---------- ---------- ----------
362 362 0 0
363 363 0 0
BoardEx01.reply1.board_view1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%
request.setCharacterEncoding("utf-8");
String cpage = request.getParameter("cpage");
String seq = request.getParameter("seq");
String subject = "";
String writer = "";
String wdate = "";
String hit = "";
String content = "";
String mail = "";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuffer sb = new StringBuffer();
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource datasource = (DataSource)envCtx.lookup("jdbc/oracle1");
conn = datasource.getConnection();
// 조회수
String sql = "update rep_board1 set hit = hit + 1 where seq=?"; // 20180725
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, seq);
pstmt.executeUpdate();
// 조회
sql = "select subject, writer, wdate, hit, content, mail from rep_board1 where seq = ?"; // 20180725
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, seq);
rs = pstmt.executeQuery();
if(rs.next()) {
subject = rs.getString("subject");
writer = rs.getString("writer");
wdate = rs.getString("wdate");
hit = rs.getString("hit");
content = rs.getString("content") == null ? "" : rs.getString("content").replaceAll("\n", "<br />");
mail = rs.getString("mail");
}
} catch(NamingException e) {
System.out.println("[에러] " + e.getMessage());
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="../css/board_view.css">
</head>
<body>
<!-- 상단 디자인 -->
<div class="con_title">
<h3>게시판</h3>
<p>HOME > 게시판 > <strong>게시판</strong></p>
</div>
<div class="con_txt">
<div class="contents_sub">
<!--게시판-->
<div class="board_view">
<table>
<tr>
<th width="10%">제목</th>
<td width="60%"><%= subject %></td>
<th width="10%">등록일</th>
<td width="20%"><%= wdate %></td>
</tr>
<tr>
<th>글쓴이</th>
<td><%= writer %><%= '(' + mail + ')' %></td>
<th>조회</th>
<td><%= hit %></td>
</tr>
<tr>
<td colspan="4" height="200" valign="top" style="padding: 20px; line-height: 160%"><%= content %></td>
</tr>
</table>
</div>
<div class="btn_area">
<div class="align_left">
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_list1.jsp?cpage=<%=cpage %>'">목록</button>
</div>
<div class="align_right">
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_modify1.jsp?cpage=<%=cpage %>&seq=<%= seq %>'">수정</button>
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_delete1.jsp?cpage=<%=cpage %>&seq=<%= seq %>'">삭제</button>
<button type="button" class="btn_write btn_txt01" style="cursor: pointer;" onclick="location.href='board_write1.jsp?cpage=<%=cpage %>'">쓰기</button>
</div>
</div>
<!--//게시판-->
</div>
</div>
<!-- 하단 디자인 -->
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%
request.setCharacterEncoding("utf-8");
String cpage = request.getParameter("cpage");
String seq = request.getParameter("seq");
String subject = "";
String writer = "";
String wdate = "";
String hit = "";
String content = "";
String mail = "";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuffer sb = new StringBuffer();
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource datasource = (DataSource)envCtx.lookup("jdbc/oracle1");
conn = datasource.getConnection();
// 조회수
String sql = "update rep_board1 set hit = hit + 1 where seq=?"; // 20180725
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, seq);
pstmt.executeUpdate();
// 조회
sql = "select subject, writer, wdate, hit, content, mail from rep_board1 where seq = ?"; // 20180725
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, seq);
rs = pstmt.executeQuery();
if(rs.next()) {
subject = rs.getString("subject");
writer = rs.getString("writer");
wdate = rs.getString("wdate");
hit = rs.getString("hit");
content = rs.getString("content") == null ? "" : rs.getString("content").replaceAll("\n", "<br />");
mail = rs.getString("mail");
}
} catch(NamingException e) {
System.out.println("[에러] " + e.getMessage());
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="../css/board_view.css">
</head>
<body>
<!-- 상단 디자인 -->
<div class="con_title">
<h3>게시판</h3>
<p>HOME > 게시판 > <strong>게시판</strong></p>
</div>
<div class="con_txt">
<div class="contents_sub">
<!--게시판-->
<div class="board_view">
<table>
<tr>
<th width="10%">제목</th>
<td width="60%"><%= subject %></td>
<th width="10%">등록일</th>
<td width="20%"><%= wdate %></td>
</tr>
<tr>
<th>글쓴이</th>
<td><%= writer %><%= '(' + mail + ')' %></td>
<th>조회</th>
<td><%= hit %></td>
</tr>
<tr>
<td colspan="4" height="200" valign="top" style="padding: 20px; line-height: 160%"><%= content %></td>
</tr>
</table>
</div>
<div class="btn_area">
<div class="align_left">
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_list1.jsp?cpage=<%=cpage %>'">목록</button>
</div>
<div class="align_right">
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_modify1.jsp?cpage=<%=cpage %>&seq=<%= seq %>'">수정</button>
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_delete1.jsp?cpage=<%=cpage %>&seq=<%= seq %>'">삭제</button>
<button type="button" class="btn_write btn_txt01" style="cursor: pointer;" onclick="location.href='board_write1.jsp?cpage=<%=cpage %>'">새글쓰기</button>
<button type="button" class="btn_write btn_txt01" style="cursor: pointer;" onclick="location.href='board_reply1.jsp?cpage=<%=cpage %>&seq=<%= seq %>'">답글쓰기</button> <!-- 20180725 -->
</div>
</div>
<!--//게시판-->
</div>
</div>
<!-- 하단 디자인 -->
</body>
</html>
view1 밑에 댓글쓰기 버튼을 새로 만들어 주고 이것은 cpage와 seq 정보를 같이 가지고 가는 태그로 만들어 줌
BoardEx01.reply1.board_reply1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String cpage = request.getParameter("cpage");
String seq = request.getParameter("seq"); // 20180725
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="../css/board_write.css">
<script type="text/javascript">
window.onload = function() {
document.getElementById('submit1').onclick = function() {
//alert('click');
if(document.frm.info.checked == false) {
alert('동의하셔야 합니다.');
return;
}
if(document.frm.writer.value.trim() == "") {
alert('이름을 입력하셔야 합니다.');
return;
}
if(document.frm.password.value.trim() == "") {
alert('비밀번호를 입력하셔야 합니다.');
return;
}
if(document.frm.subject.value.trim() == "") {
alert('제목을 입력하셔야 합니다.');
return;
}
document.frm.submit();
};
};
</script>
</head>
<body>
<!-- 상단 디자인 -->
<div class="con_title">
<h3>게시판</h3>
<p>HOME > 게시판 > <strong>게시판</strong></p>
</div>
<div class="con_txt">
<form action="./board_reply1_ok.jsp" method="post" name="frm"> <!-- 20180725 -->
<input type="hidden" name="cpage" value="<%=cpage %>" /> <!-- 20180725 -->
<input type="hidden" name="seq" value="<%=seq %>" /> <!-- 20180725 -->
<div class="contents_sub">
<!--게시판-->
<div class="board_write">
<table>
<tr>
<th class="top">글쓴이</th>
<td class="top" colspan="3"><input type="text" name="writer" value="" class="board_view_input_mail" maxlength="5" /></td>
</tr>
<tr>
<th>제목</th>
<td colspan="3"><input type="text" name="subject" value="" class="board_view_input" /></td>
</tr>
<tr>
<th>비밀번호</th>
<td colspan="3"><input type="password" name="password" value="" class="board_view_input_mail"/></td>
</tr>
<tr>
<th>내용</th>
<td colspan="3"><textarea name="content" class="board_editor_area"></textarea></td>
</tr>
<tr>
<th>이메일</th>
<td colspan="3"><input type="text" name="mail1" value="" class="board_view_input_mail"/> @ <input type="text" name="mail2" value="" class="board_view_input_mail"/></td>
</tr>
</table>
<table>
<tr>
<br />
<td style="text-align:left;border:1px solid #e0e0e0;background-color:f9f9f9;padding:5px">
<div style="padding-top:7px;padding-bottom:5px;font-weight:bold;padding-left:7px;font-family: Gulim,Tahoma,verdana;">※ 개인정보 수집 및 이용에 관한 안내</div>
<div style="padding-left:10px;">
<div style="width:97%;height:95px;font-size:11px;letter-spacing: -0.1em;border:1px solid #c5c5c5;background-color:#fff;padding-left:14px;padding-top:7px;">
1. 수집 개인정보 항목 : 회사명, 담당자명, 메일 주소, 전화번호, 홈페이지 주소, 팩스번호, 주소 <br />
2. 개인정보의 수집 및 이용목적 : 제휴신청에 따른 본인확인 및 원활한 의사소통 경로 확보 <br />
3. 개인정보의 이용기간 : 모든 검토가 완료된 후 3개월간 이용자의 조회를 위하여 보관하며, 이후 해당정보를 지체 없이 파기합니다. <br />
4. 그 밖의 사항은 개인정보취급방침을 준수합니다.
</div>
</div>
<div style="padding-top:7px;padding-left:5px;padding-bottom:7px;font-family: Gulim,Tahoma,verdana;">
<input type="checkbox" name="info" value="1" class="input_radio"> 개인정보 수집 및 이용에 대해 동의합니다.
</div>
</td>
</tr>
</table>
</div>
<div class="btn_area">
<div class="align_left">
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_list1.jsp?cpage=<%=cpage %>'">목록</button>
</div>
<div class="align_right">
<button type="button" id="submit1" class="btn_write btn_txt01" style="cursor: pointer;">답글</button> <!-- 20180725 -->
</div>
</div>
<!--//게시판-->
</div>
</form>
</div>
<!-- 하단 디자인 -->
</body>
</html>
BoardEx01.reply1.board_reply1_ok
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %> <!-- 20180725 -->
<%@ page import="java.sql.SQLException" %>
<%
request.setCharacterEncoding("utf-8");
String cpage = request.getParameter("cpage"); // 20180725
String seq = request.getParameter("seq"); // 20180725
String subject = request.getParameter("subject");
String writer = request.getParameter("writer");
String mail = request.getParameter("mail1") + "@" + request.getParameter("mail2");
String password = request.getParameter("password");
String content = request.getParameter("content");
String wip = request.getRemoteAddr();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null; // 20180725
int flag = 1;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource datasource = (DataSource)envCtx.lookup("jdbc/oracle1");
conn = datasource.getConnection();
////////////////// 20180725 //////////////////////////////////////////////////
// 부모글
String sql = "select grp, grps, grpl from rep_board1 where seq=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, seq);
rs = pstmt.executeQuery();
int grp = 0;
int grps = 0;
int grpl = 0;
if(rs.next()) {
grp = rs.getInt("grp");
grps = rs.getInt("grps");
grpl = rs.getInt("grpl");
}
// grps - 같은 그룹내에서 글의 순서 1. 부모글의 grps보다 큰 grps 번호는 무조건 1씩 증가하고
sql = "update rep_board1 set grps = grps+1 where grp=? and grps>?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, grp);
pstmt.setInt(2, grps);
pstmt.executeUpdate();
//////////////////20180725 ////////////////////////////////////////////////////
sql = "insert into rep_board1 values (board_seq.nextval, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, sysdate)"; // 20180725
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, grp); // 20180725
pstmt.setInt(2, grps + 1); // 2. 자신은 부모글 + 1 // 20180725
pstmt.setInt(3, grpl + 1); // 1. 부모글 grpl + 1 // 20180725
pstmt.setString(4, subject); // 20180725
pstmt.setString(5, writer); // 20180725
pstmt.setString(6, mail); // 20180725
pstmt.setString(7, password); // 20180725
pstmt.setString(8, content); // 20180725
pstmt.setString(9, wip); // 20180725
int result = pstmt.executeUpdate();
if(result == 1) {
flag = 0;
}
} catch(NamingException e) {
System.out.println("[에러] " + e.getMessage());
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
out.println("<script type='text/javascript'>");
if(flag == 0) {
out.println("alert('답글 쓰기에 성공했습니다.');");
out.println("location.href='./board_list1.jsp?cpage=" + cpage + "';");
} else if(flag == 1) {
out.println("alert('답글 쓰기에 실패했습니다.');");
out.println("history.back();");
}
out.println("</script>");
%>
BoardEx01.reply1.board_list1에 또 추가
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- Java 전처리 -->
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%
request.setCharacterEncoding("utf-8");
int cpage = 1;
if(request.getParameter("cpage") != null && !request.getParameter("cpage").equals("")) {
cpage = Integer.parseInt(request.getParameter("cpage"));
}
int recordPerPage = 10;
int totalRecord = 0;
int totalPage = 1;
int blockPerPage = 5; // 한 페이지당 5개
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuffer sb = new StringBuffer();
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource datasource = (DataSource)envCtx.lookup("jdbc/oracle1");
conn = datasource.getConnection();
String sql = "select seq, grpl, subject, wri sgrpl += " ";
}
///////////////////// 20180725 ///////////////////////
sb.append("<tr>");
sb.append(" <td> </td>");
//sb.append(" <td>").append(rs.getString("seq")).append("</td>");
sb.append(" <td>" + seq + "</td>");
sb.append(" <td class='left'>");
if(grpl != 0) { // 20180725
sb.append(sgrpl + "<img src='../images/icon_re.gif'>"); // 20180725
}
sb.append(" <a href='./board_view1.jsp?cpage=" + cpage + "&seq=" + seq + "'>" + subject + "</a>");
if(wgap == 0) {
sb.append(" <img src='../images/icon_hot.gif'>");
}
sb.append(" </td>");
sb.append(" <td>" + writer + "</td>");
sb.append(" <td>" + wdate + "</td>");
sb.append(" <td>" + hit + "</td>");
sb.append(" <td> </td>");
sb.append("</tr>");
}
} catch(NamingException e) {
System.out.println("[에러] " + e.getMessage());
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="../css/board_list.css">
</head>
<body>
<!-- 상단 디자인 -->
<div class="con_title">
<h3>게시판</h3>
<p>HOME > 게시판 > <strong>게시판</strong></p>
</div>
<div class="con_txt">
<div class="contents_sub">
<div class="board_top">
<div class="bold">총 <span class="txt_orange"><%=totalRecord %></span>건</div>
</div>
<!--게시판-->
<div class="board">
<table>
<tr>
<th width="3%"> </th>
<th width="5%">번호</th>
<th>제목</th>
<th width="10%">글쓴이</th>
<th width="17%">등록일</th>
<th width="5%">조회</th>
<th width="3%"> </th>
</tr>
<tr>
<!-- 시작 -->
<%= sb %>
<!-- 끝 -->
</tr>
</table>
</div>
<!--//게시판-->
<div class="align_right">
<button type="button" class="btn_write btn_txt01" style="cursor: pointer;" onclick="location.href='board_write1.jsp?cpage=<%=cpage %>'">쓰기</button>
</div>
<!--페이지넘버-->
<div class="paginate_regular">
<div class="board_pagetab" align="absmiddle">
<%
int startBlock = ((cpage -1) / blockPerPage)*blockPerPage + 1;
int endBlock = ((cpage -1) / blockPerPage)*blockPerPage + blockPerPage;
if(endBlock >= totalPage) {
endBlock = totalPage;
}
%>
<%
if(startBlock == 1) {
out.println("<span class='on'><<</span>");
} else {
out.println("<span class='off'><a href='./board_list1.jsp?cpage=" + (startBlock - blockPerPage) +"'><< </a></span>");
out.println(" ");
}
%>
<%
if(cpage == 1) {
out.println("<span class='on'><</span>");
} else {
out.println("<span class='off'><a href='./board_list1.jsp?cpage=" + (cpage-1) + "'>< </a></span>");
out.println(" ");
}
%>
<%
for(int i=startBlock; i<=endBlock; i++) {
if(cpage == i) {
out.println("<span class='on'>[ " + i + " ]</span>");
} else {
out.println("<span class='off'><a href='./board_list1.jsp?cpage=" + i + "'>" + i + "</a></span>");
}
}
%>
<%
if(cpage == totalPage) {
out.println("<span class='on'>></span>");
} else {
out.println("<span class='off'><a href='./board_list1.jsp?cpage=" + (cpage+1) + "'>> </a></span>");
out.println(" ");
}
%>
<%
if(endBlock == totalPage) {
out.println("<span class='on'>>></span>");
} else {
out.println("<span class='off'><a href='./board_list1.jsp?cpage=" + (startBlock + blockPerPage) +"'>>> </a></span>");
out.println(" ");
}
%>
</div>
</div>
<!--//페이지넘버-->
</div>
</div>
<!--//하단 디자인 -->
</body>
</html>
반응형
'Web & Mobile > JSP' 카테고리의 다른 글
Lecture 52 - JSP(10) Model1 기반 게시판 파일 업로드 기능 구현 (0) | 2019.05.29 |
---|---|
Lecture 51 - JSP(9) 검색기능 추가한 Model1 기반 게시판 (0) | 2019.05.28 |
Lecture 49 - JSP(7) 페이지 수를 나타내는 Model1 기반 게시판 (0) | 2019.05.24 |
Lecture 48 - JSP(6) 필수요소이해, 리다이렉트, Session, Application, Buffer, pageContext, Model1기법 게시판 (0) | 2019.05.23 |
Lecture 47 - JSP(5) 게시판에 이모티콘 추가하기 (0) | 2019.05.22 |
댓글