블로그 목차를 잘 나눌 수 있게 됐다.
역시 해야 느는 거였다.
// dto
public class BoardDTO {
private int num;
private String writer;
private String email;
private String subject;
private String passwd;
private String reg_date;
private int readcount;
private String content;
private String ip;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getReg_date() {
return reg_date;
}
public void setReg_date(String reg_date) {
this.reg_date = reg_date;
}
public int getReadcount() {
return readcount;
}
public void setReadcount(int readcount) {
this.readcount = readcount;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
}
dto 는 이렇게 만들어 줬다.
create table board (
num number primary key,
writer varchar2(10) not null,
email varchar2(30) not null,
subject varchar2(50) not null,
passwd varchar2(12) not null,
reg_date varchar2(10) not null,
readcount number not null,
content varchar2(4000) not null,
ip varchar2(20) not null
);
이게 db 구성
CREATE SEQUENCE board_seq
START WITH 1
INCREMENT BY 1
NOMAXVALUE;
시퀀스도 만들었는데 이상하게 이렇게 만들어도 마음대로 번호가 배정되더라.
왜 하나씩 늘지 않는 거니.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" import="java.util.*, my.board.*"%>
<% request.setCharacterEncoding("EUC-KR"); %>
<%@ include file="../top.jsp"%>
<!-- login.jsp-->
<link rel="stylesheet" type="text/css" href="../style.css">
<!-- find.jsp -->
<jsp:useBean id="boarddao" class="my.board.BoardDAO"/>
<jsp:useBean id="pool" class="my.db.ConnectionPoolBean" scope="application"/>
<jsp:setProperty name="boarddao" property="pool" value="<%=pool%>"/>
<html>
<head>
<title>글 목록</title>
</head>
<body>
<div align="center">
<hr color="green" width="300">
<h2>글 목 록</h2>
<hr color="green" width="300">
<table borde="0" width="100%" class="outline">
<tr>
<td class="m1" colspan="5"></td>
<td class="m1" align="center"><a href="writeForm.jsp">글쓰기</a></td>
</tr>
<tr bgcolor="limegreen">
<th class="m1" width="10%">번호</th>
<th class="m1" width="30%">제목</th>
<th class="m1" width="15%">작성자</th>
<th class="m1" width="20%">작성일</th>
<th class="m1" width="10%">조회</th>
<th class="m1" width="20%">ip</th>
</tr>
<%
List<BoardDTO> list = boarddao.ListBoard(startRow, endRow);
if (list == null || list.size() == 0){
%>
<tr>
<td colspan="6">글이 등록되어 있지 않습니다.</td>
</tr>
<%
}else {
for(BoardDTO dto : list){
%>
<tr>
<td align="center"><%=co--%></td>
<td>
<img src="../img/level.gif" width="<%=dto.getRe_level()*10%>"/>
<a href="content.jsp?num=<%=dto.getNum()%>&co=<%=co %>">
<%=dto.getSubject()%>
</a>
<% if (dto.getReadcount() > 10){ %>
<img src="../img/hot.gif">
<% } %>
</td>
<td align="center"><%=dto.getWriter()%></td>
<td align="center"><%=dto.getReg_date()%></td>
<td align="center"><%=dto.getReadcount()%></td>
<td align="center"><%=dto.getIp()%></td>
</tr>
<% }
}%>
</table><br>
<%
}
%>
</div>
</body>
</html>
<%@ include file="../bottom.jsp"%>
list.jsp
이미 다 만든 상태에서 위키 정리를 하는 중이라 적절한 코드를 다 뺐는지 모르겠다.
우선 DB에 저장된 글들이 출력될 수 있도록 만들었음. 그래서 dao에 디비에 있는 dto객체들을 리스트로 만들어서 값들이 출력될 수 있도록 만들었다.
package my.board;
import java.sql.*;
import java.util.*;
import my.db.ConnectionPoolBean;
import my.member.MemberDTO;
//dao
public class BoardDAO {
Connection con;
PreparedStatement ps;
ResultSet rs;
private ConnectionPoolBean pool;
public void setPool(ConnectionPoolBean pool) {
this.pool = pool;
}
public List<BoardDTO> makeList(ResultSet rs) throws SQLException {
List<BoardDTO> list = new ArrayList<>();
while(rs.next()) {
BoardDTO dto = new BoardDTO();
dto.setNum(rs.getInt("num"));
dto.setWriter(rs.getString("writer"));
dto.setEmail(rs.getString("email"));
dto.setSubject(rs.getString("subject"));
dto.setPasswd(rs.getString("passwd"));
dto.setReg_date(rs.getString("reg_date"));
dto.setReadcount(rs.getInt("readcount"));
dto.setContent(rs.getString("content"));
dto.setIp(rs.getString("ip"));
list.add(dto);
}
return list;
}
public List<BoardDTO> ListBoard() throws SQLException{
try {
sql = "select * from board";
ps = con.prepareStatement(sql); // sql 문의 결과를 가져옴
ResultSet rs = ps.executeQuery(); // rs 쿼리 문의 결과를 담음
List<BoardDTO> list = makeList(rs);
return list;
}finally{
if (rs != null) rs.close();
if (ps != null) ps.close();
if (con != null) pool.returnConnection(con);
}
}
dao 에 게시판 list 출력될 수 있게 만드는 메서드를 만들어 준다.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<% request.setCharacterEncoding("EUC-KR"); %>
<%@ include file="../top.jsp"%>
<!-- login.jsp-->
<link rel="stylesheet" type="text/css" href="../style.css">
<script type="text/javascript">
function check(){
if (f.writer.value == ""){
alert("이름을 입력해 주세요!!")
f.writer.focus()
return false
}
if (f.subject.value == ""){
alert("제목을 입력해 주세요!!")
f.subject.focus()
return false
}
if (f.content.value == ""){
alert("내용을 입력해 주세요!!")
f.content.focus()
return false
}
if (f.passwd.value == ""){
alert("비밀번호을 입력해 주세요!!")
f.passwd.focus()
return false
}
return true
}
</script>
<%
if (isLogin == false) {%>
<script type="text/javascript">
alert("로그인 해 주세요")
location.href="list.jsp"
</script>
<%
}
%>
<html>
<head>
<title>글 쓰기</title>
</head>
<body>
<form name="f" action="writePro.jsp" method="post" onsubmit="return check()">
<input type="hidden" name="num" value="<%=num%>"/>
<input type="hidden" name="num" value="<%=re_level%>"/>
<input type="hidden" name="num" value="<%=re_step%>"/>
<div align="center">
<table border="1" width="90%">
<tr bgcolor="lightgray">
<td colspan="2" align="center">글쓰기</a></td>
</tr>
<tr>
<td width="15%" align="center">이름</td>
<td><input type="text" name="writer" value="<%=id%>" tabindex="1" readOnly></td>
</tr>
<tr>
<td width="15%" align="center">제목</td>
<td><input type="text" name="subject" tabindex="2" style="width: 100%;"></td>
</tr>
<tr>
<td width="15%" align="center">이메일</td>
<td><input type="text" name="email" tabindex="3" style="width: 100%;"></td>
</tr>
<tr>
<td width="15%" align="center">내용</td>
<td><textarea name="content" tabindex="4" style="width: 100%;" rows="15"></textarea></td>
</tr>
<tr>
<td width="15%" align="center">비밀번호</td>
<td><input type="password" tabindex="5" name="passwd"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="hidden" name="ip" value="<%=request.getRemoteAddr()%>">
<input type="submit" value="제출">
<input type="button" value="다시 작성" onclick="location.href='<%=request.getContextPath()%>/board/writeForm.jsp';">
<input type="button" value="목록 보기" onclick="location.href='<%=request.getContextPath()%>/board/list.jsp';">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
<%@ include file="../bottom.jsp"%>
writeForm에서는 글을 쓸 수 있는 html 코드들과 input 태그로 게시글 내용을 받아 writePro에서 처리할 수 있도록 만들었다.
그리고 로그인 안 하면 못 들어옴.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<% request.setCharacterEncoding("EUC-KR"); %>
<jsp:useBean id="boarddao" class="my.board.BoardDAO"/>
<jsp:useBean id="pool" class="my.db.ConnectionPoolBean" scope="application"/>
<jsp:setProperty name="boarddao" property="pool" value="<%=pool%>"/>
<jsp:useBean id="boarddto" class="my.board.BoardDTO"/>
<jsp:setProperty property="*" name="boarddto"/>
<%
if (boarddto.getWriter() == null || boarddto.getWriter().trim().equals("")){
response.sendRedirect("list.jsp");
return;
}
int res = boarddao.UpdateBoard(boarddto);
if (res > 0){%>
<script type="text/javascript">
alert("업데이트 성공 :게시판으로 이동합니다")
location.href="list.jsp"
</script>
<% }else {%>
<script type="text/javascript">
alert("업데이트 실패 : 다시 입력해 주세요")
location.href="writeForm.jsp"
</script>
<% }
%>
만약 글을 작성했다면 dao에 있는 updateBoard 메서드를 실행해 준다.
그럼 해당 값들이 DB에 저장된다.
public int UpdateBoard(BoardDTO dto) throws SQLException {
try {
con = pool.getConnection();
sql = "insert into board values(board_seq.nextval, ?, ?, ?, ?, sysdate, ?, ?, ?)";
ps = con.prepareStatement(sql);
ps.setString(1, dto.getWriter());
ps.setString(2, dto.getEmail());
ps.setString(3, dto.getSubject());
ps.setString(4, dto.getPasswd());
ps.setInt(5, 0);
ps.setString(6, dto.getContent());
ps.setString(7, dto.getIp());
int res = ps.executeUpdate();
return res;
}finally{
if (ps != null) ps.close();
if (con != null) pool.returnConnection(con);
}
}
dao 에 게시글 업데이트 메서드를 추가해 주기
이렇게 새글을 만들었다면 게시판에 추가한 글을 볼 수 있도록 content 화면을 짜 준다.
역시 top 에 기재한login 을 통해 isLogin 이 false면 값을 볼 수 없다.
또한 만약 글이 클릭된다면 조회수를 +1 해 준다.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<% request.setCharacterEncoding("EUC-KR"); %>
<!-- content.jsp-->
<%@ include file="../top.jsp"%>
<link rel="stylesheet" type="text/css" href="../style.css">
<jsp:useBean id="boarddao" class="my.board.BoardDAO"/>
<jsp:useBean id="pool" class="my.db.ConnectionPoolBean" scope="application"/>
<jsp:setProperty name="boarddao" property="pool" value="<%=pool%>"/>
<jsp:useBean id="dto" class="my.board.BoardDTO"/>
<jsp:setProperty property="*" name="dto"/>
<%
if (isLogin == false) {%>
<script type="text/javascript">
alert("로그인 해 주세요")
location.href="list.jsp"
</script>
<%
}
int num = Integer.parseInt(request.getParameter("num"));
dto = boarddao.viewBoardPosts(num);
int co = Integer.parseInt(request.getParameter("co"));
System.out.println(num + "번 글을 불러왔습니다.");
System.out.println(co + "번 글을 불러왔습니다.");
int res = boarddao.updateReadcount(dto);
if (res > 0){
System.out.println("조회수 업데이트");
}else {
System.out.println("조회수 업데이트 실패");
}
%>
<html>
<head>
<title>글 쓰기</title>
</head>
<body>
<div align="center">
<table border="1" width="90%">
<tr bgcolor="lightgray">
<td colspan="4" align="center">글 내용 보기</td>
</tr>
<tr>
<td width="25%" align="center">글 번호</td>
<td width="25%" align="center"><%= co+1 %></td>
<td width="25%" align="center">조회수</td>
<td width="25%" align="center"><%= dto.getReadcount() %></td>
</tr>
<tr>
<td width="25%" align="center">작성자</td>
<td width="25%" align="center"><%= dto.getWriter() %></td>
<td width="25%" align="center">작성일</td>
<td width="25%" align="center"><%= dto.getReg_date() %></td>
</tr>
<tr>
<td width="25%" align="center">글 제목</td>
<td colspan="3" align="center"><%= dto.getSubject() %></td>
</tr>
<tr>
<td width="25%" align="center">글 내용</td>
<td colspan="3" align="center"><%= dto.getContent() %></td>
</tr>
<tr>
<td colspan="4" align="right">
<input type="button" value="답글"
onclick="window.location='writeForm.jsp?num=<%= dto.getNum() %>&re_step=<%=dto.getRe_step() %>&re_level=<%=dto.getRe_level() %>'">
<input type="button" value="수정"
onclick="window.location='updateForm.jsp?num=<%= dto.getNum() %>'">
<input type="button" value="삭제"
onclick="window.location='deleteForm.jsp?num=<%= dto.getNum() %>'">
<input type="button" value="목록"
onclick="window.location='list.jsp'">
</td>
</tr>
</table>
</div>
</body>
</html>
<%@ include file="../bottom.jsp"%>
// content 내용을 출력하는 메서드
public BoardDTO viewBoardPosts(int num) throws SQLException{
try {
con = pool.getConnection();
String sql = "select * from board where num = ?";
ps = con.prepareStatement(sql);
ps.setInt(1, num);
rs = ps.executeQuery();
List<BoardDTO> list = makeList(rs);
return list.get(0);
}finally{
if (rs != null) rs.close();
if (ps != null) ps.close();
if (con != null) pool.returnConnection(con);
}
}
// 조회수가 오를 때마다 카운트를 세고 +1 해 준다.
public int updateReadcount(BoardDTO dto) throws SQLException {
try {
con = pool.getConnection();
String sql = "update board set readcount=? where num=?";
ps = con.prepareStatement(sql);
ps.setInt(1, (dto.getReadcount()+1));
ps.setInt(2, dto.getNum());
int res = ps.executeUpdate();
return res;
}finally{
if (rs != null) rs.close();
if (ps != null) ps.close();
if (con != null) pool.returnConnection(con);
}
}
dao 에 추가한 메서드들
num은 get 방식을 통해 따로 가져와 구분한다.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" import="java.util.*, my.board.*"%>
<%@ include file="../top.jsp"%>
<!-- login.jsp-->
<link rel="stylesheet" type="text/css" href="../style.css">
<jsp:useBean id="boarddao" class="my.board.BoardDAO"/>
<jsp:useBean id="pool" class="my.db.ConnectionPoolBean" scope="application"/>
<jsp:setProperty name="boarddao" property="pool" value="<%=pool%>"/>
<%
int num = Integer.parseInt(request.getParameter("num"));
// List<BoardDTO> list = boarddao.ListBoard();
%>
<html>
<head>
<title>글 삭제</title>
</head>
<body>
<form name="f" method="POST" action="deletePro.jsp?num=<%=num %>">
<div align="center">
<hr color="green" width="300">
<h2>글 삭제</h2>
<hr color="green" width="300">
<table borde="0" width="100%" class="outline">
<tr>
<td align="center">비밀번호를 입력해 주세요</td>
</tr>
<tr>
<td align="center">비밀번호 : <input type="password" name="passwd"></td>
</tr>
<td align="center">
<input type="submit" value="삭제" >
<input type="button" value="글목록"
onclick="window.location='list.jsp'">
</td>
</table>
</div>
</form>
</body>
</html>
<%@ include file="../bottom.jsp"%>
삭제하려면 비밀번호를 필요로 한다.
그건 form 태그를 통해 post 방식으로 가져간다.
이건 나중이랑 똑같아서 그냥 첨부해 봤다.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<jsp:useBean id="boarddao" class="my.board.BoardDAO"/>
<jsp:useBean id="pool" class="my.db.ConnectionPoolBean" scope="application"/>
<jsp:setProperty name="boarddao" property="pool" value="<%=pool%>"/>
<jsp:useBean id="boarddto" class="my.board.BoardDTO"/>
<jsp:setProperty property="*" name="boarddto"/>
<%
int num = Integer.parseInt(request.getParameter("num"));
int res = boarddao.deleteBoard(num, boarddto.getPasswd());
if (res > 0){%>
<script type="text/javascript">
alert("삭제 성공 : 게시판으로 돌아갑니다")
location.href="list.jsp"
</script>
<% }else {%>
<script type="text/javascript">
alert("삭제 실패 : 비밀번호를 다시 입력해 주세요")
location.href="list.jsp"
</script>
<% }
%>
public int deleteBoard(int num, String pass) throws SQLException {
try {
con = pool.getConnection();
String sql = "delete from board where num = ? and passwd = ?";
ps = con.prepareStatement(sql);
ps.setInt(1, num);
ps.setString(2, pass);
int res = ps.executeUpdate();
return res;
}finally {
if (ps != null) ps.close();
if (con != null) pool.returnConnection(con);
}
}
DB에 있는 passwd 와 작성되어 input 값으로 들어온 passwd, 그리고 번호 num 값을 대조한 후 맞으면 삭제해 준다.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" import="java.util.*, my.board.*"%>
<%@ include file="../top.jsp"%>
<% request.setCharacterEncoding("EUC-KR"); %>
<!-- login.jsp-->
<link rel="stylesheet" type="text/css" href="../style.css">
<jsp:useBean id="boarddao" class="my.board.BoardDAO"/>
<jsp:useBean id="pool" class="my.db.ConnectionPoolBean" scope="application"/>
<jsp:setProperty name="boarddao" property="pool" value="<%=pool%>"/>
<jsp:useBean id="dto" class="my.board.BoardDTO"/>
<jsp:setProperty property="*" name="dto"/>
<%
int num = Integer.parseInt(request.getParameter("num"));
dto = boarddao.viewBoardPosts(num);
%>
<script type="text/javascript">
function check(){
if (f.writer.value == ""){
alert("이름을 입력해 주세요!!")
f.writer.focus()
return false
}
if (f.subject.value == ""){
alert("제목을 입력해 주세요!!")
f.subject.focus()
return false
}
if (f.content.value == ""){
alert("내용을 입력해 주세요!!")
f.content.focus()
return false
}
if (f.passwd.value == ""){
alert("비밀번호을 입력해 주세요!!")
f.passwd.focus()
return false
}
return true
}
</script>
<%
if (isLogin == false) {%>
<script type="text/javascript">
alert("로그인 해 주세요")
location.href="list.jsp"
</script>
<%
}
%>
<html>
<head>
<title>글 쓰기</title>
</head>
<body>
<form name="f" action="updatePro.jsp?num=<%= dto.getNum() %>" method="post" onsubmit="return check()">
<div align="center">
<table border="1" width="90%">
<tr bgcolor="lightgray">
<td colspan="2" align="center">글쓰기</td>
</tr>
<tr>
<td width="15%" align="center">이름</td>
<td><input type="text" name="writer" value="<%=id%>" tabindex="1" readOnly></td>
</tr>
<tr>
<td width="15%" align="center">제목</td>
<td><input type="text" name="subject" value="<%=dto.getSubject() %>" tabindex="2" style="width: 100%;"></td>
</tr>
<tr>
<td width="15%" align="center">이메일</td>
<td><input type="text" name="email" value="<%=dto.getEmail() %>" tabindex="3" style="width: 100%;"></td>
</tr>
<tr>
<td width="15%" align="center">내용</td>
<td><textarea name="content" tabindex="4" style="width: 100%;" rows="15"><%=dto.getContent()%></textarea></td>
</tr>
<tr>
<td width="15%" align="center">비밀번호</td>
<td><input type="password" tabindex="5" value="<%=dto.getPasswd() %>" name="password" readOnly></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="hidden" name="ip" value="<%=request.getRemoteAddr()%>">
<input type="submit" value="수정">
<input type="button" value="다시 하기"
onclick="window.location='updateForm.jsp?num=<%= num %>'">
<input type="button" value="목록 보기" onclick="location.href='<%=request.getContextPath()%>/board/list.jsp';">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
<%@ include file="../bottom.jsp"%>
게시글을 수정하고 싶다면 DB에 해당 num값인 DTO 객체를 불러와 write와 똑같은 환경에 text로 써 준다.
아이디와 비밀번호는 수정할 수 없도록 했다.
생각해 보니까 해당 아이디로 작성한 글만 수정할 수 있도록 했어야 했는데 왜 생각을 못 했지.
차후에 수정해야 한다.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<% request.setCharacterEncoding("EUC-KR"); %>
<jsp:useBean id="boarddao" class="my.board.BoardDAO"/>
<jsp:useBean id="pool" class="my.db.ConnectionPoolBean" scope="application"/>
<jsp:setProperty name="boarddao" property="pool" value="<%=pool%>"/>
<jsp:useBean id="boarddto" class="my.board.BoardDTO"/>
<jsp:setProperty property="*" name="boarddto"/>
<%
/*
if (boarddto.getWriter() == null || boarddto.getWriter().trim().equals("")){
response.sendRedirect("list.jsp");
return;
}
*/
int num = Integer.parseInt(request.getParameter("num"));
int res = boarddao.updateBoardNew(boarddto, num);
if (res > 0){%>
<script type="text/javascript">
alert("업데이트 성공 :게시판으로 이동합니다")
location.href="list.jsp"
</script>
<% }else {%>
<script type="text/javascript">
alert("업데이트 실패 : 다시 입력해 주세요")
location.href="writeForm.jsp"
</script>
<% }
%>
public int updateBoardNew(BoardDTO dto, int num) throws SQLException {
try {
con = pool.getConnection();
String sql = "update board set writer = ?, email = ?, subject = ?, "
+ "reg_date = sysdate, content = ? WHERE num = ?";
ps = con.prepareStatement(sql);
ps.setString(1, dto.getWriter());
ps.setString(2, dto.getEmail());
ps.setString(3, dto.getSubject());
ps.setString(4, dto.getContent());
ps.setInt(5, num);
int res = ps.executeUpdate();
return res;
} finally {
if (ps != null) ps.close();
if (con != null) pool.returnConnection(con);
}
}
여기서부터 약간 응용 + 한걸음 더가 들어가는 부분.
생각해 보자.
만약 게시글 답글 기능을 원한다면 생각해야 할 것.
답글 기능을 눌러 답글을 달았을 경우, 해당 글은 원글의 밑으로 향하게 할 것. 그리고 그 다음 답글은 더 밑으로 향하게 할 것.
그래서 두 가지 DB 값을 추가한다.
출력 시, re_step을 오름차순으로 나타낸다.
private int re_step;
private int re_level;
public int getRe_step() {
return re_step;
}
public void setRe_step(int re_step) {
this.re_step = re_step;
}
public int getRe_level() {
return re_level;
}
public void setRe_level(int re_level) {
this.re_level = re_level;
}
public int updateRe_step(BoardDTO dto, int num) throws SQLException{
try {
con = pool.getConnection();
String sql = "update board set re_step+1 WHERE num > ?";
ps = con.prepareStatement(sql);
ps.setInt(1, num);
int res = ps.executeUpdate();
return res;
} finally {
if (ps != null) ps.close();
if (con != null) pool.returnConnection(con);
}
}
public int updateRe_level(BoardDTO dto, int num) throws SQLException{
try {
con = pool.getConnection();
String sql = "update board set re_level+1 WHERE num > ?";
ps = con.prepareStatement(sql);
ps.setInt(1, num);
int res = ps.executeUpdate();
return res;
} finally {
if (ps != null) ps.close();
if (con != null) pool.returnConnection(con);
}
}
Re_step과 Re_level 을 관리해 주는 메서드를 추가해 새 글이 올라오면 답글일 경우 각 변수들의 값을 업데이트 해 준다.
public int UpdateBoard(BoardDTO dto) throws SQLException {
try {
con = pool.getConnection();
String sql = null;
if (dto.getNum() == 0) { //새글
sql = "update board set re_step = re_step + 1";
}else { //답글
sql = "update board set re_step = re_step + 1 where re_step > " + dto.getRe_step();
dto.setRe_step(dto.getRe_step() + 1);
dto.setRe_level(dto.getRe_level() + 1);
}
ps = con.prepareStatement(sql);
ps.executeUpdate();
// 새글 처리
sql = "insert into board values(board_seq.nextval, ?, ?, ?, ?, sysdate, ?, ?, ?, ?, ?)";
ps = con.prepareStatement(sql);
ps.setString(1, dto.getWriter());
ps.setString(2, dto.getEmail());
ps.setString(3, dto.getSubject());
ps.setString(4, dto.getPasswd());
ps.setInt(5, 0);
ps.setString(6, dto.getContent());
ps.setString(7, dto.getIp());
ps.setInt(8, dto.getRe_step());
ps.setInt(9, dto.getRe_level());
int res = ps.executeUpdate();
return res;
}finally{
if (ps != null) ps.close();
if (con != null) pool.returnConnection(con);
}
}
새로운 글과 답글이 올라오면 답글 관리하는 변수들을 설정해 주기.
list.jsp 는 밑에 기술함.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" import="java.util.*, my.board.*"%>
<% request.setCharacterEncoding("EUC-KR"); %>
<%@ include file="../top.jsp"%>
<!-- login.jsp-->
<link rel="stylesheet" type="text/css" href="../style.css">
<!-- find.jsp -->
<jsp:useBean id="boarddao" class="my.board.BoardDAO"/>
<jsp:useBean id="pool" class="my.db.ConnectionPoolBean" scope="application"/>
<jsp:setProperty name="boarddao" property="pool" value="<%=pool%>"/>
<%
// List<BoardDTO> list = boarddao.ListBoard();
int pageSize = 5;
String pageNum = request.getParameter("pageNum");
if (pageNum == null){
pageNum = "1";
}
int currentPage= Integer.parseInt(pageNum);
int startRow = (currentPage-1)*pageSize + 1;
int endRow = startRow + pageSize -1;
int count = boarddao.getCount();
if (endRow > count) endRow = count;
int co = count - startRow+1;
%>
<html>
<head>
<title>글 목록</title>
</head>
<body>
<div align="center">
<hr color="green" width="300">
<h2>글 목 록</h2>
<hr color="green" width="300">
<table borde="0" width="100%" class="outline">
<tr>
<td class="m1" colspan="5"></td>
<td class="m1" align="center"><a href="writeForm.jsp">글쓰기</a></td>
</tr>
<tr bgcolor="limegreen">
<th class="m1" width="10%">번호</th>
<th class="m1" width="30%">제목</th>
<th class="m1" width="15%">작성자</th>
<th class="m1" width="20%">작성일</th>
<th class="m1" width="10%">조회</th>
<th class="m1" width="20%">ip</th>
</tr>
<%
List<BoardDTO> list = boarddao.ListBoard(startRow, endRow);
if (list == null || list.size() == 0){
%>
<tr>
<td colspan="6">글이 등록되어 있지 않습니다.</td>
</tr>
<%
}else {
for(BoardDTO dto : list){
%>
<tr>
<td align="center"><%=co--%></td>
<td>
<% if (dto.getRe_level()>0){ %>
<img src="../img/level.gif" width="<%=dto.getRe_level()*10%>"/>
<img src="../img/re.gif">
<% } %>
<a href="content.jsp?num=<%=dto.getNum()%>&co=<%=co %>">
<%=dto.getSubject()%>
</a>
<% if (dto.getReadcount() > 10){ %>
<img src="../img/hot.gif">
<% } %>
</td>
<td align="center"><%=dto.getWriter()%></td>
<td align="center"><%=dto.getReg_date()%></td>
<td align="center"><%=dto.getReadcount()%></td>
<td align="center"><%=dto.getIp()%></td>
</tr>
<% }
}%>
</table><br>
<%
if (count>0) {
int pageBlock=3;
int pageCount = count/pageSize + (count%pageSize == 0 ? 0 : 1);
int startPage = (currentPage-1)/pageBlock*pageBlock+1;
int endPage = startPage + pageBlock - 1;
if (endPage>pageCount) endPage= pageCount;
if (startPage > pageBlock){%>
<a href="list.jsp?pageNum=<%=startPage-3%>">[이전]</a>
<% }
for (int i=startPage; i<=endPage; ++i){%>
<a href="list.jsp?pageNum=<%=i%>">[<%=i%>]</a>
<% }
if (endPage < pageCount){%>
<a href="list.jsp?pageNum=<%=startPage+3%>">[다음]</a>
<% }%>
<%
}
%>
</div>
</body>
</html>
<%@ include file="../bottom.jsp"%>
public int getCount() throws SQLException{
try {
con = pool.getConnection();
String sql = "select count(*) from board";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
rs.next();
int count = rs.getInt(1);
return count;
} finally {
if (rs != null) rs.close();
if (ps != null) ps.close();
if (con != null) pool.returnConnection(con);
}
}
public List<BoardDTO> ListBoard(int start, int end) throws SQLException{
try {
con = pool.getConnection();
String sql = "select * from "
+ "(select rownum rn, A.* from "
+ "(select * from board re_step order by re_step asc)A) "
+ "where rn between ? and ?";
// rn 값을 다 해 주고 나머지는 A의 모든 것을 정렬하겠다.
// rownum : 그냥 숫자 붙여 주는 거
ps = con.prepareStatement(sql); // sql 문의 결과를 가져옴
ps.setInt(1, start);
ps.setInt(2, end);
ResultSet rs = ps.executeQuery(); // rs 쿼리 문의 결과를 담음
List<BoardDTO> list = makeList(rs);
return list;
}finally{
if (rs != null) rs.close();
if (ps != null) ps.close();
if (con != null) pool.returnConnection(con);
}
}
내가 지금까지 뭐하고 있었나 싶다. 적성에 맞는 일을 찾으니까 그 일을 위해서 뭘 하고 싶다. 원랜 다른 사람들이 자기 꿈을 위해 뭘 하는 걸 보면 대체 어떻게 저런 걸 찾지.... 나는 너무 게으른 것 같다. 라고 생각하고 가식일 거라고 생각했는데 내가 막상 하고 싶은 일을 찾으니까 직접 찾아서 하고 있다.
내 적성에 맞고, 내가 하고 싶어서 하는 일이니까 국비 끝날 때까지 열심히 해 보자.