public ArrayList<BoardDTO> getBoardList(){
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
ArrayList<BoardDTO> boardlist = new ArrayList<>();
try {
con = getConnection();
// 3단계 sql
// 기본 num 기준 오름차순 => 최근글 위에 올라오게 정렬 (내림차순)
String sql = "select * from board order by num desc";
pstmt = con.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()) {
BoardDTO dto = new BoardDTO();
dto.setNum(rs.getInt("num"));
dto.setName(rs.getString("name"));
dto.setSubject(rs.getString("subject"));
dto.setDate(rs.getTimestamp("date"));
dto.setReadcount(rs.getInt("readcount"));
dto.setDate(rs.getTimestamp("date"));
// 바구니의 주소값을 배열 한칸에 저장
boardlist.add(dto);
}
} catch (Exception e) {
e.printStackTrace();;
} finally {
if(con!=null)try {con.close();} catch (Exception e2) {}
if(pstmt!=null)try {pstmt.close();} catch (Exception e2) {}
if(rs!=null)try {rs.close();} catch (Exception e2) {}
}
return boardlist;
}
<%@page import="board.BoardDTO"%>
<%@page import="java.util.ArrayList"%>
<%@page import="board.BoardDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>board/list.jsp</title>
</head>
<body>
<h1>board/list.jsp</h1>
<%
// BoardDAO 객체생성
// 리턴할형 ArrayList<BoardDTO> getBoardList() 메서드정의
// ArrayList<BoardDTO> BoardList = dao.getBoardList() 메서드 호출
BoardDAO dao = new BoardDAO();
ArrayList<BoardDTO> BoardList = dao.getBoardList();
%>
<table border="1">
<tr><td>글번호</td><td>글쓴이</td><td>제목</td><td>글쓴날짜</td><td>조회수</td></tr>
<%
// 배열 접근 => for => // 배열 한칸에 내용 가져오기 => BoardDTO 저장 => 출력
for(int i=0;i<BoardList.size();i++){
BoardDTO dto = BoardList.get(i); %>
<tr><td><%=dto.getNum()%></td><td><%=dto.getName()%></td><td><a href="content.jsp?num=<%=dto.getName()%>"><%=dto.getSubject()%></a></td>
<td><%=dto.getDate()%></td><td><%=dto.getReadcount()%></td></tr>
<%}
%>
</table>
</body>
</html>
public BoardDTO getBoard(int num) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
BoardDTO dto = null;
try {
con = getConnection();
String sql = "select * from board where num = ?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, num);
rs=pstmt.executeQuery();
while(rs.next()) {
dto = new BoardDTO();
dto.setNum(rs.getInt("num"));
dto.setName(rs.getString("name"));
dto.setSubject(rs.getString("subject"));
dto.setDate(rs.getTimestamp("date"));
dto.setReadcount(rs.getInt("readcount"));
dto.setDate(rs.getTimestamp("date"));
dto.setContent(rs.getNString("content"));}
}catch (Exception e) {
e.printStackTrace();
} finally {
if(con!=null)try {con.close();} catch (Exception e2) {}
if(pstmt!=null)try {pstmt.close();} catch (Exception e2) {}
if(rs!=null)try {rs.close();} catch (Exception e2) {}
}
return dto;
}
<%@page import="board.BoardDTO"%>
<%@page import="board.BoardDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>board/content.jsp</title>
</head>
<body>
<%
// request에 저장된 num 파라미터값 가져오기
int num = Integer.parseInt(request.getParameter("num"));
// BoardDAO 객체생성
BoardDAO dao = new BoardDAO();
// 리턴할형 BoardDTO getBoard(int num) 메서드 정의
// BoardDTO dto = dao.getBoard(num) 메서드 호출
BoardDTO dto = dao.getBoard(num);
%>
<h1>글내용</h1>
<table border="1">
<tr><td>글번호</td><td><%=dto.getNum()%></td></tr>
<tr><td>작성자</td><td><%=dto.getName()%></td></tr>
<tr><td>글쓴날짜</td><td><%=dto.getDate() %></td></tr>
<tr><td>조회수</td><td><%=dto.getReadcount() %></td></tr>
<tr><td>글제목</td><td><%=dto.getSubject()%></td></tr>
<tr><td>글내용</td><td><%=dto.getContent()%></td></tr>
<tr><td colspan="2">
<input type="button" value="글목록" onclick="location.href='list.jsp'"></td></tr>
</table>
</body>
</html>
<%@page import="board.BoardDTO"%>
<%@page import="board.BoardDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>board/updateForm.jsp</title>
</head>
<body>
<%
// request에 저장된 num 파라미터값 가져오기
int num = Integer.parseInt(request.getParameter("num"));
// BoardDAO 객체생성
BoardDAO dao = new BoardDAO();
// BoardDTO dto = dao.getboard(num) 메서드 호출
BoardDTO dto = dao.getBoard(num);
%>
<h1>글수정</h1>
<form action="updatePro.jsp" method="post">
<input type="hidden" name="num" value="<%=num%>">
<table border="1">
<tr><td>글쓴이</td>
<td><input type="text" name="name" value="<%=dto.getName()%>" readonly></td></tr>
<tr><td>글제목</td>
<td><input type="text" name="subject" value="<%=dto.getSubject()%>"></td></tr>
<tr><td>글내용</td>
<td><textarea name="content" rows="10" cols="20"><%=dto.getContent()%></textarea></td></tr>
</table>
<input type="submit" value="작성완료" class="submit">
</form>
</body>
</html>
public void updateBoard(BoardDTO dto) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = getConnection();
String sql = "update board set subject = ?, content = ? where num = ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, dto.getSubject());
pstmt.setString(2, dto.getContent());
pstmt.setInt(3, dto.getNum());
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(con!=null)try {con.close();} catch (Exception e2) {}
if(pstmt!=null)try {pstmt.close();} catch (Exception e2) {}
}
}
<%@page import="board.BoardDAO"%>
<%@page import="board.BoardDTO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// board/updatePro.jsp
// request => num, name, subject, content 파라미터 => 변수 저장
request.setCharacterEncoding("utf-8");
int num = Integer.parseInt(request.getParameter("num"));
String name = request.getParameter("name");
String subject = request.getParameter("subject");
String content = request.getParameter("content");
// BoardDTO 객체생성
// set 메서드 호출 num, name, subject content 저장
BoardDTO dto = new BoardDTO();
dto.setNum(num);
dto.setName(name);
dto.setSubject(subject);
dto.setContent(content);
// BoardDAO 객체생성
// 리턴할형 없음 updateBoard(dto)
BoardDAO dao = new BoardDAO();
// updateBoard(dto) 메서드 호출
// list.jsp 이동
dao.updateBoard(dto);
response.sendRedirect("list.jsp");
%>
public void deleteBoard(int num) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = getConnection();
String sql = "delete from board where num = ?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, num);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(con!=null)try {con.close();} catch (Exception e2) {}
if(pstmt!=null)try {pstmt.close();} catch (Exception e2) {}
}
}
<%@page import="board.BoardDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// board/deletePro.jsp
// request => num 파라미터 변수저장
int num = Integer.parseInt(request.getParameter("num"));
// BoardDAO 객체생성
BoardDAO dao = new BoardDAO();
// 리턴할형 없음 deleteBoard(int num) 메서드 정의;
// deleteBoard(num) 메서드 호출
dao.deleteBoard(num);
response.sendRedirect("list.jsp");
%>