* 게시판 소스
1. 보려는 page = 현재페이지
2. total 게시물 = DB
3. page 내 게시물 수 = utilities
4. pageGroup 내 page = utilites
◀ -> 이전 page group 유무
1 2 3 4 5 ->현재 page가 속한 page Group을 알아야 한다. 이를 바탕으로 시작 page와 끝 page를 알아낸다. 그후 시작~끝까지
루프를 돌려 알아낸다.
▶ -> 다음 page group의 유무(있으면 Link를 걸고, 없으면 Link를 걸면 안된다.)
* 위의 작업을 list.jsp에서 해야하는데 이 작업은 공통이기 때문에 계산을 bean(pagingDTO.java)에서 처리하고 list.jsp에서는 가져다 쓴다.
* 아래 PagingDTO.java 안에 메소드를 채워라.
package board.dto;
import board.util.Utilities;
//totalContent -> 게시물수 - 201
//currentPage -> 보려는 page - 5
//contentPerPage -> page에 보여줄 컨텐츠 수 - 10
//pagePerPageGroup -> page내 page 수 - 5
/**
* 페이징 처리위한 bean <br>
* page : 게시물 묶음
* page group : page 묶음
* @author kgmyh
*
*/
public class PagingDTO {
/**
* 총 데이터(게시물)의 개수
*/
private int totalContent;
/**
* 현재 페이지
*/
private int currentPage;
/**
* 한 페이지에 보여질 데이터(게시물)개수
*/
private int contentsPerPage = Utilities.CONTENT_PER_PAGE; //5
/**
* Page Group 내 Page 수. 페이지 그룹에 들어갈 페이지 개수
*/
private int pagePerPagegroup = Utilities.PAGE_PER_PAGEGROUP; //10
/**
* 총 데이터(게시물) 개수, 현재 페이지를 받아 member variable에 할당
* @param totalContent
* @param currentPage
*/
public PagingDTO(int totalContent, int nowPage){
this.totalContent = totalContent;
this.currentPage = nowPage;
}
/**
* 현재 페이지 return
* @return
*/
public int getCurrentPage() {
return currentPage;
}
/**
* 현재 페이지 setting
* @param nowPage
*/
public void setCurrentPage(int nowPage) {
this.currentPage = nowPage;
}
/***************************************************************************
* 아래 메소드들을 구현하시오.
****************************************************************************/
/**
* 총 페이지 수를 return한다.<br>
* 1. 전체 데이터(게시물) % 한 페이지에 보여줄 데이터 개수 => 0 이면 둘을 / 값이 총 페이지 수<br>
* 2. 전체 데이터(게시물) % 한 페이지에 보여줄 데이터 개수 => 0보다 크면 둘을 / 값에 +1을 한 값이 총 페이지 수
* @return
*/
private int getTotalPage(){
int totalPage = 0;
if((this.totalContent%this.contentsPerPage)==0){
totalPage = this.totalContent / this.contentsPerPage;
}else if((this.totalContent%this.contentsPerPage)>0){
totalPage = this.totalContent / this.contentsPerPage +1;
}
return totalPage;
}
/**
* 총 페이지 그룹의 수를 return한다.<br>
* 1. 총 페이지수 % Page Group 내 Page 수. => 0 이면 둘을 / 값이 총 페이지 수<br>
* 2. 총 페이지수 % Page Group 내 Page 수. => 0보다 크면 둘을 / 값에 +1을 한 값이 총 페이지 수
* @return
*/
private int getTotalPageGroup(){
int totalPageGoup = 0;
if((this.getTotalPage()%this.pagePerPagegroup)==0){
totalPageGoup = this.getTotalPage() / this.pagePerPagegroup;
}else if((this.getTotalPage()%this.pagePerPagegroup)>0){
totalPageGoup = this.getTotalPage() / this.pagePerPagegroup +1;
}
return totalPageGoup;
}
/**
* 현재 페이지가 속한 페이지 그룹 번호(몇 번째 페이지 그룹인지) 을 return 하는 메소드
* 1. 현재 페이지 % Page Group 내 Page 수 => 0 이면 둘을 / 값이 현재 페이지 그룹.
* 2. 현재 페이지 % Page Group 내 Page 수 => 0 크면 둘을 / 값에 +1을 한 값이 현재 페이지 그룹
* @return
*/
private int getCurrentPageGroup(){
int currentPageGroup = 0;
if((this.currentPage%this.pagePerPagegroup)==0){
currentPageGroup = this.currentPage / this.pagePerPagegroup;
}else if((this.currentPage%this.pagePerPagegroup)>0){
currentPageGroup = this.currentPage / this.pagePerPagegroup +1;
}
return currentPageGroup;
}
/**
* 현재 페이지가 속한 페이지 그룹의 시작 페이지 번호를 return 한다.<br>
* 1. Page Group 내 Page 수*(현재 페이지 그룹 -1) + 1을 한 값이 첫 페이지이다.(페이지 그룹*페이지 그룹 개수 이 그 그룹의 마지막 번호이므로)
* 2. 위의 계산 결과가 0인 경우는 첫페이지 이므로 1을 return 한다.
* @return
*/
public int getStartPageOfPageGroup(){
int startPageNo = 0;
int currentPageGroup = this.getCurrentPageGroup();
startPageNo = pagePerPagegroup*(currentPageGroup-1)+1;
if(startPageNo==0){
startPageNo = 1;
}
return startPageNo;
}
/**
* 현재 페이지가 속한 페이지 그룹의 마지막 페이지 번호를 return 한다.<br>
* 1. 현재 페이지 그룹 * 페이지 그룹내 페이지 수 가 마지막 번호이다.
* 2. 그 그룹의 마지막 페이지 번호가 전체 페이지의 마지막 페이지 번호보다 큰 경우는 전체 페이지의 마지막 번호를 return 한다.
* @return
*/
public int getEndPageOfPageGroup(){
int endPageNo = this.getCurrentPageGroup() * pagePerPagegroup;
if(endPageNo>getTotalPage()){ //마지막 page가 총 page수 보다 크다면
endPageNo = getTotalPage();
}
return endPageNo;
}
/**
* 이전 페이지 그룹이 있는지 체크
* 현재 페이지가 속한 페이지 그룹이 1보다 크면 true
* @return
*/
public boolean isPreviousPageGroup(){
boolean flag = false;
if(this.getCurrentPageGroup()!=1){//현재 pagegroup이 1이 아니면
flag = true;
}
return flag;
}
/**
* 다음 페이지 그룹이 있는지 체크
* 현재 페이지 그룹이 마지막 페이지 그룹(마지막 페이지 그룹 == 총 페이지 그룹 수) 보다 작으면 true
* @return
*/
public boolean isNextPageGroup(){
boolean flag = false;
if(this.getCurrentPageGroup()<this.getTotalPageGroup()){
flag = true;
}
return flag;
}
/*
테스트 용 메인 메소드
*/
public static void main(String[] args) {
PagingDTO d = new PagingDTO(201, 5);//총 201개 게시물, 현재page : 5
//page내 게시물수 - 5
//page그룹내 page수 - 10
System.out.println("총 page 수 : "+d.getTotalPage()); //41
System.out.println("총 page그룹 : "+d.getTotalPageGroup());//5
System.out.println("현 페이지 그룹 : "+d.getCurrentPageGroup());//1
System.out.println("첫 page : "+d.getStartPageOfPageGroup());//1
System.out.println("마지막 page : "+d.getEndPageOfPageGroup());//10
System.out.println("이전 page그룹 존재 ? : "+d.isPreviousPageGroup());//false
System.out.println("다음 page그룹 존재 ? : "+d.isNextPageGroup());//true
}
}
class :
package : board.dto
name : ListDTO
package board.dto;
import java.util.ArrayList;
public class ListDTO {
private ArrayList<BoardDTO> list;
private PagingDTO pagingDTO;
public ListDTO() {
super();
// TODO Auto-generated constructor stub
}
public ListDTO(ArrayList<BoardDTO> list, PagingDTO pagingDTO) {
super();
this.list = list;
this.pagingDTO = pagingDTO;
}
public ArrayList<BoardDTO> getList() {
return list;
}
public void setList(ArrayList<BoardDTO> list) {
this.list = list;
}
public PagingDTO getPagingDTO() {
return pagingDTO;
}
public void setPagingDTO(PagingDTO pagingDTO) {
this.pagingDTO = pagingDTO;
}
@Override
public String toString() {
return "ListDTO [list=" + list + ", pagingDTO=" + pagingDTO + "]";
}
}
--------------------------목록 페이지 조회 - 페이징 처리된 -----------------------------
시작 : show_content.jsp : 글목록(페이징) 링크 클릭 시 command : list
Ctr : BoardListController.execute()
BoardService : getBoardListByPage(int page) : ListDTO
BoardDAO : selectBoardListByPage(int page) : ArrayList<BoardDTO>
selectTotalContent() : int - count함수 사용
응답 : list.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="/${initParam.context_root }/boardController?command=list&page=${param.page }">글목록(페이징)</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();
}else if(command.equals("reply_content")){
ctr = new ReplyContentController();
}else if(command.equals("list")){
ctr = new BoardListController();
}
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.dto.ListDTO;
import board.model.service.BoardService;
public class BoardListController implements Controller {
@Override
public ForwardDTO execute(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//글번로를 받아 글의 벙보를 조회하는 컨트롤러
ForwardDTO forwardDTO = null;
int page = 1;
try{
page = Integer.parseInt(request.getParameter("page"));
}catch(Exception e){}
//2. 비지니스 로직 호출->model
BoardService boardService = BoardService.getInstance();
try{
ListDTO ldto = boardService.getBoardListByPage(page);
//응답
request.setAttribute("ldto", ldto);
forwardDTO = new ForwardDTO("/list.jsp",false);
} catch(Exception e){
e.printStackTrace();
request.setAttribute("error_message", e.getMessage());
forwardDTO = new ForwardDTO("/error.jsp",false);
}
return forwardDTO;
}
}
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;
import board.util.Utilities;
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);
}
}
public ArrayList<BoardDTO> selectBoardListByPage(int page) throws SQLException{
System.out.println("Utilities.CONTENT_PER_PAGE : "+Utilities.CONTENT_PER_PAGE);
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset = null;
String sql = "select no, title, writer, content, writedate, viewcount, refamily, restep, relevel from(select ceil(rownum/?) page, no, title, writer, content, writedate, viewcount, refamily, restep, relevel from (select no, title, writer, content, writedate, viewcount, refamily, restep, relevel from board order by refamily desc, restep asc)) where page=?";
//String sql = "select no, title, writer, content, writedate, viewcount, refamily, restep, relevel from board where no=?";
ArrayList<BoardDTO> list = new ArrayList<BoardDTO>();
try{
BasicDataSource dataSource = dbUtil.getDataSource();
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, Utilities.CONTENT_PER_PAGE);
pstmt.setInt(2, page);
rset = pstmt.executeQuery();
while(rset.next()){
list.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;
}
public int selectTotalContent() throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset = null;
String sql ="SELECT COUNT(no) FROM board";
int totalContent = 0;
try {
BasicDataSource ds = dbUtil.getDataSource();
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql);
rset = pstmt.executeQuery();
if (rset.next()) {
totalContent = rset.getInt(1);
}
} finally {
dbUtil.close(conn, pstmt, rset);
}
return totalContent;
}
}
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;
import board.util.Utilities;
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);
}
}
public ArrayList<BoardDTO> selectBoardListByPage(int page) throws SQLException{
System.out.println("Utilities.CONTENT_PER_PAGE : "+Utilities.CONTENT_PER_PAGE);
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset = null;
String sql = "select no, title, writer, content, writedate, viewcount, refamily, restep, relevel from(select ceil(rownum/?) page, no, title, writer, content, writedate, viewcount, refamily, restep, relevel from (select no, title, writer, content, writedate, viewcount, refamily, restep, relevel from board order by refamily desc, restep asc)) where page=?";
//String sql = "select no, title, writer, content, writedate, viewcount, refamily, restep, relevel from board where no=?";
ArrayList<BoardDTO> list = new ArrayList<BoardDTO>();
try{
BasicDataSource dataSource = dbUtil.getDataSource();
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, Utilities.CONTENT_PER_PAGE);
pstmt.setInt(2, page);
rset = pstmt.executeQuery();
while(rset.next()){
list.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;
}
public int selectTotalContent() throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset = null;
String sql ="SELECT COUNT(no) FROM board";
int totalContent = 0;
try {
BasicDataSource ds = dbUtil.getDataSource();
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql);
rset = pstmt.executeQuery();
if (rset.next()) {
totalContent = rset.getInt(1);
}
} finally {
dbUtil.close(conn, pstmt, rset);
}
return totalContent;
}
}
<%@ 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>
</head>
<style type="text/css">
table{
width : 650px;
border-collapse: collapse;
}
td{
padding: 5px;
}
</style>
<body>
<h2>글목록</h2>
<p>
<table border="1" width="800">
<tr align="center" style="font-weight: bold" bgcolor="yellow">
<td width="65px">글번호</td>
<td width="300">제목</td>
<td width="80px">작성자</td>
<td width="60">작성일</td>
<td width="60">조회수</td>
</tr>
<c:forEach items="${requestScope.ldto.list }" var="bdto">
<tr>
<td>${bdto.no }</td>
<td>
<c:if test="${bdto.relevel!=0 }">
<c:forEach begin="1" end="${bdto.relevel }" step="1">
</c:forEach>
ㄴ
</c:if>
<a href="/${initParam.context_root }/boardController?command=get_content&no=${bdto.no}&page=${requestScope.ldto.pagingDTO.currentPage }">${bdto.title }</a></td>
<td>${bdto.writer }</td>
<td>${bdto.writedate }</td>
<td>${bdto.viewcount }</td>
</tr>
</c:forEach>
</table>
<table>
<tr>
<td>
<c:choose>
<c:when test="${requestScope.ldto.pagingDTO.previousPageGroup }">
<a href="/${initParam.context_root }/boardController?command=list&page=${requestScope.ldto.pagingDTO.startPageOfPageGroup-1 }">◀ </a>
</c:when>
<c:otherwise>◀ </c:otherwise>
</c:choose>
<c:forEach begin="${requestScope.ldto.pagingDTO.startPageOfPageGroup }" end="${requestScope.ldto.pagingDTO.endPageOfPageGroup }" step="1" var="cnt" >
<c:choose>
<c:when test="${cnt==requestScope.ldto.pagingDTO.currentPage }">${cnt }</c:when>
<c:otherwise>
<a href="/${initParam.context_root }/boardController?command=list&page=${cnt }">${cnt }</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:choose>
<c:when test="${requestScope.ldto.pagingDTO.nextPageGroup }">
<a href="/${initParam.context_root }/boardController?command=list&page=${requestScope.ldto.pagingDTO.endPageOfPageGroup+1 }">▶ </a>
</c:when>
<c:otherwise>▶ </c:otherwise>
</c:choose>
</td>
</tr>
</table>
</body>
</html>
* 결과보기
'프로그래밍 > JSP Programming' 카테고리의 다른 글
2012-5-21 게시판 페이징 처리하기(rownum, 서브쿼리) (0) | 2012.05.21 |
---|---|
2012-5-20 MVC를 이용한 게시판 만들기[처음~끝 완성본] (0) | 2012.05.20 |
게시판 소스 (0) | 2012.05.18 |
2012-5-18 답변폼 조회 (0) | 2012.05.18 |
2012-5-18 MVC패턴을 활용한 게시판 글삭제하기 (0) | 2012.05.18 |