<td align="left">
<%for(int j=0;j<depth;j++){ out.println(" ");} %>
<a href="javascript:read('<%=num %>')">
package ch15;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
public class BCommentMgr {
private DBConnectionMgr pool;
public BCommentMgr() {
pool = DBConnectionMgr.getInstance();
}
//Comment Insert (read.jsp)
public void insertBComment(BCommentBean bean) {
Connection con = null;
PreparedStatement pstmt = null;
String sql = null;
try {
con = pool.getConnection();
sql = "insert tblBComment(name,comment,regdate,num) "
+ "values(?,?,now(),?)";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, bean.getName());
pstmt.setString(2, bean.getComment());
pstmt.setInt(3, bean.getNum());
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con, pstmt);
}
}
//Comment List (read.jsp)
public Vector<BCommentBean> getBComment(int num){
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = null;
Vector<BCommentBean> vlist = new Vector<>();
try {
con = pool.getConnection();
sql = "select * from tblBComment where num = ?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, num);
rs = pstmt.executeQuery();
while(rs.next()){
BCommentBean bean = new BCommentBean();
bean.setCnum(rs.getInt("cnum"));
bean.setName(rs.getString("name"));
bean.setComment(rs.getString("comment"));
bean.setRegdate(rs.getString("regdate"));
bean.setNum(rs.getInt("num"));
vlist.addElement(bean);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con, pstmt, rs);
}
return vlist;
}
//Comment Count (list.jsp)
public int getBCommentCount(int num) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = null;
int count = 0;
try {
con = pool.getConnection();
sql = "select count(num) from tblBComment where num=?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, num);
rs = pstmt.executeQuery();
if(rs.next())
count = rs.getInt(1);
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con, pstmt, rs);
}
return count;
}
//Comment Delete (read.jsp)
public void deleteBComment(int cnum) {
Connection con = null;
PreparedStatement pstmt = null;
String sql = null;
try {
con = pool.getConnection();
sql = "delete from tblBComment where cnum=?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, cnum);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con, pstmt);
}
}
//Comment All Delete
//(게시물 원글 삭제시 - BoardDeleteServlet)
public void deleteAllBComment(int num) {
Connection con = null;
PreparedStatement pstmt = null;
String sql = null;
try {
con = pool.getConnection();
sql = "delete from tblBComment where num=?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, num);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con, pstmt);
}
}
}
package ch15;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
@WebServlet("/ch15/boardReply")
public class BoardReplyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
BoardMgr mgr = new BoardMgr();
BoardBean reBean = new BoardBean();
reBean.setName(request.getParameter("name"));
reBean.setSubject(request.getParameter("subject"));
reBean.setContent(request.getParameter("content"));
reBean.setRef(UtilMgr.parseInt(request, "ref"));
reBean.setPos(UtilMgr.parseInt(request, "pos"));
reBean.setDepth(UtilMgr.parseInt(request, "depth"));
reBean.setPass(request.getParameter("pass"));
reBean.setIp(request.getParameter("ip"));
mgr.replyUpBoard(reBean.getRef(), reBean.getPos());
mgr.replyBoard(reBean);
String nowPage = request.getParameter("nowPage");
String numPerPage = request.getParameter("numPerPage");
response.sendRedirect("list.jsp?nowPage="+nowPage+
"&numPerPage="+numPerPage);
}
}
댓글이 달리면 빨간 색으로 몇개의 댓글이 달린지 나온다
댓글은 글 밑에 표시된다
// board Reply : 답변글 입력
public void replyBoard(BoardBean bean) {
Connection con = null;
PreparedStatement pstmt = null;
String sql = null;
try {
con = pool.getConnection();
sql = "insert tblBoard(name,content,subject,ref,pos,depth,regdate,"
+ "pass,count,ip)values(?, ?, ?, ?, ?, ?, now(), ?, 0, ?)";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, bean.getName());
pstmt.setString(2, bean.getContent());
pstmt.setString(3, bean.getSubject());
////
pstmt.setInt(4, bean.getRef()); // 원글과 동일한 ref 값(그룹을 위한 값)
pstmt.setInt(5, bean.getPos()+1); // 원글 pos+1(정렬을 위한 값)
pstmt.setInt(6, bean.getDepth()+1); // 원글 depth+1
///
pstmt.setString(7, bean.getPass());
pstmt.setString(8, bean.getIp());
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con, pstmt);
}
}
// board Reply Up : 답변글 위치값 수정
public void replyUpBoard(int ref, int pos) {
Connection con = null;
PreparedStatement pstmt = null;
String sql = null;
try {
con = pool.getConnection();
sql = "update tblboard set pos=pos+1 where ref = ? and pos > ?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, ref);
pstmt.setInt(2, pos);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con, pstmt);
}
return;
}
// 댓글 기능 : insert, delete
String flag = request.getParameter("flag");
if(flag!=null){
if(flag.equals("insert")){
BCommentBean cbean = new BCommentBean();
cbean.setNum(num); // 어떤 게시물
cbean.setName(request.getParameter("cName"));
cbean.setComment(request.getParameter("comment"));
cmgr.insertBComment(cbean);
}else if(flag.equals("delete")){
cmgr.deleteBComment(UtilMgr.parseInt(request, "cnnum"));
}
}else{
//list.jsp 게시물 읽음 : 이때만 조회수 증가
mgr.upCount(num);
}
//
function cInsert() {
if(document.cFrm.comment.value==""){
alert("댓글을 입력하세요.");
document.cFrm.comment.focus();
return;
}
document.cFrm.submit();
}
//
<!-- 댓글 입력폼 Start -->
<form method="post" name="cFrm">
<table>
<tr align="center">
<td width="50">이 름</td>
<td align="left">
<input name="cName" size="10" value="aaa">
</td>
</tr>
<tr align="center">
<td>내 용</td>
<td>
<input name="comment" size="50">
<input type="button" value="등록" onclick="cInsert()"></td>
</tr>
</table>
<input type="hidden" name="flag" value="insert">
<input type="hidden" name="num" value="<%=num%>">
<input type="hidden" name="cnum">
<input type="hidden" name="nowPage" value="<%=nowPage%>">
<input type="hidden" name="numPerPage" value="<%=numPerPage%>">
<%if(!(keyWord==null||keyWord.equals(""))){ %>
<input type="hidden" name="keyField" value="<%=keyField%>">
<input type="hidden" name="keyWord" value="<%=keyWord%>">
<%}%>
</form>
<!-- 댓글 입력폼 End -->
<!-- reply.jsp -->
<%@page contentType="text/html; charset=UTF-8"%>
<!-- read.jsp에서 원글을 session 저장 -->
<jsp:useBean id="bean" scope="session" class="ch15.BoardBean"/>
<%
String nowPage = request.getParameter("nowPage");
String numPerPage = request.getParameter("numPerPage");
String subject = bean.getSubject();
String content = bean.getContent();
%>
<html>
<head>
<title>JSPBoard</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#FFFFCC">
<div align="center">
<br><br>
<table width="600" cellpadding="3">
<tr>
<td bgcolor="#CCCC00" height="21" align="center">답변하기</td>
</tr>
</table>
<form method="post" action="boardReply" >
<table width="600" cellpadding="7">
<tr>
<td>
<table>
<tr>
<td width="20%">성 명</td>
<td width="80%">
<input name="name" size="30" maxlength="20"></td>
</tr>
<tr>
<td>제 목</td>
<td>
<input name="subject" size="50" value="답변 : <%=subject%>" maxlength="50"></td>
</tr>
<tr>
<td>내 용</td>
<td>
<textarea name="content" rows="12" cols="50">
<%=content %>
========답변 글을 쓰세요.=======
</textarea>
</td>
</tr>
<tr>
<td>비밀 번호</td>
<td>
<input type="password" name="pass" size="15" maxlength="15"></td>
</tr>
<tr>
<td colspan="2" height="5"><hr/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="답변등록" >
<input type="reset" value="다시쓰기">
<input type="button" value="뒤로" onClick="history.back()"></td>
</tr>
</table>
</td>
</tr>
</table>
<input type="hidden" name="ip" value="<%=request.getRemoteAddr()%>" >
<input type="hidden" name="nowPage" value="<%=nowPage%>">
<input type="hidden" name="numPerPage" value="<%=numPerPage%>">
<input type="hidden" name="ref" value="<%=bean.getRef()%>">
<input type="hidden" name="pos" value="<%=bean.getPos()%>">
<input type="hidden" name="depth" value="<%=bean.getDepth()%>">
</form>
</div>
</body>
</html>