본문 바로가기

MVC1/게시판

게시판// 글삭제, 댓글추가, 글 detail

bbslist.jsp

<%!
//댓글의 depth와 image를 추가하는 함수 생성
public String arrow(int depth){//depth=1 ->' '(한칸),  depth=2 -> '  '(두칸),  0이면 기본글로 추가아무것도안함 
	String rs = "<img src='./image/arrow.png' width='20px' height='20px' />";
	String nbsp = "&nbsp;&nbsp;&nbsp;&nbsp;";
	
	String ts = "";
	for(int i = 0; i < depth; i++){
		ts += nbsp;
	}
	return depth==0?"":ts+rs;
}

%>
<%
	Object ologin = session.getAttribute("login");
	MemberDto mem = null;
	
	if(ologin == null){//session이 날라갔을 때(시간경과 등)
		%>
		<script type="text/javascript">
		alert("로그인 해 주십시오");
		location.href = "login.jsp";
		</script>		
		<%
	}
	mem = (MemberDto)ologin;
	BbsDao dao = BbsDao.getInstance();
	List<BbsDto> list = dao.getBbsList();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style type="text/css">
table{
	border-collapse: collapse;
	/* max-width: 100%; */
	
}
.container {
  margin-left: auto;
  margin-right: auto;
  padding-left: 10px;
  padding-right: 10px;
}
 .table-header {
    background-color: #95A5A6;
    font-size: 14px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
  .table-row {
    background-color: #ffffff;
    box-shadow: 0px 0px 9px 0px rgba(0,0,0,0.3);
}
.h4{
	background-color: #f0f0f0;
	 text-align: right;
}
.wri{
	text-align: right;
}
.btn {        
    font-size: 16px;
    font-weight: bold;
	background: gray;
	border: none;
    outline: none !important;
    color: white;
    border-radius: 2px;
    border-radius: 100px;
}
.div{
	text-align: center;
}
</style>
</head>
<body>

<h4 class="h4" >
	환영합니다 <%=mem.getId() %>님<a href="login.jsp">&nbsp;&nbsp;로그아웃</a>
</h4>
<h1 align="center">게시판</h1>
<!-- <a href="bbswrite.jsp" id="btn" class="wri">글쓰기</a> -->
<a href="bbswrite.jsp">글쓰기</a>
<div align="center" class="container">
<table border="1">
<col width="70"><col width="600"><col width="60"><col width="100"><col width="100">
<tr class="table-header">
	<th>번호</th><th>제목</th><th>조회수</th><th>작성자</th><th>작성일</th>
</tr>
<%
	if(list == null || list.size()==0){//list가 없거나 글이 없을 때
		%>
		<tr>
			<td colspan="3">작성된 글이 없습니다</td>
		</tr>
		<%
	}else{
		
		for(int i = 0; i < list.size(); i++){
			BbsDto bbs = list.get(i);
		%>
		<tr class="table-row">
			<th><%=i+1 %></th>
			
			<%-- <td>
				<%=arrow(bbs.getDepth()) %><!-- 여백+이미지 -->
				<a href="bbsdetail.jsp?seq=<%=bbs.getSeq() %>">
					<%=bbs.getTitle() %>
				</a>
			</td> --%>
		<td>
			<%
			if(bbs.getDel() == 0){
				%>
				<%=arrow( bbs.getDepth() ) %>			
				<a href="bbsdetail.jsp?seq=<%=bbs.getSeq() %>">
					<%=bbs.getTitle() %>
				</a>	
				<%
			}else{
				%>		
				<font color="#ff0000">삭제된 글입니다</font> 
				<%
			}
			%>
		</td>  
			<td align="center">
				<%=bbs.getReadcount() %>
			</td>
			<td align="center">
				<%=bbs.getId() %>
			</td>
			<td align="center">
				<%=bbs.getWdate() %>
			</td>
		</tr>
		<%
		}
	}
%>
</table>
</div>
<div class="div">
<form action="bbssearch.jsp" method="get">
	<select id="search" name="search">
		<option value="title">제목</option>
		<option value="content">내용</option>
		<option value="id">작성자</option>
	</select>
	<%
	
	%>
		<input type="text" id="text" name="text" placeholder="검색어를 입력해주세요">
		<button type="submit" class="btn">검색</button>
</form>
</div>
<script type="text/javascript">
function write() {
	location.href = "bbswrite.jsp";
}
</script>
</body>
</html>

 

 

 

 

bbsdetail.jsp

<%
	MemberDto mem = (MemberDto)request.getSession().getAttribute("login");
%>
<%
	String seq = request.getParameter("seq");
	System.out.println(seq);
	
	BbsDao dao = BbsDao.getInstance();
	dao.readcount(Integer.parseInt(seq));
	BbsDto dto = dao.getBbs(Integer.parseInt(seq));

%>    
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login하기</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="http://lab.alexcican.com/set_cookies/cookie.js" type="text/javascript" ></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
<style>
table{
	border-collapse: collapse;
	
	/* max-width: 100%; */
	
}
.container {
  margin-left: auto;
  margin-right: auto;
  padding-left: 10px;
  padding-right: 10px;
}
 .table-header {
    background-color: #95A5A6;
    font-size: 14px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    height: 40px;
    font-size: 16px;
}
  .table-row {
    background-color: #ffffff;
    box-shadow: 0px 0px 9px 0px rgba(0,0,0,0.3);
}
.h2{
	background-color: #f0f0f0;
	 text-align: center;
}
.wri{
	text-align: right;
}
.btn {        
    font-size: 16px;
    font-weight: bold;
	background: gray;
	border: none;
    color: white;
    border-radius: 2px;
    
}
</style>
</head>
<body>
<h2 class="h2">작성 글 정보</h2>

<div align="center" class="container">
<table border="1">
	<tr>
		<th class="table-header">작성자</th><th><%=dto.getId() %></th>
	</tr>
	<tr>
		<th class="table-header">작성일</th><th><%=dto.getWdate() %></th>
	</tr>
	<tr>
		<th class="table-header">조회수</th><th><%=dto.getReadcount() %></th>
	</tr>
	<tr >
		<th class="table-header">정보</th><th><%=dto.getRef() %>-<%=dto.getStep() %>-<%=dto.getDepth() %></th>
	</tr>
	<tr>
		<th class="table-header">제목</th><th><%=dto.getTitle() %></th>
	</tr>
	<tr>
		<th class="table-header">내용</th><th><textarea class="form-area" id="area" name="area" cols="80" rows="30" placeholder="내용을 입력해주세요" required="required"><%=dto.getContent() %></textarea></th>
	</tr>
</table>
<%
if(dto.getId().equals(mem.getId())){//작성자와 로그인id가 같을때만 수정하게해라(다를경우 수정,삭제 버튼 비공개)
	%>
	<button type="button" onclick="updateBbs(<%=dto.getSeq() %>)" class="btn">수정</button>
	<button type="button" onclick="deleteBbs(<%=dto.getSeq() %>)" class="btn">삭제</button>
	<%
}
%>
<!-- 댓글방법1  --><button type="button" onclick="answerBbs(<%=dto.getSeq()%>)" class="btn">댓글 작성하기</button>
<!-- 댓글방법2 
<form action="answer.jsp" method="get">
	<input type="hidden" name="seq" value="<%-- <%=bbs.getSeq()%> --%>">
	<input type="submit" value="댓글">
</form>-->
</div>

<script type="text/javascript">
function updateBbs() {
	location.href = "bbsupdate.jsp?seq=<%=dto.getSeq()%>";
}

function deleteBbs() {
	location.href = "bbsdelete.jsp?seq=<%=dto.getSeq()%>";	

}
function answerBbs() {
	location.href = "answer.jsp?seq=<%=dto.getSeq()%>";	

}
</script>
</body>

 

 

 

 

 

answer.jsp

<%
	String sseq = request.getParameter("seq");
	int seq = Integer.parseInt(sseq);
	
	Object ologin = session.getAttribute("login");
	MemberDto mem = (MemberDto)ologin;

%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style type="text/css">
table{
	border-collapse: collapse;
	
	/* max-width: 100%; */
	
}
.container {
  margin-left: auto;
  margin-right: auto;
  padding-left: 10px;
  padding-right: 10px;
}
 .table-header {
    background-color: #95A5A6;
    font-size: 14px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    height: 40px;
    font-size: 16px;
}
  .table-row {
    background-color: #ffffff;
    box-shadow: 0px 0px 9px 0px rgba(0,0,0,0.3);
}
.h2{
	background-color: #f0f0f0;
	 text-align: center;
}
.wri{
	text-align: right;
}
.btn {        
    font-size: 16px;
    font-weight: bold;
	background: gray;
	border: none;
    color: white;
    border-radius: 2px;
    width: 100%
}
</style>
</head>
<body>
<%
	BbsDao dao = BbsDao.getInstance();
	BbsDto parentBbs = dao.getBbs(seq);
%>
<!-- 
form 2개, table 2개

기본글:
	작성자, 제목, 작성일, 조회수, 정보, 내용
	
댓글:
	로그인ID(session id 정보), 제목, 내용
 -->
 <h2 class="h2">기본 글</h2>
 <div align="center">
 <table border="1">
<!-- <col width="70"><col width="600"> -->
	<tr>
		<th class="table-header">작성자</th><td><%=parentBbs.getId() %></td>
	</tr>
	<tr>
		<th class="table-header">작성일</th><td><%=parentBbs.getWdate() %></td>
	</tr>
	<tr>
		<th class="table-header">조회수</th><td><%=parentBbs.getReadcount() %></td>
	</tr>
	<tr >
		<th class="table-header">정보</th><td><%=parentBbs.getDepth() %></td>
	</tr>
	<tr>
		<th class="table-header">제목</th><td><%=parentBbs.getTitle() %></td>
	</tr>
	<tr>
		<th class="table-header">내용</th><td>
		<textarea class="form-area" id="area" name="area" cols="80" rows="20" placeholder="내용을 입력해주세요" required="required"><%=parentBbs.getContent() %></textarea></td>
	</tr>
</table>
</div>

<h2 class="h2">답글</h2>
<div align="center">
<form action="answerAf.jsp" method="post">
<input type="hidden" name="seq" value="<%=parentBbs.getSeq()%>">
 <table border="1">
 
<!-- <col width="70"><col width="600"> -->
	<tr>
		<th class="table-header">작성자</th><td><input type="text" name="id" readonly="readonly" size="100%"
			value="<%=mem.getId() %>"></td>
	</tr>

	<tr>
		<th class="table-header" >제목</th><td><input type="text" name="title" width="100%"></td>
	</tr>
	<tr>
		<th class="table-header">내용</th>
		<td><textarea class="form-area" id="area" name="content" cols="80" rows="5" placeholder="내용을 입력해주세요" required="required"></textarea></td>
	</tr>
	<tr>
		<th colspan="2"><button type="submit" class="btn">댓글 추가</button></th>
	</tr>
</table>
</div>
</form>
</body>

 

 

 

 

answerAf.jsp

<%
request.setCharacterEncoding("utf-8");

int seq = Integer.parseInt(request.getParameter("seq"));
String id = request.getParameter("id");
String title = request.getParameter("title");
String content = request.getParameter("content");

System.out.println("seq:"+seq+" id:"+id+" title:"+title+" content:"+content);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>answerAf.jsp</title>
</head>
<body>
<%
	BbsDao dao = BbsDao.getInstance();
	boolean isS = dao.answer(seq, new BbsDto(id, title, content));
	
	if(isS){
		%>
		<script type="text/javascript">
		alert("답글이 입력되었습니다");
		location.href = "bbslist.jsp";
		</script>
		<%
		}else{
		 %>
		<script type="text/javascript">
		alert("답글입력이 실패되었습니다");
		location.href = "bbslist.jsp";
		</script>
		<%
	}
%>
</body>

 

 

 

 

 

bbsdelete.jsp

<%
	String sseq = request.getParameter("seq");
	int seq = Integer.parseInt(sseq);
	System.out.println(seq);
	
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	BbsDao dao = BbsDao.getInstance();
	boolean isS = dao.deleteBbs(seq);
	
	if(isS){
		%>
		<script type="text/javascript">
		alert("삭제 되었습니다");
		location.href = "bbslist.jsp";
		</script>
		<%
	}else{
		%>
		<script type="text/javascript">
		alert("실패되었습니다");
		location.href = "bbslist.jsp";
		</script>
		<%
	}
%>
</body>

 

 

 

 

 

 

BbsDao.java

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import db.DBClose;
import db.DBConnection;
import dto.BbsDto;

public class BbsDao {
	private static BbsDao dao = new BbsDao();
	
	public BbsDao() {
		DBConnection.initConnection();
	}
	public static BbsDao getInstance() {
		return dao;
	}
	
	//(글 목록) LIST 가져오기
	public List<BbsDto> getBbsList() {
		
		String sql = " SELECT SEQ, ID, REF, STEP, DEPTH,"
					+ " TITLE, CONTENT, WDATE,"
					+ " DEL, READCOUNT "
					+ " FROM BBS "
					+ " ORDER BY REF DESC, STEP ASC ";
		
		Connection conn = null;
		PreparedStatement psmt = null;
		ResultSet rs = null;
		
		List<BbsDto> list = new ArrayList<BbsDto>();
		
		try {
			conn = DBConnection.getConnection();
			System.out.println("1/6 getBbsList success");

			psmt = conn.prepareStatement(sql);
			System.out.println("2/6 getBbsList success");

			rs = psmt.executeQuery();
			System.out.println("3/6 getBbsList success");

			while(rs.next()) {
				int i = 1;
				BbsDto dto = new BbsDto(rs.getInt(i++), 
										rs.getString(i++), 
										rs.getInt(i++), 
										rs.getInt(i++), 
										rs.getInt(i++), 
										rs.getString(i++), 
										rs.getString(i++), 
										rs.getString(i++), 
										rs.getInt(i++), 
										rs.getInt(i++));
				list.add(dto);
			}
			System.out.println("4/6 getBbsList success");

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBClose.close(psmt, conn, rs);
		}
		return list;
	}

	
	//글 추가 하기
	public boolean writeBbs(BbsDto dto) {
		//첫글 게시 할 때 REF의 최고값이 0이므로 +1, SEQ와 값이 같아짐
		String sql = " INSERT INTO BBS "
					+ " (SEQ, ID, REF, STEP, DEPTH, "
					+ " TITLE, CONTENT, WDATE, "
					+ " DEL, READCOUNT) "
					+ " VALUES( SEQ_BBS.NEXTVAL, ?, "
							+ " (SELECT NVL(MAX(REF), 0)+1 FROM BBS), 0, 0, "
							+ " ?, ?, SYSDATE, "
							+ " 0, 0) ";
		
		Connection conn = null;
		PreparedStatement psmt = null;
		
		int count = 0;
		
		try {
			conn = DBConnection.getConnection();
			System.out.println("1/6 writeBbs success");
			
			psmt = conn.prepareStatement(sql);
			psmt.setString(1, dto.getId());
			psmt.setString(2, dto.getTitle());
			psmt.setString(3, dto.getContent());
			System.out.println("2/6 writeBbs success");

			count = psmt.executeUpdate();
			System.out.println("3/6 writeBbs success");

			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBClose.close(psmt, conn, null);
		}
		return count>0?true:false;
	}
	
	
	//seq로 dto가져오기
	public BbsDto getBbs(int seq) {
		
		String sql = " SELECT SEQ, ID, REF, STEP, DEPTH,"
				+ " TITLE, CONTENT, WDATE,"
				+ " DEL, READCOUNT "
				+ " FROM BBS "
				+ " WHERE SEQ=? ";
		
		Connection conn = null;
		PreparedStatement psmt = null;
		ResultSet rs = null;
		
		BbsDto dto = null;
		
		try {
			conn = DBConnection.getConnection();
			
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, seq);
			
			rs = psmt.executeQuery();
			
			if (rs.next()) {
				dto = new BbsDto();
				dto.setSeq(rs.getInt("SEQ"));
				dto.setId(rs.getString("ID"));
				dto.setRef(rs.getInt("REF"));
				dto.setStep(rs.getInt("STEP"));
				dto.setDepth(rs.getInt("DEPTH"));
				dto.setTitle(rs.getString("TITLE"));
				dto.setContent(rs.getString("CONTENT"));
				dto.setWdate(rs.getString("WDATE"));
				dto.setDel(rs.getInt("DEL"));
				dto.setReadcount(rs.getInt("READCOUNT"));
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBClose.close(psmt, conn, rs);
		}
		return dto;
	}
	
	
	//seq로 조회수 가져오기
	public void readcount(int seq) {
		String sql = " UPDATE BBS "
				+ " SET READCOUNT=READCOUNT+1 "
				+ " WHERE SEQ=? ";
		
		Connection conn = null;
		PreparedStatement psmt = null;
		
		try {
			conn = DBConnection.getConnection();
			System.out.println("1/6 readcount success");
				
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, seq);			
			System.out.println("2/6 readcount success");
			
			psmt.executeUpdate();
			System.out.println("3/6 readcount success");

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBClose.close(psmt, conn, null);
		}
	}
	
	
	//댓글 추가
	public boolean answer(int seq, BbsDto bbs) {
		//update : (단 BBS의 REF와 같고, 현재 STEP보다 큰 STEP들을)STEP+1
		String sql1 = " UPDATE BBS "
					+ " SET STEP=STEP+1 "
					+ " WHERE REF=(SELECT REF FROM BBS WHERE SEQ=? ) "
					+ " 	AND STEP > (SELECT STEP FROM BBS WHERE SEQ=? )";
		
		//insert : SEQ에 맞는 REF STEP DEPTH
		String sql2 = " INSERT INTO BBS "
					+ " (SEQ, ID, "
					+ " REF, STEP, DEPTH, "
					+ " TITLE, CONTENT, WDATE, DEL, READCOUNT) "
					+ " VALUES(SEQ_BBS.NEXTVAL, ?, "
					+ " 	(SELECT REF FROM BBS WHERE SEQ=?), "
					+ "		(SELECT STEP FROM BBS WHERE SEQ=?) + 1, "
					+ "		(SELECT DEPTH FROM BBS WHERE SEQ=?) + 1, "
					+ "		?, ?, SYSDATE, 0, 0) ";
		
		Connection conn = null;
		PreparedStatement psmt = null;
		
		int count = 0;
		
		try {
			conn = DBConnection.getConnection();
			conn.setAutoCommit(false);//먼저 적용시키지 마라(sql 2개이상 적용할떄)
			System.out.println("1/6 readcount success");

			//update sql1대입
			psmt = conn.prepareStatement(sql1);
			psmt.setInt(1, seq);
			psmt.setInt(2, seq);
			System.out.println("2/6 readcount success");

			count = psmt.executeUpdate();
			System.out.println("3/6 readcount success");
			
			//psmt초기화 (sql문을 2개째 실행전 )
			psmt.clearParameters();
			
			//insert sql2대입
			psmt = conn.prepareStatement(sql2);
			
			psmt.setString(1, bbs.getId());
			psmt.setInt(2, seq);
			psmt.setInt(3, seq);
			psmt.setInt(4, seq);
			psmt.setString(5, bbs.getTitle());
			psmt.setString(6, bbs.getContent());
			System.out.println("4/6 readcount success");
			
			count = psmt.executeUpdate();
			System.out.println("5/6 readcount success");

			conn.commit();
			
			
		} catch (Exception e) {
			System.out.println();
			e.printStackTrace();
			
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}finally {
			
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		DBClose.close(psmt, conn, null);
		System.out.println("6/6 readcount success");

		}
		return count>0?true:false;
		
		
	}
	
	
	//게시글 삭제
	public boolean deleteBbs(int seq) {
		String sql = " UPDATE BBS SET DEL=1 "
					+ " WHERE SEQ=?";
					
		Connection conn = null;
		PreparedStatement psmt = null;
		int count = 0;
		
		try {
			conn = DBConnection.getConnection();
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, seq);
			count = psmt.executeUpdate();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBClose.close(psmt, conn, null);
		}
		return count>0?true:false;
	}
	
	
}

 

 

'MVC1 > 게시판' 카테고리의 다른 글

게시판// 페이징 처리  (0) 2020.07.30
게시판// 업데이트  (0) 2020.07.30
게시판// select search 검색  (0) 2020.07.30
게시판 list/ 글쓰기  (0) 2020.07.28