<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="jspstudy.domain.BoardVo" %>
<% BoardVo bv = (BoardVo)request.getAttribute("bv");%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>게시판 내용보기</h1>
<table border=1 style="width:800px;">
<tr>
<td style="width:250px">제목 (날짜 : <%=bv.getWriteday().substring(0,10) %>)</td>
<td><%=bv.getSubject() %></td>
</tr>
<tr>
<td>내용</td>
<td style="height:100px;">
<%=bv.getContent() %>
<img src="<%=request.getContextPath()%>/images/<%=bv.getFilename()%>">
<a href="<%=request.getContextPath()%>/board/fileDownload.do?filename=<%=bv.getFilename()%>"><%=bv.getFilename()%></a>
</td>
</tr>
<tr>
<td>작성자</td>
<td><%=bv.getWriter() %></td>
</tr>
<tr>
<td colspan=2 style="text-align:right;">
<input type="button" name="modify" value="수정" onclick="location.href='<%=request.getContextPath()%>/board/boardModify.do?bidx=<%=bv.getBidx()%>'">
<input type="button" name="delete" value="삭제" onclick="location.href='<%=request.getContextPath()%>/board/boardDelete.do?bidx=<%=bv.getBidx()%>'">
<input type="button" name="reply" value="답변" onclick="location.href='<%=request.getContextPath()%>/board/boardReply.do?bidx=<%=bv.getBidx()%>&originbidx=<%=bv.getOriginbidx()%>&depth=<%=bv.getDepth()%>&level_=<%=bv.getLevel_()%>'">
<input type="button" name="list" value="목록" onclick="location.href='<%=request.getContextPath()%>/board/boardList.do'">
</td>
</tr>
</table>
</body>
</html>
package jspstudy.service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import jspstudy.dbconn.Dbconn;
import jspstudy.domain.BoardVo;
import jspstudy.domain.Criteria;
import jspstudy.domain.SearchCriteria;
public class BoardDao {
private Connection conn;
private PreparedStatement pstmt;
public BoardDao() {
Dbconn db = new Dbconn();
this.conn = db.getConnection();
}
public int insertBoard(String subject,String content,String writer,String ip, int midx,String fileName) {
int value= 0;
//String sql="INSERT INTO b_board(bidx,originbidx,depth,level_,subject,content,writer,ip,midx,filename)"
// + " VALUES(BIDX_B_SEQ.NEXTVAL,bidx_b_seq.nextval,0,0,?,?,?,?,?,?)";
String sql="INSERT INTO b_board(originbidx,depth,level_,subject,content,writer,ip,midx,filename)"
+ " select max(bidx)+1,0,0,?,?,?,?,?,? from b_board ";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, subject);
pstmt.setString(2, content);
pstmt.setString(3, writer);
pstmt.setString(4, ip);
pstmt.setInt(5, midx);
pstmt.setString(6, fileName);
value = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return value;
}
public ArrayList<BoardVo> boardSelectAll(SearchCriteria scri){
ArrayList<BoardVo> alist = new ArrayList<BoardVo>();
ResultSet rs = null;
String str = "";
if (scri.getSearchType().equals("subject")) {
str = "and subject like ?";
}else {
str = "and writer like ?";
}
String sql = "select * from b_board where delyn='N' "+str+" order by originbidx desc, depth ASC limit ?,? ";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%"+scri.getKeyword()+"%" );
pstmt.setInt(2, (scri.getPage()-1)*15);
pstmt.setInt(3, 15);
rs = pstmt.executeQuery();
//rs.next() 다음값이 존재하면 true이고 그 행으로 커서가 이동하는 메소드
while(rs.next()) {
BoardVo bv = new BoardVo();
bv.setBidx(rs.getInt("bidx"));
bv.setSubject(rs.getString("subject"));
bv.setWriter(rs.getString("writer"));
bv.setWriteday(rs.getString("writeday"));
bv.setLevel_(rs.getInt("level_"));
alist.add(bv);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return alist;
}
public BoardVo boardSelectOne(int bidx) {
BoardVo bv = null;
ResultSet rs= null;
String sql="select * from b_board where bidx=?";
try {
pstmt = conn.prepareStatement(sql); //쿼리화 시킴
pstmt.setInt(1, bidx);
rs = pstmt.executeQuery();
if(rs.next()) { //다음값이 존재하면 true 커서는 다음행으로 이동
bv = new BoardVo();
bv.setBidx(rs.getInt("bidx")); //rs에 담겨져있는 데이터를 bv에 옮겨담는다
bv.setOriginbidx(rs.getInt("originbidx"));
bv.setDepth(rs.getInt("depth"));
bv.setLevel_(rs.getInt("level_"));
bv.setSubject(rs.getString("subject"));
bv.setContent(rs.getString("content"));
bv.setWriter(rs.getString("writer"));
bv.setWriteday(rs.getString("writeday"));
bv.setFilename(rs.getString("filename"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return bv;
}
public int updateBoard(String subject,String content,String writer,String ip,int midx,int bidx) {
int value=0;
String sql="update b_board set subject=?, content=?, writer=?, ip=?, midx=?, writeday=now() where bidx=?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, subject);
pstmt.setString(2, content);
pstmt.setString(3, writer);
pstmt.setString(4, ip);
pstmt.setInt(5, midx);
pstmt.setInt(6, bidx);
value= pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return value;
}
public int deleteBoard(int bidx) {
int value=0;
String sql="update b_board set delyn='Y', writeday= now() where bidx=?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, bidx);
value= pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return value;
}
public int replyBoard(BoardVo bv) {
int value= 0;
String sql1= "update b_board set depth = depth+1 where originbidx=? and depth >?";
String sql2="insert into b_board(originbidx,depth,level_,subject,content,writer,ip,midx)"
+ " VALUES(?,?,?,?,?,?,?,?)";
try {
conn.setAutoCommit(false); //자동커밋 기능 끔
pstmt = conn.prepareStatement(sql1);
pstmt.setInt(1, bv.getOriginbidx());
pstmt.setInt(2, bv.getDepth());
pstmt.executeUpdate();
pstmt = conn.prepareStatement(sql2);
pstmt.setInt(1, bv.getOriginbidx());
pstmt.setInt(2, bv.getDepth()+1);
pstmt.setInt(3, bv.getLevel_()+1);
pstmt.setString(4, bv.getSubject());
pstmt.setString(5, bv.getContent());
pstmt.setString(6, bv.getWriter());
pstmt.setString(7, bv.getIp());
pstmt.setInt(8, bv.getMidx());
value = pstmt.executeUpdate();
conn.commit();
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} finally {
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return value;
}
public int boardTotal(SearchCriteria scri) {
int cnt= 0;
ResultSet rs = null;
String str = "";
if (scri.getSearchType().equals("subject")) {
str = "and subject like ?";
}else {
str = "and writer like ?";
}
String sql="select count(*) as cnt from b_board where delyn='N' "+str+" ";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%"+scri.getKeyword()+"%");
rs = pstmt.executeQuery();
if (rs.next()) {
cnt = rs.getInt("cnt");
}
} catch (SQLException e) {
e.printStackTrace();
}
return cnt;
}
}
}else if (command.equals("/board/boardReply.do")) {
String bidx = request.getParameter("bidx");
String originbidx = request.getParameter("originbidx");
String depth = request.getParameter("depth");
String level_ = request.getParameter("level_");
BoardVo bv = new BoardVo();
bv.setBidx(Integer.parseInt(bidx));
bv.setOriginbidx(Integer.parseInt(originbidx));
bv.setDepth(Integer.parseInt(depth));
bv.setLevel_(Integer.parseInt(level_));
request.setAttribute("bv", bv);
RequestDispatcher rd = request.getRequestDispatcher("/board/boardReply.jsp");
rd.forward(request, response);
} else if (command.equals("/board/boardReplyAction.do")) {
String bidx = request.getParameter("bidx");
String originbidx = request.getParameter("originbidx");
String depth = request.getParameter("depth");
String level_ = request.getParameter("level_");
String subject = request.getParameter("subject");
String content = request.getParameter("content");
String writer = request.getParameter("writer");
String ip = InetAddress.getLocalHost().getHostAddress();
HttpSession session = request.getSession();
int midx = (int)session.getAttribute("midx");
BoardVo bv = new BoardVo();
bv.setBidx(Integer.parseInt(bidx));
bv.setOriginbidx(Integer.parseInt(originbidx));
bv.setDepth(Integer.parseInt(depth));
bv.setLevel_(Integer.parseInt(level_));
bv.setSubject(subject);
bv.setContent(content);
bv.setWriter(writer);
bv.setIp(ip);
bv.setMidx(midx);
BoardDao bd = new BoardDao();
int value = bd.replyBoard(bv);
if (value ==1) {
response.sendRedirect(request.getContextPath()+"/board/boardList.do");
}else {
response.sendRedirect(request.getContextPath()+"/board/boardContent.do?bidx="+bidx);
}
public int replyBoard(BoardVo bv) {
int value= 0;
String sql1= "update b_board set depth = depth+1 where originbidx=? and depth >?";
String sql2="insert into b_board(originbidx,depth,level_,subject,content,writer,ip,midx)"
+ " VALUES(?,?,?,?,?,?,?,?)";
try {
conn.setAutoCommit(false); //자동커밋 기능 끔
pstmt = conn.prepareStatement(sql1);
pstmt.setInt(1, bv.getOriginbidx());
pstmt.setInt(2, bv.getDepth());
pstmt.executeUpdate();
pstmt = conn.prepareStatement(sql2);
pstmt.setInt(1, bv.getOriginbidx());
pstmt.setInt(2, bv.getDepth()+1);
pstmt.setInt(3, bv.getLevel_()+1);
pstmt.setString(4, bv.getSubject());
pstmt.setString(5, bv.getContent());
pstmt.setString(6, bv.getWriter());
pstmt.setString(7, bv.getIp());
pstmt.setInt(8, bv.getMidx());
value = pstmt.executeUpdate();
conn.commit();
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} finally {
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return value;
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="jspstudy.domain.BoardVo" %>
<% BoardVo bv = (BoardVo)request.getAttribute("bv"); %>
<%
if (session.getAttribute("midx") == null){
out.println("<script>alert('로그인해주세요');location.href='"+request.getContextPath()+"/member/memberLogin.do'</script>");
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function check(){
var fm = document.frm;
if (fm.subject.value==""){
alert("제목을 입력해주세요");
fm.subject.focus();
return;
}else if (fm.content.value==""){
alert("내용을 입력해주세요");
fm.content.focus();
return;
}else if (fm.writer.value==""){
alert("작성자를 입력해주세요");
fm.writer.focus();
return;
}
fm.action = "<%=request.getContextPath()%>/board/boardReplyAction.do";
fm.method = "post";
fm.submit();
return;
}
</script>
</head>
<body>
<h1>게시판 답변하기</h1>
<table border=1 style="width:800px;">
<form name="frm">
<input type="hidden" name="bidx" value="<%=bv.getBidx() %>">
<input type="hidden" name="originbidx" value="<%=bv.getOriginbidx() %>">
<input type="hidden" name="depth" value="<%=bv.getDepth() %>">
<input type="hidden" name="level_" value="<%=bv.getLevel_() %>">
<tr>
<td style="width:100px">제목</td>
<td><input type="text" name="subject" size="50"></td>
</tr>
<tr>
<td>내용</td>
<td>
<textarea name="content" placeholder="내용을 입력해주세요" cols="80" rows="10">
</textarea>
</td>
</tr>
<tr>
<td>작성자</td>
<td><input type="text" name="writer" size="50"></td>
</tr>
<tr>
<td colspan=2 style="text-align:center;">
<input type="button" name="btn" value="확인" onclick="check();">
<input type="reset" name="reset" value="리셋">
</td>
</tr>
</form>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList" %>
<%@page import="jspstudy.domain.*" %>
<% ArrayList<BoardVo> alist = (ArrayList<BoardVo>)request.getAttribute("alist");
PageMaker pm = (PageMaker)request.getAttribute("pm");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
게시판 리스트
<form name="frm" action="<%=request.getContextPath() %>/board/boardList.do" method="post">
<table border=0 style="width:800px;text-align:right">
<tr>
<td style="width:620px;">
<select name="searchType">
<option value="subject">제목</option>
<option value="writer">작성자</option>
</select>
</td>
<td>
<input type="text" name="keyword" size ="10">
</td>
<td>
<input type="submit" name="submit" value="검색">
</td>
</tr>
</table>
</form>
<table border="1" style="width:800px">
<tr style="color:green;">
<td style="width:100px;">bidx번호</td>
<td style="width:400px;">제목</td>
<td style="width:100px;">작성자</td>
<td style="width:200px;">작성일</td>
</tr>
<% for (BoardVo bv : alist) { %>
<tr>
<td><%=bv.getBidx() %></td>
<td>
<%
for (int i=1; i<= bv.getLevel_(); i++) {
out.println(" ");
if (i == bv.getLevel_()){
out.println("ㄴ");
}
}
%>
<a href="<%=request.getContextPath()%>/board/boardContent.do?bidx=<%=bv.getBidx()%>"><%=bv.getSubject() %></a>
</td>
<td><%=bv.getWriter() %></td>
<td><%=bv.getWriteday() %></td>
</tr>
<% } %>
</table>
<table style="width:800px;text-align:center;">
<tr>
<td style="width:200px;text-align:right;">
<%
String keyword = pm.getScri().getKeyword();
String searchType= pm.getScri().getSearchType();
if (pm.isPrev() == true){
out.println("<a href='"+request.getContextPath()+"/board/boardList.do?page="+(pm.getStartPage()-1)+"&keyword="+keyword+"&searchType="+searchType+"'>◀</a>");
}
%>
</td>
<td>
<%
for (int i =pm.getStartPage(); i<=pm.getEndPage();i++){
out.println("<a href='"+request.getContextPath()+"/board/boardList.do?page="+i+"&keyword="+keyword+"&searchType="+searchType+"'>"+i+"</a>");
}
%>
</td>
<td style="width:200px;text-align:left;">
<%
if (pm.isNext()&&pm.getEndPage() >0){
out.println("<a href='"+request.getContextPath()+"/board/boardList.do?page="+(pm.getEndPage()+1)+"&keyword="+keyword+"&searchType="+searchType+"'>▶</a>");
}
%>
</td>
</tr>
</table>
</body>
</html>
'기존 > 🏀Jsp' 카테고리의 다른 글
[JSP-3편](oracle) 회원가입 구현하기 dao (jsp회원가입) (0) | 2022.07.20 |
---|---|
[JSP-3편](mysql) 회원가입 구현하기 dao (0) | 2022.07.20 |
[JSP-2편](oracle) web.xml mapping 설정 (0) | 2022.07.20 |
[JSP-2편](mysql) web.xml mapping 설정 (0) | 2022.07.20 |
[JSP-1편](oracle) db 연결 jar 파일 포함 (0) | 2022.07.20 |