1) Member 완성해오기
2) Board 만들어오기
2번째 Board 관련 코드입니다.
BoardDTO.java
import org.springframework.stereotype.Component;
@Component("boardDTO")
public class BoardDTO {
private int board_num;
private String board_title;
private String board_context;
private String board_writer;
private String board_date;
public int getBoard_num() {
return board_num;
}
public void setBoard_num(int board_num) {
this.board_num = board_num;
}
public String getBoard_title() {
return board_title;
}
public void setBoard_title(String board_title) {
this.board_title = board_title;
}
public String getBoard_context() {
return board_context;
}
public void setBoard_context(String board_context) {
this.board_context = board_context;
}
public String getBoard_writer() {
return board_writer;
}
public void setBoard_writer(String board_writer) {
this.board_writer = board_writer;
}
public String getBoard_date() {
return board_date;
}
public void setBoard_date(String board_date) {
this.board_date = board_date;
}
@Override
public String toString() {
return "BoardDTO [board_num=" + board_num + ", board_title=" + board_title + ", board_context=" + board_context
+ ", board_writer=" + board_writer + ", board_date=" + board_date + "]";
}
}
BoardDAO.java
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import com.nsd.app.biz.common.JDBCUtil;
@Repository
public class BoardDAO implements BoardService{
private final String SQL_ALL = "SELECT BOARD_NUM, BOARD_TITLE, BOARD_CONTEXT, BOARD_DATE, BOARD_WRITER FROM BOARD";
private final String SQL_ONE = "SELECT BOARD_NUM, BOARD_TITLE, BOARD_CONTEXT, BOARD_DATE, BOARD_WRITER FROM BOARD WHERE BOARD_NUM = ?";
private final String SQL_INSERT = "INSERT INTO BOARD (BOARD_TITLE, BOARD_CONTEXT,BOARD_WRITER) VALUES (?,?,?)";
private final String SQL_UPDATE = "";
private final String SQL_DELETE = "";
@Autowired
private BoardDTO member;
@Override
public List<BoardDTO> SelectAll(BoardDTO boardDTO) {
Connection conn=JDBCUtil.connect();
PreparedStatement pstmt=null;
List<BoardDTO> datas = new ArrayList<>();
try {
pstmt=conn.prepareStatement(SQL_ALL);
ResultSet rs=pstmt.executeQuery();
while(rs.next()) {
this.member = new BoardDTO();
this.member.setBoard_num(rs.getInt("BOARD_NUM"));
this.member.setBoard_title(rs.getString("BOARD_TITLE"));
this.member.setBoard_context(rs.getString("BOARD_CONTEXT"));
this.member.setBoard_date(rs.getString("BOARD_DATE"));
this.member.setBoard_writer(rs.getString("BOARD_WRITER"));
datas.add(this.member);
}
} catch (SQLException e) {
System.out.println("SQL문 실패");
}
JDBCUtil.disconnect(pstmt,conn);
return datas;
}
@Override
public BoardDTO SelectOne(BoardDTO boardDTO) {
Connection conn=JDBCUtil.connect();
PreparedStatement pstmt=null;
try {
pstmt=conn.prepareStatement(SQL_ONE);
pstmt.setInt(1, boardDTO.getBoard_num());
ResultSet rs=pstmt.executeQuery();
if(rs.next()) {
this.member = new BoardDTO();
this.member.setBoard_num(rs.getInt("BOARD_NUM"));
this.member.setBoard_title(rs.getString("BOARD_TITLE"));
this.member.setBoard_context(rs.getString("BOARD_CONTEXT"));
this.member.setBoard_date(rs.getString("BOARD_DATE"));
this.member.setBoard_writer(rs.getString("BOARD_WRITER"));
}
} catch (SQLException e) {
System.out.println("SQL문 실패");
}
JDBCUtil.disconnect(pstmt,conn);
return this.member;
}
@Override
public boolean insert(BoardDTO boardDTO) {
Connection conn=JDBCUtil.connect();
PreparedStatement pstmt=null;
boolean flag=false;
try {
pstmt=conn.prepareStatement(SQL_INSERT);
pstmt.setString(1, boardDTO.getBoard_title());
pstmt.setString(2, boardDTO.getBoard_context());
pstmt.setString(3, boardDTO.getBoard_writer());
int update=pstmt.executeUpdate();
flag = true;
if(update <= 0) {
System.out.println("업데이트 실패");
return flag;
}
} catch (SQLException e) {
System.out.println("Insert SQL문 실패");
}
JDBCUtil.disconnect(pstmt,conn);
return flag;
}
@Override
public boolean update(BoardDTO boardDTO) {
return false;
}
@Override
public boolean delete(BoardDTO boardDTO) {
return false;
}
}
BoardService.java
import java.util.List;
public interface BoardService {
List<BoardDTO> SelectAll(BoardDTO boardDTO);
BoardDTO SelectOne(BoardDTO boardDTO);
boolean insert(BoardDTO boardDTO);
boolean update(BoardDTO boardDTO);
boolean delete(BoardDTO boardDTO);
}
BoardServiceImpl.java
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("boardService")
public class BoardServiceImpl implements BoardService {
@Autowired
private BoardDAO boardDAO;
@Override
public List<BoardDTO> SelectAll(BoardDTO memberDTO) {
return this.boardDAO.SelectAll(memberDTO);
}
@Override
public BoardDTO SelectOne(BoardDTO memberDTO) {
return this.boardDAO.SelectOne(memberDTO);
}
@Override
public boolean insert(BoardDTO memberDTO) {
return this.boardDAO.insert(memberDTO);
}
@Override
public boolean update(BoardDTO memberDTO) {
return this.boardDAO.update(memberDTO);
}
@Override
public boolean delete(BoardDTO memberDTO) {
return this.boardDAO.delete(memberDTO);
}
}
MainAction.java
import java.util.List;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Component;
import com.nsd.app.biz.model.board.BoardDTO;
import com.nsd.app.biz.model.board.BoardService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@Component("mainPageAction")
public class MainAction implements Action {
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) {
AbstractApplicationContext factory = new GenericXmlApplicationContext("applicationContext.xml");
ActionForward forward = (ActionForward)factory.getBean("forward");
BoardService mes = (BoardService)factory.getBean("boardService");
BoardDTO boardDTO = (BoardDTO)factory.getBean("boardDTO");
List<BoardDTO> datas = mes.SelectAll(boardDTO);
System.out.println(datas);
request.setAttribute("datas", datas);
factory.close();
forward.setPath("main.jsp");
forward.setRedirect(false);
return forward;
}
}
BoardViewPage.java
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Component;
import com.nsd.app.biz.common.Action;
import com.nsd.app.biz.common.ActionForward;
import com.nsd.app.biz.model.board.BoardDTO;
import com.nsd.app.biz.model.board.BoardService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@Component("boardViewPage")
public class BoardViewPage implements Action{
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) {
AbstractApplicationContext factory = new GenericXmlApplicationContext("applicationContext.xml");
ActionForward forward = (ActionForward)factory.getBean("forward");
BoardService bs = (BoardService)factory.getBean("boardService");
BoardDTO data = (BoardDTO)factory.getBean("boardDTO");
int board_num = Integer.parseInt(request.getParameter("board_num"));
data.setBoard_num(board_num);
data = bs.SelectOne(data);
request.setAttribute("data", data);
forward.setPath("view.jsp");
forward.setRedirect(false);
return forward;
}
}
BoardWriterPageAction.java
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Component;
import com.nsd.app.biz.common.Action;
import com.nsd.app.biz.common.ActionForward;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@Component("boardWriterPage")
public class BoardWriterPageAction implements Action{
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) {
AbstractApplicationContext factory = new GenericXmlApplicationContext("applicationContext.xml");
ActionForward forward = (ActionForward)factory.getBean("forward");
forward.setPath("write.jsp");
forward.setRedirect(true);
return forward;
}
}
BoardWriterAction.java
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Component;
import com.nsd.app.biz.common.Action;
import com.nsd.app.biz.common.ActionForward;
import com.nsd.app.biz.model.board.BoardDTO;
import com.nsd.app.biz.model.board.BoardService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
@Component("writerAction")
public class BoardWriterAction implements Action{
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) {
AbstractApplicationContext factory = new GenericXmlApplicationContext("applicationContext.xml");
ActionForward forward = (ActionForward)factory.getBean("forward");
HttpSession session = request.getSession();
String mid = (String)session.getAttribute("mid");
if(mid != null) {
BoardService bs = (BoardService)factory.getBean("boardService");
BoardDTO data = (BoardDTO)factory.getBean("boardDTO");
data.setBoard_title(request.getParameter("board_title"));
data.setBoard_context(request.getParameter("board_context"));
data.setBoard_writer(mid);
boolean flag = bs.insert(data);
if(flag) {
request.setAttribute("error", "success");
request.setAttribute("title", "글작성 성공!");
request.setAttribute("msg", "글작성에 성공하였습니다.");
request.setAttribute("path", "main.do");
}
else {
request.setAttribute("error", "error");
request.setAttribute("title", "글작성 실패!");
request.setAttribute("msg", "작성을 실패했습니다. \n 관리자에게 문의해주세요.");
request.setAttribute("path", "main.do");
}
}
else {
request.setAttribute("error", "error");
request.setAttribute("title", "로그인해주세요");
request.setAttribute("msg", "로그인 후 이용 가능한 서비스 입니다.");
request.setAttribute("path", "login.jsp");
}
forward.setPath("info.jsp");
forward.setRedirect(false);
return forward;
}
}
728x90
'국비 내용 정리 > 과제' 카테고리의 다른 글
중간 프로젝트 Spring 이관작업 (1) | 2024.10.09 |
---|---|
Spring 기본 @(어노테이션) 활용 (0) | 2024.10.08 |
[Spring] Page 작성 과제(MEMBER 관련) (0) | 2024.10.05 |
국비 34일차 과제 (0) | 2024.08.19 |
jSP 쇼핑몰 제작(page : controller) (0) | 2024.08.11 |