-------------------답변폼 조회----------------------
시작 : show_content.jsp 답변링크 클릭. command : reply_form
Ctr : ReplyFormController.execute()
BoardService : getContentByNOForForm(int no) : BoardDTO
BoardDAO : selectContentByNO(int no) : BoardDTO
응답 : reply_form.jsp
---------------------------답변처리-----------------
시작 : reply_form.jsp 답변버튼 클릭. command : reply_content
Ctr : ReplyContentController.execute()
BoardService : replyContent(BoardDTO)
BoardDAO : updateRestep(int refamily, int restep)
selectBoardNO()
insertContent()
응답 : show_content.jsp
-------------------답변폼 조회----------------------
시작 : show_content.jsp 답변링크 클릭. command : reply_form
Ctr : ReplyFormController.execute()
BoardService : getContentByNOForForm(int no) : BoardDTO
BoardDAO : selectContentByNO(int no) : BoardDTO
응답 : reply_form.jsp
<%@page import="board.dto.BoardDTO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>글내용</title>
<style type="text/css">
div{
width:600px;
border:1px solid gray;
padding : 5px;
}
#title{
font-weight: bold;
background-color: yellow;
}
#info{
font-size:13px;
}
#content{
min-height:300px;
height:auto;
}
</style>
</head>
<body>
<h1>글목록</h1>
<p>
<div id="title">${requestScope.board.no}.${requestScope.board.title }</div>
<div id="info">작성자 : ${requestScope.board.writer } | 조회수 : ${requestScope.board.viewcount } 작성일시 : ${requestScope.board.writedate }</div>
<div id="content">${requestScope.board.content }</div>
<p>
<a href="/${initParam.context_root }/write_form.jsp">글쓰기</a>
<a href="/${initParam.context_root }/boardController?command=list_all">전체 글목록</a>
<a href="">글목록(페이징)</a>
<a href="/${initParam.context_root }/boardController?command=modify_form&no=${requestScope.board.no}">글수정</a>
<a href="/${initParam.context_root }/boardController?command=delete_content&no=${requestScope.board.no}">글삭제</a>
<a href="/${initParam.context_root }/boardController?command=reply_form&no=${requestScope.board.no}">답변</a>
</body>
</html>
package board.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import board.dto.ForwardDTO;
public class BoardFrontController extends HttpServlet {
private static final long serialVersionUID = 1L;
public BoardFrontController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 공통 사전 작업 - 한글처리
request.setCharacterEncoding("utf-8");
//2. Controller 로직 처리 - Controller객체.execute()호출
String command = request.getParameter("command");
Controller ctr = ControllerCommandMapping.getController(command);
ForwardDTO fdto = ctr.execute(request, response);
//3. 공통 사후 작업
if(fdto.isRedirect()){
response.sendRedirect(fdto.getUrl());
}else{
RequestDispatcher rdp = request.getRequestDispatcher(fdto.getUrl());
rdp.forward(request, response);
}
}
}
package board.controller;
public class ControllerCommandMapping {
public static Controller getController(String command){
Controller ctr = null;
if(command.equals("write_content")){
ctr = new WriteContentController();
}else if(command.equals("list_all")){
ctr = new BoardAllListController();
}else if(command.equals("get_content")){
ctr = new GetContentController();
}else if(command.equals("modify_form")){
ctr = new ModifyFormController();
}else if(command.equals("modify_content")){
ctr = new ModifyContentController();
}else if(command.equals("delete_content")){
ctr = new DeleteContentController();
}else if(command.equals("reply_form")){
ctr = new ReplyFormController();
}
return ctr;
}
}
package board.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import board.dto.BoardDTO;
import board.dto.ForwardDTO;
import board.model.service.BoardService;
public class ReplyFormController implements Controller {
@Override
public ForwardDTO execute(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
ForwardDTO forwardDTO = null;
try {
int no = Integer.parseInt(request.getParameter("no"));
BoardService boardService = BoardService.getInstance();
BoardDTO dto = boardService.getContentByNOForForm(no);
request.setAttribute("dto",dto);
forwardDTO = new ForwardDTO("/reply_form.jsp",false);
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("error_message", e.getMessage());
forwardDTO = new ForwardDTO("/error.jsp",false);
}
return forwardDTO;
}
}
package board.model.service;
import java.sql.SQLException;
import java.util.ArrayList;
import board.dto.BoardDTO;
import board.model.dao.BoardDAO;
import board.util.Utilities;
public class BoardService {
private static BoardService instance = new BoardService();
private BoardDAO dao;
private BoardService(){
dao = BoardDAO.getInstance();
}
public static BoardService getInstance(){
return instance;
}
public void writeContent(BoardDTO bto)throws SQLException{
/*
* 1. bto에 property 설정 - no, writedate, content
* no - sequence의 값 - dao.selectBoardNO();
* writedate - Utilities.getNow();
* content를 DB에 넣는 용으로 변경 Utilities.changeContentForDB();
* bdto의 refamily에 no 값을 설정
* 2. dao.insertContent(bto);
* 3. 날짜 형태를 년.월.일 시:분:초 형태로 변경 - bto의 property 변경
* Utilities.changeDayTimeFormat();
*/
//1
int no = dao.selectBoardNo();
String writedate = Utilities.getNow();
bto.setNo(no);
bto.setWritedate(writedate);
bto.setContent(Utilities.changeContentForDB(bto.getContent()));
bto.setRefamily(no);
//2
dao.insertContent(bto);
//3
bto.setWritedate(Utilities.changeDayTimeFormat(writedate));
}
//글목록
public ArrayList<BoardDTO> getBoardAllList() throws SQLException{
//1. dao로 부터 전체 글내용을 ArrayList로 조회
ArrayList<BoardDTO> list_all = dao.selectBoardAllList();
//2. list내의 BoardDTO객체들의 wrtiedate를 변경
for(int i=0;i<list_all.size();i++){
list_all.get(i).setWritedate(Utilities.changeDayFormat(list_all.get(i).getWritedate()));
}
return list_all;
}
public BoardDTO getContentByNO(int no) throws SQLException{
//1.조회수 증가
dao.updateViewCount(no);
//2. no로 글 정보 조회
BoardDTO bto = dao.selectContentByNO(no);
//2-1 writedate를 yyy.MM.dd HH:mm:ss 형식으로 변경
bto.setWritedate(Utilities.changeDayTimeFormat(bto.getWritedate()));
return bto;
}
public BoardDTO getContentByNOForForm(int no)throws SQLException{
//1. no값으로 글 조회
BoardDTO bto = dao.selectContentByNO(no);
//2. content를 textarea용으로 변경(DB용으로 바뀌어 있는 것을 HTML용으로 변경)
bto.setContent(Utilities.changeContentForTextArea(bto.getContent()));
System.out.println("가져올때-----------\n"+bto.getContent());
return bto;
}
public void modifyContent(BoardDTO bto)throws SQLException {
//1. property변경 - content, writedate
bto.setWritedate(Utilities.getNow());
bto.setContent(Utilities.changeContentForDB(bto.getContent()));
//2. 수정처리
dao.updateContent(bto);
//3. 날짜 포멧 변경
bto.setWritedate(Utilities.changeDayTimeFormat(Utilities.getNow()));
}
public void deleteContentByNO(int no)throws SQLException{
dao.deleteContentByNO(no);
}
}
package board.model.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import board.dto.BoardDTO;
import board.util.DBUtil;
public class BoardDAO {
private static BoardDAO instance = new BoardDAO();
private DBUtil dbUtil;
private BoardDAO() {
dbUtil = DBUtil.getInstance();
}
public static BoardDAO getInstance() {
return instance;
}
public int selectBoardNo() throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset = null;
String sql = "select board_no_seq.nextval from dual";
int no = 0;
try {
BasicDataSource ds = dbUtil.getDataSource();
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql);
rset = pstmt.executeQuery();
if (rset.next()) {
no = rset.getInt(1);
}
} finally {
dbUtil.close(conn, pstmt, rset);
}
return no;
}
public void insertContent(BoardDTO bto) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "insert into board (no, title, writer, content, writedate, viewcount, refamily, restep, relevel) values(?,?,?,?,?,0,?,?,?)";
try {
BasicDataSource ds = dbUtil.getDataSource();
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, bto.getNo());
pstmt.setString(2, bto.getTitle());
pstmt.setString(3, bto.getWriter());
pstmt.setString(4, bto.getContent());
pstmt.setString(5, bto.getWritedate());
//viewcount값은 처음 등록하면 무조건 0이라서 쿼리에서 부터 그냥 값을 넣어줬다
// pstmt.setInt(6, bto.getViewcount());
pstmt.setInt(6, bto.getRefamily());
pstmt.setInt(7, bto.getRestep());
pstmt.setInt(8, bto.getRelevel());
int cnt = pstmt.executeUpdate();
System.out.println(cnt + "개의 행이 삽입되었습니다.");
} finally {
dbUtil.close(conn, pstmt);
}
}
public ArrayList<BoardDTO> selectBoardAllList() throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset = null;
String sql = "select no, title, writer, content, writedate, viewcount, refamily, restep, relevel from board order by refamily desc, restep asc";
ArrayList<BoardDTO> list_all = new ArrayList<BoardDTO>();
try{
BasicDataSource dataSource = dbUtil.getDataSource();
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
rset = pstmt.executeQuery();
while(rset.next()){
list_all.add(new BoardDTO(rset.getInt(1), rset.getString(2), rset.getString(3), rset.getString(4), rset.getString(5), rset.getInt(6), rset.getInt(7), rset.getInt(8), rset.getInt(9)));
}
}finally{
dbUtil.close(conn, pstmt, rset);
}
return list_all;
}
public BoardDTO selectContentByNO(int no) throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset = null;
String sql = "select no, title, writer, content, writedate, viewcount, refamily, restep, relevel from board where no=?";
BoardDTO bto = null;
try{
BasicDataSource dataSource = dbUtil.getDataSource();
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, no);
rset = pstmt.executeQuery();
if(rset.next()){
bto = new BoardDTO(rset.getInt(1), rset.getString(2), rset.getString(3), rset.getString(4), rset.getString(5), rset.getInt(6), rset.getInt(7), rset.getInt(8), rset.getInt(9));
}
}finally{
dbUtil.close(conn, pstmt, rset);
}
return bto;
}
public void updateViewCount(int no) throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "update board set viewcount=viewcount+1 where no=?";
try{
BasicDataSource dataSource = dbUtil.getDataSource();
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, no);
int cnt = pstmt.executeUpdate();
System.out.println(cnt+"개의 행이 수정되었습니다.");
}finally{
dbUtil.close(conn, pstmt);
}
}
public void updateContent(BoardDTO dto) throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "update board set title=?, writer=?, content=?, writedate=? where no=?";
try{
BasicDataSource dataSource = dbUtil.getDataSource();
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, dto.getTitle());
pstmt.setString(2, dto.getWriter());
pstmt.setString(3, dto.getContent());
pstmt.setString(4, dto.getWritedate());
pstmt.setInt(5, dto.getNo());
int cnt = pstmt.executeUpdate();
System.out.println(cnt+"개의 행이 수정되었습니다.");
}finally{
dbUtil.close(conn, pstmt);
}
}
public void deleteContentByNO(int no) throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "delete from board where no=?";
try{
BasicDataSource dataSource = dbUtil.getDataSource();
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, no);
int cnt = pstmt.executeUpdate();
System.out.println(cnt+"개의 행이 삭제되었습니다.");
}finally{
dbUtil.close(conn, pstmt);
}
}
public void updateRestep(int refamily, int restep) throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "update board set restep=restep+1 where refamily=? and restep>?";
try{
BasicDataSource dataSource = dbUtil.getDataSource();
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, refamily);
pstmt.setInt(2, restep);
int cnt = pstmt.executeUpdate();
System.out.println(cnt+"개의 행이 수정되었습니다.");
}finally{
dbUtil.close(conn, pstmt);
}
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>글 수정</title>
</head>
<body>
<h2>글 답변 폼</h2><p>
${requestScope.dto.no }번 답변글
<form action="/${initParam.context_root }/boardController" method="post">
<input type="hidden" name="refamily" value="${requestScope.dto.refamily }">
<input type="hidden" name="restep" value="${requestScope.dto.restep }">
<input type="hidden" name="relevel" value="${requestScope.dto.relevel }">
<input type="hidden" name="command" value="reply_content">
<table width="500px">
<tr>
<td width="100px">제목</td><td><input type="text" name="title" size="50" value="RE : ${requestScope.dto.title }"></td>
</tr>
<tr>
<td>작성자</td><td><input type="text" name="writer" size="10" maxlength="10" value="${requestScope.dto.writer }"></td>
</tr>
<tr>
<td colspan="2"><textarea cols="50" rows="10" name="content">
${requestScope.dto.content }
----------------답변-----------------
</textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="답변">
<input type="reset" value="초기화">
</td>
</tr>
</table>
</form>
</body>
</html>
* 결과보기
---------------------------답변처리-----------------
시작 : reply_form.jsp 답변버튼 클릭. command : reply_content
Ctr : ReplyContentController.execute()
BoardService : replyContent(BoardDTO)
BoardDAO : updateRestep(int refamily, int restep)
selectBoardNO()
insertContent()
응답 : show_content.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>글 수정</title>
</head>
<body>
<h2>글 답변 폼</h2><p>
${requestScope.dto.no }번 답변글
<form action="/${initParam.context_root }/boardController" method="post">
<input type="hidden" name="refamily" value="${requestScope.dto.refamily }">
<input type="hidden" name="restep" value="${requestScope.dto.restep }">
<input type="hidden" name="relevel" value="${requestScope.dto.relevel }">
<input type="hidden" name="command" value="reply_content">
<table width="500px">
<tr>
<td width="100px">제목</td><td><input type="text" name="title" size="50" value="RE : ${requestScope.dto.title }"></td>
</tr>
<tr>
<td>작성자</td><td><input type="text" name="writer" size="10" maxlength="10" value="${requestScope.dto.writer }"></td>
</tr>
<tr>
<td colspan="2"><textarea cols="50" rows="10" name="content">
${requestScope.dto.content }
----------------답변-----------------
</textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="답변">
<input type="reset" value="초기화">
</td>
</tr>
</table>
</form>
</body>
</html>
package board.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import board.dto.ForwardDTO;
public class BoardFrontController extends HttpServlet {
private static final long serialVersionUID = 1L;
public BoardFrontController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 공통 사전 작업 - 한글처리
request.setCharacterEncoding("utf-8");
//2. Controller 로직 처리 - Controller객체.execute()호출
String command = request.getParameter("command");
Controller ctr = ControllerCommandMapping.getController(command);
ForwardDTO fdto = ctr.execute(request, response);
//3. 공통 사후 작업
if(fdto.isRedirect()){
response.sendRedirect(fdto.getUrl());
}else{
RequestDispatcher rdp = request.getRequestDispatcher(fdto.getUrl());
rdp.forward(request, response);
}
}
}
package board.controller;
public class ControllerCommandMapping {
public static Controller getController(String command){
Controller ctr = null;
if(command.equals("write_content")){
ctr = new WriteContentController();
}else if(command.equals("list_all")){
ctr = new BoardAllListController();
}else if(command.equals("get_content")){
ctr = new GetContentController();
}else if(command.equals("modify_form")){
ctr = new ModifyFormController();
}else if(command.equals("modify_content")){
ctr = new ModifyContentController();
}else if(command.equals("delete_content")){
ctr = new DeleteContentController();
}else if(command.equals("reply_form")){
ctr = new ReplyFormController();
}else if(command.equals("reply_content")){
ctr = new ReplyContentController();
}
return ctr;
}
}
package board.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import board.dto.BoardDTO;
import board.dto.ForwardDTO;
import board.model.service.BoardService;
public class ReplyContentController implements Controller {
@Override
public ForwardDTO execute(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
ForwardDTO forwardDTO = null;
try {
//1, client가 보낸 요청 파라미터 조회
String title = request.getParameter("title");
String writer = request.getParameter("writer");
String content = request.getParameter("content");
int refamily = Integer.parseInt(request.getParameter("refamily"));
int restep = Integer.parseInt(request.getParameter("restep"));
int relevel = Integer.parseInt(request.getParameter("relevel"));
//글쓴일자하고 no,와 viewcount는 서비스에서 다시 생성할것이기 때문에 디폴트 값을 넣었다.
BoardDTO bto = new BoardDTO(0,title, writer, content,null,0,refamily,restep,relevel);
//2. 비지니스 로직 - Model(Business Service)로 요청
BoardService boardService = BoardService.getInstance();
boardService.replyContent(bto);
request.setAttribute("board",bto);
forwardDTO = new ForwardDTO("/show_content.jsp",false);
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("error_message", e.getMessage());
forwardDTO = new ForwardDTO("/error.jsp",false);
}
return forwardDTO;
}
}
package board.model.service;
import java.sql.SQLException;
import java.util.ArrayList;
import board.dto.BoardDTO;
import board.model.dao.BoardDAO;
import board.util.Utilities;
public class BoardService {
private static BoardService instance = new BoardService();
private BoardDAO dao;
private BoardService(){
dao = BoardDAO.getInstance();
}
public static BoardService getInstance(){
return instance;
}
public void writeContent(BoardDTO bto)throws SQLException{
/*
* 1. bto에 property 설정 - no, writedate, content
* no - sequence의 값 - dao.selectBoardNO();
* writedate - Utilities.getNow();
* content를 DB에 넣는 용으로 변경 Utilities.changeContentForDB();
* bdto의 refamily에 no 값을 설정
* 2. dao.insertContent(bto);
* 3. 날짜 형태를 년.월.일 시:분:초 형태로 변경 - bto의 property 변경
* Utilities.changeDayTimeFormat();
*/
//1
int no = dao.selectBoardNo();
String writedate = Utilities.getNow();
bto.setNo(no);
bto.setWritedate(writedate);
bto.setContent(Utilities.changeContentForDB(bto.getContent()));
bto.setRefamily(no);
//2
dao.insertContent(bto);
//3
bto.setWritedate(Utilities.changeDayTimeFormat(writedate));
}
//글목록
public ArrayList<BoardDTO> getBoardAllList() throws SQLException{
//1. dao로 부터 전체 글내용을 ArrayList로 조회
ArrayList<BoardDTO> list_all = dao.selectBoardAllList();
//2. list내의 BoardDTO객체들의 wrtiedate를 변경
for(int i=0;i<list_all.size();i++){
list_all.get(i).setWritedate(Utilities.changeDayFormat(list_all.get(i).getWritedate()));
}
return list_all;
}
public BoardDTO getContentByNO(int no) throws SQLException{
//1.조회수 증가
dao.updateViewCount(no);
//2. no로 글 정보 조회
BoardDTO bto = dao.selectContentByNO(no);
//2-1 writedate를 yyy.MM.dd HH:mm:ss 형식으로 변경
bto.setWritedate(Utilities.changeDayTimeFormat(bto.getWritedate()));
return bto;
}
public BoardDTO getContentByNOForForm(int no)throws SQLException{
//1. no값으로 글 조회
BoardDTO bto = dao.selectContentByNO(no);
//2. content를 textarea용으로 변경(DB용으로 바뀌어 있는 것을 HTML용으로 변경)
bto.setContent(Utilities.changeContentForTextArea(bto.getContent()));
System.out.println("가져올때-----------\n"+bto.getContent());
return bto;
}
public void modifyContent(BoardDTO bto)throws SQLException {
//1. property변경 - content, writedate
bto.setWritedate(Utilities.getNow());
bto.setContent(Utilities.changeContentForDB(bto.getContent()));
//2. 수정처리
dao.updateContent(bto);
//3. 날짜 포멧 변경
bto.setWritedate(Utilities.changeDayTimeFormat(Utilities.getNow()));
}
public void deleteContentByNO(int no)throws SQLException{
dao.deleteContentByNO(no);
}
public void replyContent(BoardDTO bto)throws SQLException {
//1. DB에 같은 refamily의 restep들을 1씩 증가 시킨다.
dao.updateRestep(bto.getRefamily(),bto.getRestep());
//2. bdto의 restep, relevel을 1 증가 시킨다.
bto.setRestep(bto.getRestep()+1);
bto.setRelevel(bto.getRelevel()+1);
//3. insert할 글 번호를 가져온뒤 bto에 넣는다.
int no = dao.selectBoardNo();
//4. BoardDTO에 no, writeDate와 content는 DB 저장용으로 변경한다.
bto.setNo(no);
bto.setWritedate(Utilities.getNow());
bto.setContent(Utilities.changeContentForDB(bto.getContent()));
//5. 새글 등록 - dao.insertContent()이용
dao.insertContent(bto);
//6. 날짜 형태 show_content.jsp에서 보여주도록 년.월.일 시:분:초 로 변경
bto.setWritedate(Utilities.changeDayTimeFormat(Utilities.getNow()));
}
}
package board.model.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import board.dto.BoardDTO;
import board.util.DBUtil;
public class BoardDAO {
private static BoardDAO instance = new BoardDAO();
private DBUtil dbUtil;
private BoardDAO() {
dbUtil = DBUtil.getInstance();
}
public static BoardDAO getInstance() {
return instance;
}
public int selectBoardNo() throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset = null;
String sql = "select board_no_seq.nextval from dual";
int no = 0;
try {
BasicDataSource ds = dbUtil.getDataSource();
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql);
rset = pstmt.executeQuery();
if (rset.next()) {
no = rset.getInt(1);
}
} finally {
dbUtil.close(conn, pstmt, rset);
}
return no;
}
public void insertContent(BoardDTO bto) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "insert into board (no, title, writer, content, writedate, viewcount, refamily, restep, relevel) values(?,?,?,?,?,0,?,?,?)";
try {
BasicDataSource ds = dbUtil.getDataSource();
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, bto.getNo());
pstmt.setString(2, bto.getTitle());
pstmt.setString(3, bto.getWriter());
pstmt.setString(4, bto.getContent());
pstmt.setString(5, bto.getWritedate());
//viewcount값은 처음 등록하면 무조건 0이라서 쿼리에서 부터 그냥 값을 넣어줬다
// pstmt.setInt(6, bto.getViewcount());
pstmt.setInt(6, bto.getRefamily());
pstmt.setInt(7, bto.getRestep());
pstmt.setInt(8, bto.getRelevel());
int cnt = pstmt.executeUpdate();
System.out.println(cnt + "개의 행이 삽입되었습니다.");
} finally {
dbUtil.close(conn, pstmt);
}
}
public ArrayList<BoardDTO> selectBoardAllList() throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset = null;
String sql = "select no, title, writer, content, writedate, viewcount, refamily, restep, relevel from board order by refamily desc, restep asc";
ArrayList<BoardDTO> list_all = new ArrayList<BoardDTO>();
try{
BasicDataSource dataSource = dbUtil.getDataSource();
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
rset = pstmt.executeQuery();
while(rset.next()){
list_all.add(new BoardDTO(rset.getInt(1), rset.getString(2), rset.getString(3), rset.getString(4), rset.getString(5), rset.getInt(6), rset.getInt(7), rset.getInt(8), rset.getInt(9)));
}
}finally{
dbUtil.close(conn, pstmt, rset);
}
return list_all;
}
public BoardDTO selectContentByNO(int no) throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset = null;
String sql = "select no, title, writer, content, writedate, viewcount, refamily, restep, relevel from board where no=?";
BoardDTO bto = null;
try{
BasicDataSource dataSource = dbUtil.getDataSource();
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, no);
rset = pstmt.executeQuery();
if(rset.next()){
bto = new BoardDTO(rset.getInt(1), rset.getString(2), rset.getString(3), rset.getString(4), rset.getString(5), rset.getInt(6), rset.getInt(7), rset.getInt(8), rset.getInt(9));
}
}finally{
dbUtil.close(conn, pstmt, rset);
}
return bto;
}
public void updateViewCount(int no) throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "update board set viewcount=viewcount+1 where no=?";
try{
BasicDataSource dataSource = dbUtil.getDataSource();
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, no);
int cnt = pstmt.executeUpdate();
System.out.println(cnt+"개의 행이 수정되었습니다.");
}finally{
dbUtil.close(conn, pstmt);
}
}
public void updateContent(BoardDTO dto) throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "update board set title=?, writer=?, content=?, writedate=? where no=?";
try{
BasicDataSource dataSource = dbUtil.getDataSource();
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, dto.getTitle());
pstmt.setString(2, dto.getWriter());
pstmt.setString(3, dto.getContent());
pstmt.setString(4, dto.getWritedate());
pstmt.setInt(5, dto.getNo());
int cnt = pstmt.executeUpdate();
System.out.println(cnt+"개의 행이 수정되었습니다.");
}finally{
dbUtil.close(conn, pstmt);
}
}
public void deleteContentByNO(int no) throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "delete from board where no=?";
try{
BasicDataSource dataSource = dbUtil.getDataSource();
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, no);
int cnt = pstmt.executeUpdate();
System.out.println(cnt+"개의 행이 삭제되었습니다.");
}finally{
dbUtil.close(conn, pstmt);
}
}
public void updateRestep(int refamily, int restep) throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "update board set restep=restep+1 where refamily=? and restep>?";
try{
BasicDataSource dataSource = dbUtil.getDataSource();
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, refamily);
pstmt.setInt(2, restep);
int cnt = pstmt.executeUpdate();
System.out.println(cnt+"개의 행이 수정되었습니다.");
}finally{
dbUtil.close(conn, pstmt);
}
}
}
<%@page import="board.dto.BoardDTO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>글내용</title>
<style type="text/css">
div{
width:600px;
border:1px solid gray;
padding : 5px;
}
#title{
font-weight: bold;
background-color: yellow;
}
#info{
font-size:13px;
}
#content{
min-height:300px;
height:auto;
}
</style>
</head>
<body>
<h1>글목록</h1>
<p>
<div id="title">${requestScope.board.no}.${requestScope.board.title }</div>
<div id="info">작성자 : ${requestScope.board.writer } | 조회수 : ${requestScope.board.viewcount } 작성일시 : ${requestScope.board.writedate }</div>
<div id="content">${requestScope.board.content }</div>
<p>
<a href="/${initParam.context_root }/write_form.jsp">글쓰기</a>
<a href="/${initParam.context_root }/boardController?command=list_all">전체 글목록</a>
<a href="">글목록(페이징)</a>
<a href="/${initParam.context_root }/boardController?command=modify_form&no=${requestScope.board.no}">글수정</a>
<a href="/${initParam.context_root }/boardController?command=delete_content&no=${requestScope.board.no}">글삭제</a>
<a href="/${initParam.context_root }/boardController?command=reply_form&no=${requestScope.board.no}">답변</a>
</body>
</html>
* 결과보기
'프로그래밍 > JSP Programming' 카테고리의 다른 글
2012-5-20 MVC를 이용한 게시판 만들기[처음~끝 완성본] (0) | 2012.05.20 |
---|---|
게시판 소스 (0) | 2012.05.18 |
2012-5-18 MVC패턴을 활용한 게시판 글삭제하기 (0) | 2012.05.18 |
2012-5-17 게시판 글수정하기 (0) | 2012.05.17 |
2012-5-17 게시판 글번호로 조회 (0) | 2012.05.17 |