[계산 BoardController.java]
package edu.global.board.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import edu.global.board.command.*;
@WebServlet("*.do")
public class BoardController extends HttpServlet {
private static final long serialVersionUID = 1L;
public BoardController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
actionDo(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
actionDo(request, response);
}
private void actionDo(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("actionDo() ..");
request.setCharacterEncoding("UTF-8");
String viewPage = null;
BCommand command = null;
String uri = request.getRequestURI();
String contextPath = request.getContextPath();
String com = uri.substring(contextPath.length());
// servlet_kdb_board/list.do
// contextPath:/servlet_kdb_board = OS에서 나온 개념
// com:/list.do
System.out.println("uri:" + uri);
System.out.println("contextPath:" + contextPath);
System.out.println("com:" + com);
if (com.equals("/list.do")) { // list.do로 들어오면 BListCommand 게시판의 글을 가져옴
command = new BListCommand();
command.execute(request, response); // 게시글 가져오기
viewPage = "list.jsp"; // dispatcher의 request객체와 response객체를 forward함
} else if (com.equals("/content_view.do")) { // http://localhost:8282/servlet_kdb_board/content_view.do?bid=4
command = new BContentCommand();
command.execute(request, response);
viewPage = "content_view.jsp";
} else if (com.equals("/modify.do")) { // http://localhost:8282/servlet_kdb_board/modify.do
command = new BModifyCommand();
command.execute(request, response);
viewPage = "list.do"; // 수정 후 list 화면으로 이동
} else if (com.equals("/delete.do")) { // http://localhost:8282/servlet_kdb_board/delete.do?bid=4
System.out.println("/delete.do..");
command = new BDeleteCommand();
command.execute(request, response); // 게시글 가져오기
viewPage = "list.do";
} else if (com.equals("/write_view.do")) { // http://localhost:8282/servlet_kdb_board/write_view.do
command = new BWriteCommand();
command.execute(request, response);
viewPage = "list.do";
}
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPage);
dispatcher.forward(request, response);
}
}
---------------------------------------------------------------------
[계산 BCommand.java]
package edu.global.board.command;
import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface BCommand{ // 인터페이스
public abstract void execute(HttpServletRequest request, HttpServletResponse response);
}
---------------------------------------------------------------------
[계산 BListCommand.java]
package edu.global.board.command;
import java.io.*;
import java.util.*;
import javax.servlet.http.*;
import edu.global.board.dao.BDao;
import edu.global.board.vo.BoardVO;
public class BListCommand implements BCommand {
// 실행파일
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
BDao dao = new BDao(); // Board DAO 호출
List<BoardVO> vos = dao.list(); // DAO 안의 list()를 호출하여 BoardVO의 List 객체 생성
request.setAttribute("boards", vos); // 요청하여 갖고 오기
}
}
---------------------------------------------------------------------
[계산 BoardVO.java]
package edu.global.board.vo;
import java.sql.Timestamp;
//Name Null? Type
//-------- -------- --------------
//BID NOT NULL NUMBER(4)
//BNAME VARCHAR2(50)
//BTITLE VARCHAR2(100)
//BCONTENT VARCHAR2(1000)
//BDATE DATE
//BHIT NUMBER(4)
//BGROUP NUMBER(4)
//BSTEP NUMBER(4)
//BINDENT NUMBER(4)
public class BoardVO {
private int bid;
private String bname;
private String btitle;
private String bcontent;
private Timestamp bdate;
private int bhit;
private int bgroup;
private int bstep;
private int bindent;
public BoardVO(
int bid, String bname, String btitle, String bcontent, Timestamp bdate, int bhit, int bgroup, int bstep,
int bindent
) {
this.bid = bid;
this.bname = bname;
this.btitle = btitle;
this.bcontent = bcontent;
this.bdate = bdate;
this.bhit = bhit;
this.bgroup = bgroup;
this.bstep = bstep;
this.bindent = bindent;
}
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public String getBtitle() {
return btitle;
}
public void setBtitle(String btitle) {
this.btitle = btitle;
}
public String getBcontent() {
return bcontent;
}
public void setBcontent(String bcontent) {
this.bcontent = bcontent;
}
public Timestamp getBdate() {
return bdate;
}
public void setBdate(Timestamp bdate) {
this.bdate = bdate;
}
public int getBhit() {
return bhit;
}
public void setBhit(int bhit) {
this.bhit = bhit;
}
public int getBgroup() {
return bgroup;
}
public void setBgroup(int bgroup) {
this.bgroup = bgroup;
}
public int getBstep() {
return bstep;
}
public void setBstep(int bstep) {
this.bstep = bstep;
}
public int getBindent() {
return bindent;
}
public void setBindent(int bindent) {
this.bindent = bindent;
}
}
---------------------------------------------------------------------
[계산 BDao.java]
package edu.global.board.dao;
import java.sql.*;
import java.util.*;
import javax.naming.*;
import javax.sql.DataSource;
import edu.global.board.vo.BoardVO;
public class BDao {
private DataSource dataSource;
public BDao() {
try {
Context context = new InitialContext();
dataSource = (DataSource) context.lookup("java:comp/env/jdbc/oracle");
} catch (Exception e) {
e.printStackTrace();
}
}
public int writeView(String bid, String bname, String btitle, String bcontent, String bdate, String bhit,
String bgroup, String bstep, String bindent) { // CRUD 중 Update
Connection connection = null;
PreparedStatement preparedStatement = null;
int rn = 0;
try {
String query = "insert into mvc_board (bid, bname, btitle, bcontent) values (mvc_board_seq.nextval, ? , ?, ?)";
// ? = preparedStatement.setString(1, bname); 1번 값을 집어넣는 것
// setString(1)⇒1번글 가져오기 || setString(2)⇒2번글 가져오기
connection = dataSource.getConnection();
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, bname);
preparedStatement.setString(2, btitle);
preparedStatement.setString(3, bcontent);
rn = preparedStatement.executeUpdate(); // rn : result number
// resultSet = preparedStatement.executeQuery(); 가 아님
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (preparedStatement != null)
preparedStatement.close();
if (connection != null)
connection.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return rn;
}
public int delete(String bid) { // CRUD 중 Update
Connection connection = null;
PreparedStatement preparedStatement = null;
int rn = 0;
try {
String query = "delete mvc_board where bid= ? ";
connection = dataSource.getConnection();
preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, Integer.valueOf(bid));
rn = preparedStatement.executeUpdate(); // rn : result number
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (preparedStatement != null)
preparedStatement.close();
if (connection != null)
connection.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return rn;
}
public int modify(String bid, String bname, String btitle, String bcontent) { // CRUD 중 Update
Connection connection = null;
PreparedStatement preparedStatement = null;
int rn = 0;
try {
String query = "update mvc_board set bname = ?, btitle = ?, bcontent= ? where bid= ? ";
// ? = preparedStatement.setString(1, bname); 1번 값을 집어넣는 것
// setString(1)⇒1번글 가져오기 || setString(2)⇒2번글 가져오기
connection = dataSource.getConnection();
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, bname);
preparedStatement.setString(2, btitle);
preparedStatement.setString(3, bcontent);
preparedStatement.setInt(4, Integer.valueOf(bid));
rn = preparedStatement.executeUpdate(); // rn : result number
// resultSet = preparedStatement.executeQuery(); 가 아님
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (preparedStatement != null)
preparedStatement.close();
if (connection != null)
connection.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return rn;
}
public BoardVO contentView(String strID) {
BoardVO board = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
String query = "select * from mvc_board where bid = ?";
// ? = preparedStatement.setInt(1,Integer.valueOf(strID)) 1번 값을 집어넣는 것
// setInt(1)⇒1번글 가져오기 || setInt(2)⇒2번글 가져오기
connection = dataSource.getConnection();
preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, Integer.valueOf(strID));
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int bid = resultSet.getInt("bid");
String bname = resultSet.getString("bname");
String btitle = resultSet.getString("btitle");
String bcontent = resultSet.getString("bcontent");
Timestamp bdate = resultSet.getTimestamp("bdate");
int bhit = resultSet.getInt("bhit");
int bgroup = resultSet.getInt("bgroup");
int bstep = resultSet.getInt("bstep");
int bindent = resultSet.getInt("bindent");
board = new BoardVO(bid, bname, btitle, bcontent, bdate, bhit, bgroup, bstep, bindent);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
try {
if (resultSet != null)
resultSet.close();
if (preparedStatement != null)
preparedStatement.close();
if (connection != null)
connection.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null)
resultSet.close();
if (preparedStatement != null)
preparedStatement.close();
if (connection != null)
connection.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return board;
}
public List<BoardVO> list() {
List<BoardVO> boards = new ArrayList<BoardVO>();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
String query = "select * from mvc_board order by bgroup desc, bstep asc"; // 게시판에서 댓글
connection = dataSource.getConnection();
preparedStatement = connection.prepareStatement(query);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int bid = resultSet.getInt("bid");
String bname = resultSet.getString("bname");
String btitle = resultSet.getString("btitle");
String bcontent = resultSet.getString("bcontent");
Timestamp bdate = resultSet.getTimestamp("bdate");
int bhit = resultSet.getInt("bhit");
int bgroup = resultSet.getInt("bgroup");
int bstep = resultSet.getInt("bstep");
int bindent = resultSet.getInt("bindent");
BoardVO vo = new BoardVO(bid, bname, btitle, bcontent, bdate, bhit, bgroup, bstep, bindent);
boards.add(vo);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null)
resultSet.close();
if (preparedStatement != null)
preparedStatement.close();
if (connection != null)
connection.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return boards;
}
}
[결과값]