Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- flex3
- 명사 추출기
- 디즈니씨
- ror실행
- 메일왕창보내는법
- php thumbnail
- 주식이야기
- 베트남어
- php
- 명사 뽑아내기
- node.js web framework
- 스킨 스쿠버
- Eclipse
- express for node.js
- nodejs express
- 책이야기
- C/C++
- rss
- docker
- ejb
- Cross
- 명사 분석기
- Lift
- scala
- Node.js
- 도커
- iBatis
- 나의 프로젝트
- ajax
- 나의 취미
Archives
- Today
- Total
nkdk의 세상
25장 자세히 보기, 댓글, 에디트부분 본문
게시판의 자세히 보기 부분, 댓글부분, 에디트 부분입니다.
// boardMgr.java
package pack;
import java.sql.*;
public class BoardMgr {
Connection conn;
PreparedStatement pstmt;
ResultSet rs;
DBConnectionMgr pool;
public BoardMgr(){
try {
pool = DBConnectionMgr.getInstance();
conn = pool.getConnection();
} catch (Exception e) {
System.out.println("BoardMgr DB연결 실패: "+e);
return;
}
}
public int currentGetnum() {
// 레코드 최대 번호 획득
int cnt = 0;
try{
String sql = "select max(num) as num from board";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
rs.next();
cnt = rs.getInt("num");
System.out.println("숫자:"+cnt);
} catch (Exception e) {
System.out.println("SQL오류 : "+e);
}finally{
try {
pool.freeConnection(conn,pstmt,rs);
} catch (Exception e) {}
}
System.out.println("ss" + cnt);
return cnt;
}
public boolean updateData(BoardData data) {
boolean cnt = false;
try {
String sql = "update board set name='"+data.getName()+"', mail='"+data.getMail()+
"', title='"+data.getTitle()+"', cont='"+ data.getCont()+"', bip='"+
data.getBip() + "' where num="+data.getNum();
conn = pool.getConnection();
System.out.println(sql);
pstmt = conn.prepareStatement(sql);
int re = pstmt.executeUpdate();
if(re > 0) cnt = true;
} catch (Exception e) {
System.out.println("저장실패:"+e);
}finally{
try {
pool.freeConnection(conn,pstmt);
} catch (Exception e) {}
}
return cnt;
}
public boolean saveData(BoardData data) {
boolean cnt = false;
try {
String sql = "insert into board values(?,?,?,?,?,?,?,?,?,?,?,?)";
conn = pool.getConnection();
System.out.println(sql);
System.out.println(currentGetnum()+" "+data.getName()+" "+data.getPass()+" "+data.getMail()+ " "
+ data.getTitle()+ " " + data.getCont()+ " " +data.getBip() + " " + data.getDate() + " "
+ data.getReadcnt() + " " + data.getNum());
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, data.getNum());
pstmt.setString(2, data.getName());
pstmt.setString(3, data.getPass());
pstmt.setString(4, data.getMail());
pstmt.setString(5, data.getTitle());
pstmt.setString(6, data.getCont());
pstmt.setString(7, data.getBip());
pstmt.setString(8, data.getDate());
pstmt.setInt(9, 0);
pstmt.setInt(10, data.getGnum());
pstmt.setInt(11, 0);
pstmt.setInt(12, 0);
int re = pstmt.executeUpdate();
if(re > 0) cnt = true;
} catch (Exception e) {
System.out.println("저장실패:"+e);
}finally{
try {
pool.freeConnection(conn,pstmt);
} catch (Exception e) {}
}
return cnt;
}
}
// boardlist.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"
import="pack.*,java.util.*"
%>
<%
request.setCharacterEncoding("EUC-KR");
%>
<jsp:useBean id="mgr" class="pack.BoardListMgr"/>
<jsp:useBean id="data" class="pack.BoardData" />
<html><head><title>게시판</title>
<link rel ="stylesheet" type="text/css" href="board.css">
<script>
function check(){
if(frm.sword.value=="") {
alert("검색어를 입력하시오");
frm.sword.focus();
return;
}else
frm.submit();
}
function link(page) {
document.frm.page.value=page;
document.frm.submit();
}
</script>
</head>
<body topmargin=20 leftmargin=50>
<form name="frm" action="boardlist.jsp" method="post">
<input type="hidden" name="page" value="<%=spage %>">
<table align=center border=1 style="border-collapse=collapse" width="700" height="500">
<tr>
<td height="50" align="center">
<img src="images/bar.gif">
</td>
</tr>
<tr>
<td align="center" valign="top">
[<a href="index.jsp" onFocus=blur()>메인으로 </a>]
[<a href="boardlist.jsp?page=1" onFocus=blur()>최근목록 </a>]
[<a href="boardwrite.jsp" onFocus=blur()>새글 작성 </a>]
[<a href="#" onFocus=blur() onClick="window.open('admin.jsp','','width=300,height=150')">관리자용</a>]
<br><br>
<table cellspacing = 2 cellpadding = 5>
<tr align="center" bgcolor="#e2e2e2">
<td width="50">번호</td>
<td width="250">제목</td>
<td width="150">작성자</td>
<td width="100">날짜</td>
<td width="50">조회수</td>
</tr>
<%!
int pageblock, start, end, spage=1, pagesu;
String spages;
%>
<%
spages = request.getParameter("page");
String type = request.getParameter("stype");
String word = request.getParameter("sword");
try {
spage = Integer.parseInt(spages);
}catch(Exception ex) {}
pageblock = 3;
start = ((int)(Math.ceil((double)spage / (double)pageblock)) -1) * pageblock + 1;
end = start + (pageblock -1);
// 레코드 전체 갯수
mgr.totalList(word, type);
pagesu = mgr.getPageSu();
if(end > pagesu) end = pagesu;
Vector vc = mgr.getList(word, type, spage);
for(int i =0; i<vc.size(); i++) {
data = (BoardData)vc.get(i);
int nst= data.getNested();
String tab=""; //들여쓰기 공백
String rep=""; //들여쓰기 아이콘
for(int k = 0;k<nst; k++) {
tab += "&&";
rep += "<img src='images/re.gif' border='0'>";
}
%>
<tr align="center" bgcolor="#efefef">
<td><%=data.getNum() %></td>
<td align="left" style = "padding-left=15">
<%=tab %><a href="boardcontent.jsp?snum=<%=data.getNum() %>&page=<%=spage %>" onFocus=blur()><%=rep %><%=data.getTitle() %></a>
</td>
<td><%=data.getName() %></td>
<td><%=data.getDate() %></td>
<td><%=data.getReadcnt() %></td>
<% } %>
</table>
<br>
<table width="500">
<tr align="center">
<td>
<%
// 페이지 단위 출력 표시
// for(int i = 1; i<= pagesu; i++) {
// out.print("<a href=boardlist.jsp?page=" + i + ">[" + i + "]</a>");
// }
if( start != 1) {
int pre = start -1;
out.println("<a href=\'javascript:link(" + pre + ");\'>[이전 " + pre + "개]</a>");
}
%>
<span style="font-size=9pt;"></span>
<%
for(int i = start; i<= end; i++) {
if(i == spage)
out.print("<font color=red><b>[" +i+"]</b></font>&");
else out.print("<a href=boardlist.jsp?page=" + i + ">["+i+"]</a>&");
}
%>
&
<%
if(end<pagesu) {
int next = end + 1;
out.println("<a href=\'javascript:link(" + next + ");\'>[다음 " + (pagesu - end) + "개]</a>");
}
// 페이지 단위 출력 종료
%>
<br><br>
<select name=stype>
<option selected value="title">글제목
<option value="cont">글내용
<option value="name">작성자
</select>&
<input type="text" name="sword">
<input type="button" value=" 검 색 " onClick=check()>
<br>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="50" align="center">
<img src="images/bar.gif">
</td>
</tr> </table>
</form>
</body></html>
// boardsave.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" %>
<% request.setCharacterEncoding("EUC-KR"); %>
<jsp:useBean id="data" class="pack.BoardData" />
<jsp:setProperty property="*" name="data" />
<% // 키보드를 통해 5개 항목은 채움 : name, pass, mail, title, cont %>
<jsp:useBean id="mgr" class="pack.BoardMgr" />
<% // 키보드를 통해 5개 항목은 채움 : name, pass, mail, title, cont
data.setName(request.getParameter("name"));
data.setPass(request.getParameter("pass"));
data.setMail(request.getParameter("mail"));
data.setTitle(request.getParameter("title"));
data.setCont(request.getParameter("cont"));
data.setBip(request.getRemoteAddr()); // IP를 취한다.
data.setDate(); // 작성일
int max = mgr.currentGetnum() + 1;
data.setNum(max); // 레코드 번호
data.setGnum(max); // 해당 레코드 댓글 그룹 번호
out.println("<html><body>");
if(mgr.saveData(data)) {
out.println("저장성공");
}else {
out.println("저장실패");
}
response.sendRedirect("boardlist.jsp?page=1");
out.println("</html></body>");
%>
// boardcontent.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<jsp:useBean id="data" class="pack.BoardData" />
<jsp:useBean id="mgr" class="pack.BoardContMgr" />
<%!
int snum;
String spage;
%>
<html><head>
<title>게시물의 내용을 본다</title>
<link rel="stylesheet" type="text/css" href="board.css" />
</head><body topmargin=50 leftmargin=50>
<table border=1 style="border-collapse=collapse" width=530 height=400>
<tr>
<td height="50" align="center">
<img src="images/bar.gif">
</td>
</tr>
<tr>
<td align=center valign=top>
<%
snum=Integer.parseInt(request.getParameter("snum"));
spage = request.getParameter("page");
System.out.println(snum + ", "+ spage);
mgr.updateCnt(snum); // 조회수 증가
data = mgr.getCont(snum); // 해당 자료 읽기
String name = data.getName();
String pass = data.getPass();
String mail = data.getMail();
String title = data.getTitle();
String cont = data.getCont();
String bip = data.getBip();
String bdate = data.getDate();
int cnt = data.getReadcnt();
System.out.printf("%s, %s, %s, %s, %s, %s, %s, %d", name, pass, mail, title, cont, bip, bdate, cnt);
//암호는 관리자로 로그인 한 경우에만 화면에 표시
String apass = "****";
String admi = (String)session.getAttribute("adminOk");
if(admi != null) {
if(admi.equals("admin")) apass=pass;
}
%>
<br>
<table width=500>
<tr>
<td style="color=#cccccc"><b>비밀번호:<%=apass %></b></td>
<td align=right>
<a href="reply.jsp?num=<%=snum %>&page=<%=spage %>" onfocus=blur()><img src="images/reply.gif" border=0></a>&
<a href="boardwrite.jsp" onfocus=blur()><img src="images/write.gif" border=0></a>&
<a href="edit.jsp?num=<%=snum %>&page=<%=spage %>" onfocus=blur()><img src="images/edit.gif" border=0></a>&
<a href="delete.jsp?num=<%=snum %>&page=<%=spage %>" onfocus=blur()><img src="images/del.gif" border=0></a>&
</table>
<table width=500 height=250>
<tr bgcolor="#efefef" height=25>
<td width=300 style="padding-left=2">작성:<a href="mailto:<%=mail %>" onFocus=blur()><%=name %></a> IP<%=bip %></td>
<td width=100 align=center><%=bdate %></td>
<td width=100 align=center>조회:<%=cnt %></td>
</tr>
<tr bgcolor="#elelel" height=25>
<td colspan=3 style="padding-left=8">제목 :<b><%=title %></b></td></tr>
<tr bgcolor="#efefef">
<td colspan=3 style="padding-left=10">
<textarea rows="15" cols="100%" readonly><%=cont %></textarea>
</td>
</tr>
</table>
<br>
[<a href="index.jsp" onfocus=blur()>메인으로</a>]&
[<a href="boardlist.jsp?page="<%=spage %> onfocus=blur()>목록으로</a>]
</td>
</tr>
</table>
</body></html>
// BoardReplyMgr.java
package pack;
import java.sql.*;
import java.util.Vector;
public class BoardReplyMgr {
Connection conn;
PreparedStatement pstmt;
ResultSet rs;
DBConnectionMgr pool;
public BoardReplyMgr() {
try {
pool = DBConnectionMgr.getInstance();
conn = pool.getConnection();
} catch (Exception e) {
System.out.println("BoardReplyMgr DB연결 실패: "+e);
return;
}
}
public BoardData getData(int num) {
BoardData bda = new BoardData();
String sql = "select title, gnum, onum, nested from board where num = " + num;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()) {
bda.setTitle(rs.getString("title"));
bda.setGnum(rs.getInt("gnum"));
bda.setOnum(rs.getInt("onum"));
bda.setNested(rs.getInt("nested"));
}
} catch (Exception e) {
System.out.println("getData 오류 : " + e);
} finally {
pool.freeConnection(conn, pstmt, rs);
}
return bda;
}
public void updateOnum(int gn, int on) {
// 답변 글의 onum, 이미 db에 있는 onum보다 크거나 같은 값은 변경한다.
// 만약 1을 가지고 오면 1이상인, 만약 2를 가지고 오면 2이상인
// 동일 그룹 내의gnum은 1씩 증가 되도록 한다.
String sql = "update board set onum=onum + 1 where onum >= ? and gnum = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, on);
pstmt.setInt(2,gn);
pstmt.executeUpdate();
} catch (Exception e) {
System.out.println("updateOnum 오류:"+e);
}finally{
try {
if(pstmt != null) pstmt.close();
} catch (Exception e) {}
}
}
public int currentMaxNum() {
int max = 0;
String sql = "select max(num) from board";
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()) {
max = rs.getInt(1);
}
} catch (Exception e) {
System.out.println("currentMaxNum 오류:"+ e);
}finally{
try {
if(pstmt != null) pstmt.close();
if(rs != null) rs.close();
} catch (Exception e) {}
}
return max;
}
public void saveReplyData(BoardData data) {
String sql = "insert into board values(?,?,?,?,?,?,?,?,?,?,?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, data.getNum());
pstmt.setString(2, data.getName());
pstmt.setString(3, data.getPass());
pstmt.setString(4, data.getMail());
pstmt.setString(5, data.getTitle());
pstmt.setString(6, data.getCont());
pstmt.setString(7, data.getBip());
pstmt.setString(8, data.getDate());
pstmt.setInt(9, 0);
pstmt.setInt(10, data.getGnum());
pstmt.setInt(11, data.getOnum());
pstmt.setInt(12, data.getNested());
pstmt.executeUpdate();
} catch (Exception e) {
System.out.println("saveReplyData 오류: "+e);
}finally{
pool.freeConnection(conn,pstmt,rs);
}
}
}
// BoardListMgr.java
package pack;
import java.sql.*;
import java.util.*;
public class BoardListMgr {
Connection conn;
PreparedStatement pstmt;
ResultSet rs;
DBConnectionMgr pool;
int tot;
int plist = 10;
int pagesu;
public BoardListMgr(){
try {
pool = DBConnectionMgr.getInstance();
conn = pool.getConnection();
} catch (Exception e) {
System.out.println("BoardMgr DB연결 실패: "+e);
return;
}
}
public int currentGetnum() {
// 레코드 최대 번호 획득
int cnt = 0;
try{
String sql = "select max(num) as num from board";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
rs.next();
cnt = rs.getInt("num");
System.out.println("숫자:"+cnt);
} catch (Exception e) {
System.out.println("SQL오류 : "+e);
}finally{
try {
pool.freeConnection(conn,pstmt,rs);
} catch (Exception e) {}
}
System.out.println("ss" + cnt);
return cnt;
}
public boolean saveData(BoardData data) {
boolean cnt = false;
try {
String sql = "insert into board values(?,?,?,?,?,?,?,?,?,?,?,?)";
conn = pool.getConnection();
System.out.println(sql);
System.out.println(currentGetnum()+" "+data.getName()+" "+data.getPass()+" "+data.getMail()+ " "
+ data.getTitle()+ " " + data.getCont()+ " " +data.getBip() + " " + data.getDate() + " "
+ data.getReadcnt() + " " + data.getNum());
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, data.getNum());
pstmt.setString(2, data.getName());
pstmt.setString(3, data.getPass());
pstmt.setString(4, data.getMail());
pstmt.setString(5, data.getTitle());
pstmt.setString(6, data.getCont());
pstmt.setString(7, data.getBip());
pstmt.setString(8, data.getDate());
pstmt.setInt(9, 0);
pstmt.setInt(10, data.getGnum());
pstmt.setInt(11, 0);
pstmt.setInt(12, 0);
int re = pstmt.executeUpdate();
if(re > 0) cnt = true;
} catch (Exception e) {
System.out.println("저장실패:"+e);
}finally{
try {
pool.freeConnection(conn,pstmt);
} catch (Exception e) {}
}
return cnt;
}
public void totalList(String word, String type) {
String sql = "select count(*) from board ";
try{
if(word == null) {
pstmt = conn.prepareStatement(sql);
}else{
sql+= " where " + type + " like ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%" + word + "%");
}
rs = pstmt.executeQuery();
rs.next();
tot = rs.getInt(1);
System.out.println("전체 갯수 : " +tot);
}catch(Exception ex) {
System.out.println("sql오류!!: "+ ex);
}
}
public Vector getList(String word, String type, int page) {
Vector<BoardData> vc = new Vector<BoardData>();
BoardData data = null;
String sql = "select * from board";
try{
if(word != null) {
sql += " where " + type + " like ?";
sql += " order by gnum desc, onum asc";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, "%" + word + "%");
}else {
sql += " order by gnum desc, onum asc";
pstmt = conn.prepareStatement(sql);
}
rs = pstmt.executeQuery();
for(int i = 0; i< (page - 1 ) * plist; i++) {
try {
rs.next();
} catch (Exception e) {}
}int k = 0;
while(rs.next() && k < plist) {
int n = rs.getInt("num");
String name = rs.getString("name");
String title = rs.getString("title");
String date = rs.getString("bdate");
int rcnt = rs.getInt("readcnt");
int nested = rs.getInt("nested");
data = new BoardData();
data.setNum(n);
data.setName(name);
data.setTitle(title);
data.setDate(date);
data.setReadcnt(rcnt);
data.setNested(nested);
vc.add(data);
k++;
}
}catch(Exception e) {
System.out.println("자료 가져 오기 오류" +e);
}finally{
pool.freeConnection(conn, pstmt, rs);
}
return vc;
}
public int getPageSu() {
pagesu = tot / plist;
if(tot % plist > 0) pagesu++;
return pagesu;
}
}
// boardwrite.jsp
<%@ page contentType="text/html; charset=EUC-KR" %>
<html><head><title>게시판</title>
<link rel="stylesheet" type="text/css" href="board.css">
<script>
function check(){
if(frm.name.value==""){
alert("이름을 입력하세요");
frm.name.focus();
}else if(frm.pass.value ==""){
alert("비밀번호를 입력하세요");
frm.pass.focus();
}else if(frm.mail.value ==""){
alert("이메일을 입력하세요");
frm.mail.focus();
}else if(frm.title.value ==""){
alert("제목을 입력하세요");
frm.title.focus();
}else if(frm.cont.value ==""){
alert("내용을 입력하세요");ㄱ
frm.cont.focus();
}else
frm.submit();
}
</script>
</head>
<body topmargin=50 leftmargin=50 onLoad=frm.name.focus()>
<center><h2>*** 글쓰기 ***</h2>
<form name=frm method=post action="boardsave.jsp">
<table border=1 style="border-collapse=collapse" width=530>
<tr><td colspan=2 height=50 align=center><img src="images/bar.gif"></td></tr>
<tr>
<td align=center width=100>이 름</td>
<td width=430><input name=name size=15></td>
</tr>
<tr>
<td align=center >암 호</td>
<td width=430><input type=password name=pass size=15></td>
</tr>
<tr>
<td align=center>메 일</td>
<td><input name=mail size=25></td>
</tr>
<tr>
<td align=center>제 목</td>
<td><input name=title size=50></td>
</tr>
<tr>
<td align=center>내 용</td>
<td><textarea name=cont cols=50 rows=12></textarea></td>
</tr>
<tr>
<td colspan=2 align=center height=30>
<input type=button value="메 인" onClick="location.href('index.jsp')"> &
<input type=button value="작 성" onClick="check()" > &
<input type=button value="목 록" onClick="location.href('boardlist.jsp')">
</td>
</tr>
<tr><td colspan=2 height=50 align=center><img src="images/bar.gif"></td></tr>
</table></form></center></body></html>
// boardContMgr.java
package pack;
import java.sql.*;
import java.util.Vector;
public class BoardContMgr {
Connection conn;
PreparedStatement pstmt;
ResultSet rs;
DBConnectionMgr pool;
public BoardContMgr() {
try {
pool = DBConnectionMgr.getInstance();
conn = pool.getConnection();
} catch (Exception e) {
System.out.println("BoardContMgr DB연결 실패: "+e);
return;
}
}
public void updateCnt(int num){
String sql="update board set readcnt=readcnt+1 where num=?";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, num);
pstmt.executeUpdate();
} catch (Exception e) {
System.out.println("updateCnt 에러:"+e);
}finally{
try {
if(pstmt != null) pstmt.close();
} catch (Exception e) {
}
}
}
public BoardData getCont(int num) {
BoardData bda = null;
// 이름, 암호, 메일, 제목, 내용, 아이피, 작성일, 조회수
Vector<BoardData> vc = new Vector<BoardData>();
String sql = "select * from board where num = "+num;
System.out.println(sql);
try{
bda = new BoardData();
pstmt=conn.prepareStatement(sql);
rs = pstmt.executeQuery();
rs.next();
int k = 0;
int n = rs.getInt("num");
String name = rs.getString("name");
String pass = rs.getString("pass");
String mail = rs.getString("mail");
String title = rs.getString("title");
String cont = rs.getString("cont");
String bip = rs.getString("bip");
String date = rs.getString("bdate");
int rcnt = rs.getInt("readcnt");
System.out.println(name+pass+mail+title+cont+bip+date);
bda.setNum(n);
bda.setName(name);
bda.setPass(pass);
bda.setMail(mail);
bda.setTitle(title);
bda.setCont(cont);
bda.setBip(bip);
bda.setDate(date);
bda.setReadcnt(rcnt);
vc.add(bda);
}catch(Exception e) {
System.out.println("자료 가져 오기 오류" +e);
}finally{
pool.freeConnection(conn, pstmt, rs);
}
return bda;
}
}
// replysave.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
request.setCharacterEncoding("EUC-KR");
%>
<jsp:useBean id="data" class="pack.BoardData" />
<jsp:setProperty name="data" property="*" />
<jsp:useBean id="mgr" class="pack.BoardReplyMgr" />
<%
String spage = request.getParameter("page");
// 원글의 onum과 nested를 1씩 증가
int gnum = data.getGnum();
int onum = data.getOnum() +1;
int nst=data.getNested() +1;
//gnum 그룹의 onum 값 증가된 자료를 DB에 저장
mgr.updateOnum(gnum, onum);
// 댓글 저장
data.setOnum(onum);
data.setNested(nst);
data.setGnum(gnum);
data.setBip(request.getRemoteAddr());
data.setNum(mgr.currentMaxNum() +1);
data.setDate();
mgr.saveReplyData(data);
response.sendRedirect("boardlist.jsp?page=" + spage);
%>
// reply.jsp
<%@ page contentType="text/html; charset=EUC-KR" %>
<jsp:useBean id="data" class="pack.BoardData" />
<jsp:useBean id="mgr" class="pack.BoardReplyMgr" />
<%!
int snum;
String spage;
%>
<html><head><title>댓글</title>
<link rel="stylesheet" type="text/css" href="board.css">
<script>
function check(){
if(frm.name.value==""){
alert("이름을 입력하세요");
frm.name.focus();
}else if(frm.pass.value ==""){
alert("비밀번호를 입력하세요");
frm.pass.focus();
}else if(frm.mail.value ==""){
alert("이메일을 입력하세요");
frm.mail.focus();
}else if(frm.cont.value ==""){
alert("내용을 입력하세요");
frm.cont.focus();
}else
frm.submit();
}
</script>
</head>
<body topmargin=50 leftmargin=50 onLoad=frm.name.focus()>
<center><h2>*** 답글 쓰기 ***</h2>
<%
request.setCharacterEncoding("EUC-KR");
snum = Integer.parseInt(request.getParameter("num"));
spage = request.getParameter("page");
//원글의 gnum, onum, nested, title 가져오기
data = mgr.getData(snum);
int gnum = data.getGnum();
int onum = data.getOnum();
int nested = data.getNested();
String title = data.getTitle();
%>
<form name=frm method=post action="editsave.jsp">
<table border=4 bordercolor="#AAAAAA" style="border-collapse=collapse" width=530 cellspacing="1" cellpadding="0">
<tr><td colspan=2 height=50 align=center><img src="images/bar.gif"></td></tr>
<tr bgcolor="#BBEEBB">
<input type="hidden" name="num" value="<%=snum %>">
<input type="hidden" name="page" value="<%=spage %>">
<input type="hidden" name="gnum" value="<%=gnum %>">
<input type="hidden" name="onum" value="<%=onum %>">
<input type="hidden" name="nested" value="<%=nested %>">
<td align=center width=100>이 름</td>
<td width=430><input name=name size=15></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center >암 호</td>
<td width=430><input type=password name=pass size=15></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center>메 일</td>
<td><input name=mail size=25></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center>제 목</td>
<td><input name=title size=50 value="[RE] <%=title %>"></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center>내 용</td>
<td><textarea name=cont cols=50 rows=12></textarea></td>
</tr>
<tr bgcolor="#BBEEBB">
<td colspan=2 align=center height=30>
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="메 인" onClick="location.href('index.jsp')"> &
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="작 성" onClick="check()" > &
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="목 록" onClick="location.href('boardlist.jsp?page=<%=spage %>')">
</td>
</tr>
<tr><td colspan=2 height=50 align=center><img src="images/bar.gif"></td></tr>
</table></form>
</center></body></html>
// edit.jsp
<%@ page contentType="text/html; charset=EUC-KR" %>
<jsp:useBean id="data" class="pack.BoardData" />
<jsp:useBean id="mgr" class="pack.editMgr" />
<%!
int snum;
String spage;
%>
<html><head><title>글고침</title>
<link rel="stylesheet" type="text/css" href="board.css">
<script>
function check(){
if(frm.name.value==""){
alert("이름을 입력하세요");
frm.name.focus();
}else if(frm.pass.value ==""){
alert("비밀번호를 입력하세요");
frm.pass.focus();
}else if(frm.mail.value ==""){
alert("이메일을 입력하세요");
frm.mail.focus();
}else if(frm.cont.value ==""){
alert("내용을 입력하세요");
frm.cont.focus();
}else
frm.submit();
}
</script>
</head>
<body topmargin=50 leftmargin=50 onLoad=frm.name.focus()>
<center><h2>*** 글 수정 ***</h2>
<%
request.setCharacterEncoding("EUC-KR");
snum = Integer.parseInt(request.getParameter("num"));
spage = request.getParameter("page");
//원글의 gnum, onum, nested, title 가져오기
data = mgr.getData(snum);
int gnum = data.getGnum();
int onum = data.getOnum();
int nested = data.getNested();
String title = data.getTitle();
String cont = data.getCont();
String mail = data.getMail();
String name = data.getName();
String pass = data.getPass();
%>
<form name=frm method=post action="editsave.jsp">
<table border=4 bordercolor="#AAAAAA" style="border-collapse=collapse" width=530 cellspacing="1" cellpadding="0">
<tr><td colspan=2 height=50 align=center><img src="images/bar.gif"></td></tr>
<tr bgcolor="#BBEEBB">
<input type="hidden" name="num" value="<%=snum %>">
<input type="hidden" name="page" value="<%=spage %>">
<input type="hidden" name="gnum" value="<%=gnum %>">
<input type="hidden" name="onum" value="<%=onum %>">
<input type="hidden" name="nested" value="<%=nested %>">
<td align=center width=100>이 름</td>
<td width=430><input name=name size=15 value="<%=name %>"></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center >암 호</td>
<td width=430><input type=password name=pass size=15 value="<%=pass %>"></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center>메 일</td>
<td><input name=mail size=25 value="<%=mail %>"></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center>제 목</td>
<td><input name=title size=50 value="<%=title %>"></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center>내 용</td>
<td><textarea name=cont cols=50 rows=12><%= cont %></textarea></td>
</tr>
<tr bgcolor="#BBEEBB">
<td colspan=2 align=center height=30>
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="메 인" onClick="location.href('index.jsp')"> &
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="작 성" onClick="check()" > &
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="목 록" onClick="location.href('boardlist.jsp?page=<%=spage %>')">
</td>
</tr>
<tr><td colspan=2 height=50 align=center><img src="images/bar.gif"></td></tr>
</table></form>
</center></body></html>
// editMgr.java
package pack;
import java.sql.*;
public class editMgr {
Connection conn;
PreparedStatement pstmt;
ResultSet rs;
DBConnectionMgr pool;
public editMgr() {
try {
pool = DBConnectionMgr.getInstance();
conn = pool.getConnection();
} catch (Exception e) {
System.out.println("editMgr DB연결 실패: "+e);
return;
}
}
public BoardData getData(int num) {
BoardData bda = new BoardData();
String sql = "select * from board where num = " + num;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
rs.next();
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
System.out.println(rs.getString(4));
System.out.println(rs.getString(5));
System.out.println(rs.getString(6));
System.out.println(rs.getString(7));
System.out.println(rs.getString(8));
bda.setTitle(rs.getString("title"));
bda.setGnum(rs.getInt("gnum"));
bda.setOnum(rs.getInt("onum"));
bda.setNested(rs.getInt("nested"));
bda.setCont(rs.getString("cont"));
bda.setMail(rs.getString("mail"));
bda.setPass(rs.getString("pass"));
bda.setName(rs.getString("name"));
} catch (Exception e) {
System.out.println("getData 오류 : " + e);
} finally {
pool.freeConnection(conn, pstmt, rs);
}
return bda;
}
}
// editsave.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" %>
<% request.setCharacterEncoding("EUC-KR"); %>
<jsp:useBean id="data" class="pack.BoardData" />
<jsp:setProperty property="*" name="data" />
<% // 키보드를 통해 5개 항목은 채움 : name, pass, mail, title, cont %>
<jsp:useBean id="mgr" class="pack.BoardMgr" />
<% // 키보드를 통해 5개 항목은 채움 : name, pass, mail, title, cont
data.setName(request.getParameter("name"));
data.setPass(request.getParameter("pass"));
data.setMail(request.getParameter("mail"));
data.setTitle(request.getParameter("title"));
data.setCont(request.getParameter("cont"));
data.setBip(request.getRemoteAddr()); // IP를 취한다.
out.println("<html><body>");
if(mgr.updateData(data)) {
out.println("저장성공");
}else {
out.println("저장실패");
}
response.sendRedirect("boardcontent.jsp?snum="+request.getParameter("num")+"&page="+request.getParameter("spage"));
out.println("</html></body>");
%>
// boardMgr.java
package pack;
import java.sql.*;
public class BoardMgr {
Connection conn;
PreparedStatement pstmt;
ResultSet rs;
DBConnectionMgr pool;
public BoardMgr(){
try {
pool = DBConnectionMgr.getInstance();
conn = pool.getConnection();
} catch (Exception e) {
System.out.println("BoardMgr DB연결 실패: "+e);
return;
}
}
public int currentGetnum() {
// 레코드 최대 번호 획득
int cnt = 0;
try{
String sql = "select max(num) as num from board";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
rs.next();
cnt = rs.getInt("num");
System.out.println("숫자:"+cnt);
} catch (Exception e) {
System.out.println("SQL오류 : "+e);
}finally{
try {
pool.freeConnection(conn,pstmt,rs);
} catch (Exception e) {}
}
System.out.println("ss" + cnt);
return cnt;
}
public boolean updateData(BoardData data) {
boolean cnt = false;
try {
String sql = "update board set name='"+data.getName()+"', mail='"+data.getMail()+
"', title='"+data.getTitle()+"', cont='"+ data.getCont()+"', bip='"+
data.getBip() + "' where num="+data.getNum();
conn = pool.getConnection();
System.out.println(sql);
pstmt = conn.prepareStatement(sql);
int re = pstmt.executeUpdate();
if(re > 0) cnt = true;
} catch (Exception e) {
System.out.println("저장실패:"+e);
}finally{
try {
pool.freeConnection(conn,pstmt);
} catch (Exception e) {}
}
return cnt;
}
public boolean saveData(BoardData data) {
boolean cnt = false;
try {
String sql = "insert into board values(?,?,?,?,?,?,?,?,?,?,?,?)";
conn = pool.getConnection();
System.out.println(sql);
System.out.println(currentGetnum()+" "+data.getName()+" "+data.getPass()+" "+data.getMail()+ " "
+ data.getTitle()+ " " + data.getCont()+ " " +data.getBip() + " " + data.getDate() + " "
+ data.getReadcnt() + " " + data.getNum());
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, data.getNum());
pstmt.setString(2, data.getName());
pstmt.setString(3, data.getPass());
pstmt.setString(4, data.getMail());
pstmt.setString(5, data.getTitle());
pstmt.setString(6, data.getCont());
pstmt.setString(7, data.getBip());
pstmt.setString(8, data.getDate());
pstmt.setInt(9, 0);
pstmt.setInt(10, data.getGnum());
pstmt.setInt(11, 0);
pstmt.setInt(12, 0);
int re = pstmt.executeUpdate();
if(re > 0) cnt = true;
} catch (Exception e) {
System.out.println("저장실패:"+e);
}finally{
try {
pool.freeConnection(conn,pstmt);
} catch (Exception e) {}
}
return cnt;
}
}
// boardlist.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"
import="pack.*,java.util.*"
%>
<%
request.setCharacterEncoding("EUC-KR");
%>
<jsp:useBean id="mgr" class="pack.BoardListMgr"/>
<jsp:useBean id="data" class="pack.BoardData" />
<html><head><title>게시판</title>
<link rel ="stylesheet" type="text/css" href="board.css">
<script>
function check(){
if(frm.sword.value=="") {
alert("검색어를 입력하시오");
frm.sword.focus();
return;
}else
frm.submit();
}
function link(page) {
document.frm.page.value=page;
document.frm.submit();
}
</script>
</head>
<body topmargin=20 leftmargin=50>
<form name="frm" action="boardlist.jsp" method="post">
<input type="hidden" name="page" value="<%=spage %>">
<table align=center border=1 style="border-collapse=collapse" width="700" height="500">
<tr>
<td height="50" align="center">
<img src="images/bar.gif">
</td>
</tr>
<tr>
<td align="center" valign="top">
[<a href="index.jsp" onFocus=blur()>메인으로 </a>]
[<a href="boardlist.jsp?page=1" onFocus=blur()>최근목록 </a>]
[<a href="boardwrite.jsp" onFocus=blur()>새글 작성 </a>]
[<a href="#" onFocus=blur() onClick="window.open('admin.jsp','','width=300,height=150')">관리자용</a>]
<br><br>
<table cellspacing = 2 cellpadding = 5>
<tr align="center" bgcolor="#e2e2e2">
<td width="50">번호</td>
<td width="250">제목</td>
<td width="150">작성자</td>
<td width="100">날짜</td>
<td width="50">조회수</td>
</tr>
<%!
int pageblock, start, end, spage=1, pagesu;
String spages;
%>
<%
spages = request.getParameter("page");
String type = request.getParameter("stype");
String word = request.getParameter("sword");
try {
spage = Integer.parseInt(spages);
}catch(Exception ex) {}
pageblock = 3;
start = ((int)(Math.ceil((double)spage / (double)pageblock)) -1) * pageblock + 1;
end = start + (pageblock -1);
// 레코드 전체 갯수
mgr.totalList(word, type);
pagesu = mgr.getPageSu();
if(end > pagesu) end = pagesu;
Vector vc = mgr.getList(word, type, spage);
for(int i =0; i<vc.size(); i++) {
data = (BoardData)vc.get(i);
int nst= data.getNested();
String tab=""; //들여쓰기 공백
String rep=""; //들여쓰기 아이콘
for(int k = 0;k<nst; k++) {
tab += "&&";
rep += "<img src='images/re.gif' border='0'>";
}
%>
<tr align="center" bgcolor="#efefef">
<td><%=data.getNum() %></td>
<td align="left" style = "padding-left=15">
<%=tab %><a href="boardcontent.jsp?snum=<%=data.getNum() %>&page=<%=spage %>" onFocus=blur()><%=rep %><%=data.getTitle() %></a>
</td>
<td><%=data.getName() %></td>
<td><%=data.getDate() %></td>
<td><%=data.getReadcnt() %></td>
<% } %>
</table>
<br>
<table width="500">
<tr align="center">
<td>
<%
// 페이지 단위 출력 표시
// for(int i = 1; i<= pagesu; i++) {
// out.print("<a href=boardlist.jsp?page=" + i + ">[" + i + "]</a>");
// }
if( start != 1) {
int pre = start -1;
out.println("<a href=\'javascript:link(" + pre + ");\'>[이전 " + pre + "개]</a>");
}
%>
<span style="font-size=9pt;"></span>
<%
for(int i = start; i<= end; i++) {
if(i == spage)
out.print("<font color=red><b>[" +i+"]</b></font>&");
else out.print("<a href=boardlist.jsp?page=" + i + ">["+i+"]</a>&");
}
%>
&
<%
if(end<pagesu) {
int next = end + 1;
out.println("<a href=\'javascript:link(" + next + ");\'>[다음 " + (pagesu - end) + "개]</a>");
}
// 페이지 단위 출력 종료
%>
<br><br>
<select name=stype>
<option selected value="title">글제목
<option value="cont">글내용
<option value="name">작성자
</select>&
<input type="text" name="sword">
<input type="button" value=" 검 색 " onClick=check()>
<br>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="50" align="center">
<img src="images/bar.gif">
</td>
</tr> </table>
</form>
</body></html>
// boardsave.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" %>
<% request.setCharacterEncoding("EUC-KR"); %>
<jsp:useBean id="data" class="pack.BoardData" />
<jsp:setProperty property="*" name="data" />
<% // 키보드를 통해 5개 항목은 채움 : name, pass, mail, title, cont %>
<jsp:useBean id="mgr" class="pack.BoardMgr" />
<% // 키보드를 통해 5개 항목은 채움 : name, pass, mail, title, cont
data.setName(request.getParameter("name"));
data.setPass(request.getParameter("pass"));
data.setMail(request.getParameter("mail"));
data.setTitle(request.getParameter("title"));
data.setCont(request.getParameter("cont"));
data.setBip(request.getRemoteAddr()); // IP를 취한다.
data.setDate(); // 작성일
int max = mgr.currentGetnum() + 1;
data.setNum(max); // 레코드 번호
data.setGnum(max); // 해당 레코드 댓글 그룹 번호
out.println("<html><body>");
if(mgr.saveData(data)) {
out.println("저장성공");
}else {
out.println("저장실패");
}
response.sendRedirect("boardlist.jsp?page=1");
out.println("</html></body>");
%>
// boardcontent.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<jsp:useBean id="data" class="pack.BoardData" />
<jsp:useBean id="mgr" class="pack.BoardContMgr" />
<%!
int snum;
String spage;
%>
<html><head>
<title>게시물의 내용을 본다</title>
<link rel="stylesheet" type="text/css" href="board.css" />
</head><body topmargin=50 leftmargin=50>
<table border=1 style="border-collapse=collapse" width=530 height=400>
<tr>
<td height="50" align="center">
<img src="images/bar.gif">
</td>
</tr>
<tr>
<td align=center valign=top>
<%
snum=Integer.parseInt(request.getParameter("snum"));
spage = request.getParameter("page");
System.out.println(snum + ", "+ spage);
mgr.updateCnt(snum); // 조회수 증가
data = mgr.getCont(snum); // 해당 자료 읽기
String name = data.getName();
String pass = data.getPass();
String mail = data.getMail();
String title = data.getTitle();
String cont = data.getCont();
String bip = data.getBip();
String bdate = data.getDate();
int cnt = data.getReadcnt();
System.out.printf("%s, %s, %s, %s, %s, %s, %s, %d", name, pass, mail, title, cont, bip, bdate, cnt);
//암호는 관리자로 로그인 한 경우에만 화면에 표시
String apass = "****";
String admi = (String)session.getAttribute("adminOk");
if(admi != null) {
if(admi.equals("admin")) apass=pass;
}
%>
<br>
<table width=500>
<tr>
<td style="color=#cccccc"><b>비밀번호:<%=apass %></b></td>
<td align=right>
<a href="reply.jsp?num=<%=snum %>&page=<%=spage %>" onfocus=blur()><img src="images/reply.gif" border=0></a>&
<a href="boardwrite.jsp" onfocus=blur()><img src="images/write.gif" border=0></a>&
<a href="edit.jsp?num=<%=snum %>&page=<%=spage %>" onfocus=blur()><img src="images/edit.gif" border=0></a>&
<a href="delete.jsp?num=<%=snum %>&page=<%=spage %>" onfocus=blur()><img src="images/del.gif" border=0></a>&
</table>
<table width=500 height=250>
<tr bgcolor="#efefef" height=25>
<td width=300 style="padding-left=2">작성:<a href="mailto:<%=mail %>" onFocus=blur()><%=name %></a> IP<%=bip %></td>
<td width=100 align=center><%=bdate %></td>
<td width=100 align=center>조회:<%=cnt %></td>
</tr>
<tr bgcolor="#elelel" height=25>
<td colspan=3 style="padding-left=8">제목 :<b><%=title %></b></td></tr>
<tr bgcolor="#efefef">
<td colspan=3 style="padding-left=10">
<textarea rows="15" cols="100%" readonly><%=cont %></textarea>
</td>
</tr>
</table>
<br>
[<a href="index.jsp" onfocus=blur()>메인으로</a>]&
[<a href="boardlist.jsp?page="<%=spage %> onfocus=blur()>목록으로</a>]
</td>
</tr>
</table>
</body></html>
// BoardReplyMgr.java
package pack;
import java.sql.*;
import java.util.Vector;
public class BoardReplyMgr {
Connection conn;
PreparedStatement pstmt;
ResultSet rs;
DBConnectionMgr pool;
public BoardReplyMgr() {
try {
pool = DBConnectionMgr.getInstance();
conn = pool.getConnection();
} catch (Exception e) {
System.out.println("BoardReplyMgr DB연결 실패: "+e);
return;
}
}
public BoardData getData(int num) {
BoardData bda = new BoardData();
String sql = "select title, gnum, onum, nested from board where num = " + num;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()) {
bda.setTitle(rs.getString("title"));
bda.setGnum(rs.getInt("gnum"));
bda.setOnum(rs.getInt("onum"));
bda.setNested(rs.getInt("nested"));
}
} catch (Exception e) {
System.out.println("getData 오류 : " + e);
} finally {
pool.freeConnection(conn, pstmt, rs);
}
return bda;
}
public void updateOnum(int gn, int on) {
// 답변 글의 onum, 이미 db에 있는 onum보다 크거나 같은 값은 변경한다.
// 만약 1을 가지고 오면 1이상인, 만약 2를 가지고 오면 2이상인
// 동일 그룹 내의gnum은 1씩 증가 되도록 한다.
String sql = "update board set onum=onum + 1 where onum >= ? and gnum = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, on);
pstmt.setInt(2,gn);
pstmt.executeUpdate();
} catch (Exception e) {
System.out.println("updateOnum 오류:"+e);
}finally{
try {
if(pstmt != null) pstmt.close();
} catch (Exception e) {}
}
}
public int currentMaxNum() {
int max = 0;
String sql = "select max(num) from board";
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()) {
max = rs.getInt(1);
}
} catch (Exception e) {
System.out.println("currentMaxNum 오류:"+ e);
}finally{
try {
if(pstmt != null) pstmt.close();
if(rs != null) rs.close();
} catch (Exception e) {}
}
return max;
}
public void saveReplyData(BoardData data) {
String sql = "insert into board values(?,?,?,?,?,?,?,?,?,?,?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, data.getNum());
pstmt.setString(2, data.getName());
pstmt.setString(3, data.getPass());
pstmt.setString(4, data.getMail());
pstmt.setString(5, data.getTitle());
pstmt.setString(6, data.getCont());
pstmt.setString(7, data.getBip());
pstmt.setString(8, data.getDate());
pstmt.setInt(9, 0);
pstmt.setInt(10, data.getGnum());
pstmt.setInt(11, data.getOnum());
pstmt.setInt(12, data.getNested());
pstmt.executeUpdate();
} catch (Exception e) {
System.out.println("saveReplyData 오류: "+e);
}finally{
pool.freeConnection(conn,pstmt,rs);
}
}
}
// BoardListMgr.java
package pack;
import java.sql.*;
import java.util.*;
public class BoardListMgr {
Connection conn;
PreparedStatement pstmt;
ResultSet rs;
DBConnectionMgr pool;
int tot;
int plist = 10;
int pagesu;
public BoardListMgr(){
try {
pool = DBConnectionMgr.getInstance();
conn = pool.getConnection();
} catch (Exception e) {
System.out.println("BoardMgr DB연결 실패: "+e);
return;
}
}
public int currentGetnum() {
// 레코드 최대 번호 획득
int cnt = 0;
try{
String sql = "select max(num) as num from board";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
rs.next();
cnt = rs.getInt("num");
System.out.println("숫자:"+cnt);
} catch (Exception e) {
System.out.println("SQL오류 : "+e);
}finally{
try {
pool.freeConnection(conn,pstmt,rs);
} catch (Exception e) {}
}
System.out.println("ss" + cnt);
return cnt;
}
public boolean saveData(BoardData data) {
boolean cnt = false;
try {
String sql = "insert into board values(?,?,?,?,?,?,?,?,?,?,?,?)";
conn = pool.getConnection();
System.out.println(sql);
System.out.println(currentGetnum()+" "+data.getName()+" "+data.getPass()+" "+data.getMail()+ " "
+ data.getTitle()+ " " + data.getCont()+ " " +data.getBip() + " " + data.getDate() + " "
+ data.getReadcnt() + " " + data.getNum());
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, data.getNum());
pstmt.setString(2, data.getName());
pstmt.setString(3, data.getPass());
pstmt.setString(4, data.getMail());
pstmt.setString(5, data.getTitle());
pstmt.setString(6, data.getCont());
pstmt.setString(7, data.getBip());
pstmt.setString(8, data.getDate());
pstmt.setInt(9, 0);
pstmt.setInt(10, data.getGnum());
pstmt.setInt(11, 0);
pstmt.setInt(12, 0);
int re = pstmt.executeUpdate();
if(re > 0) cnt = true;
} catch (Exception e) {
System.out.println("저장실패:"+e);
}finally{
try {
pool.freeConnection(conn,pstmt);
} catch (Exception e) {}
}
return cnt;
}
public void totalList(String word, String type) {
String sql = "select count(*) from board ";
try{
if(word == null) {
pstmt = conn.prepareStatement(sql);
}else{
sql+= " where " + type + " like ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%" + word + "%");
}
rs = pstmt.executeQuery();
rs.next();
tot = rs.getInt(1);
System.out.println("전체 갯수 : " +tot);
}catch(Exception ex) {
System.out.println("sql오류!!: "+ ex);
}
}
public Vector getList(String word, String type, int page) {
Vector<BoardData> vc = new Vector<BoardData>();
BoardData data = null;
String sql = "select * from board";
try{
if(word != null) {
sql += " where " + type + " like ?";
sql += " order by gnum desc, onum asc";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, "%" + word + "%");
}else {
sql += " order by gnum desc, onum asc";
pstmt = conn.prepareStatement(sql);
}
rs = pstmt.executeQuery();
for(int i = 0; i< (page - 1 ) * plist; i++) {
try {
rs.next();
} catch (Exception e) {}
}int k = 0;
while(rs.next() && k < plist) {
int n = rs.getInt("num");
String name = rs.getString("name");
String title = rs.getString("title");
String date = rs.getString("bdate");
int rcnt = rs.getInt("readcnt");
int nested = rs.getInt("nested");
data = new BoardData();
data.setNum(n);
data.setName(name);
data.setTitle(title);
data.setDate(date);
data.setReadcnt(rcnt);
data.setNested(nested);
vc.add(data);
k++;
}
}catch(Exception e) {
System.out.println("자료 가져 오기 오류" +e);
}finally{
pool.freeConnection(conn, pstmt, rs);
}
return vc;
}
public int getPageSu() {
pagesu = tot / plist;
if(tot % plist > 0) pagesu++;
return pagesu;
}
}
// boardwrite.jsp
<%@ page contentType="text/html; charset=EUC-KR" %>
<html><head><title>게시판</title>
<link rel="stylesheet" type="text/css" href="board.css">
<script>
function check(){
if(frm.name.value==""){
alert("이름을 입력하세요");
frm.name.focus();
}else if(frm.pass.value ==""){
alert("비밀번호를 입력하세요");
frm.pass.focus();
}else if(frm.mail.value ==""){
alert("이메일을 입력하세요");
frm.mail.focus();
}else if(frm.title.value ==""){
alert("제목을 입력하세요");
frm.title.focus();
}else if(frm.cont.value ==""){
alert("내용을 입력하세요");ㄱ
frm.cont.focus();
}else
frm.submit();
}
</script>
</head>
<body topmargin=50 leftmargin=50 onLoad=frm.name.focus()>
<center><h2>*** 글쓰기 ***</h2>
<form name=frm method=post action="boardsave.jsp">
<table border=1 style="border-collapse=collapse" width=530>
<tr><td colspan=2 height=50 align=center><img src="images/bar.gif"></td></tr>
<tr>
<td align=center width=100>이 름</td>
<td width=430><input name=name size=15></td>
</tr>
<tr>
<td align=center >암 호</td>
<td width=430><input type=password name=pass size=15></td>
</tr>
<tr>
<td align=center>메 일</td>
<td><input name=mail size=25></td>
</tr>
<tr>
<td align=center>제 목</td>
<td><input name=title size=50></td>
</tr>
<tr>
<td align=center>내 용</td>
<td><textarea name=cont cols=50 rows=12></textarea></td>
</tr>
<tr>
<td colspan=2 align=center height=30>
<input type=button value="메 인" onClick="location.href('index.jsp')"> &
<input type=button value="작 성" onClick="check()" > &
<input type=button value="목 록" onClick="location.href('boardlist.jsp')">
</td>
</tr>
<tr><td colspan=2 height=50 align=center><img src="images/bar.gif"></td></tr>
</table></form></center></body></html>
// boardContMgr.java
package pack;
import java.sql.*;
import java.util.Vector;
public class BoardContMgr {
Connection conn;
PreparedStatement pstmt;
ResultSet rs;
DBConnectionMgr pool;
public BoardContMgr() {
try {
pool = DBConnectionMgr.getInstance();
conn = pool.getConnection();
} catch (Exception e) {
System.out.println("BoardContMgr DB연결 실패: "+e);
return;
}
}
public void updateCnt(int num){
String sql="update board set readcnt=readcnt+1 where num=?";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, num);
pstmt.executeUpdate();
} catch (Exception e) {
System.out.println("updateCnt 에러:"+e);
}finally{
try {
if(pstmt != null) pstmt.close();
} catch (Exception e) {
}
}
}
public BoardData getCont(int num) {
BoardData bda = null;
// 이름, 암호, 메일, 제목, 내용, 아이피, 작성일, 조회수
Vector<BoardData> vc = new Vector<BoardData>();
String sql = "select * from board where num = "+num;
System.out.println(sql);
try{
bda = new BoardData();
pstmt=conn.prepareStatement(sql);
rs = pstmt.executeQuery();
rs.next();
int k = 0;
int n = rs.getInt("num");
String name = rs.getString("name");
String pass = rs.getString("pass");
String mail = rs.getString("mail");
String title = rs.getString("title");
String cont = rs.getString("cont");
String bip = rs.getString("bip");
String date = rs.getString("bdate");
int rcnt = rs.getInt("readcnt");
System.out.println(name+pass+mail+title+cont+bip+date);
bda.setNum(n);
bda.setName(name);
bda.setPass(pass);
bda.setMail(mail);
bda.setTitle(title);
bda.setCont(cont);
bda.setBip(bip);
bda.setDate(date);
bda.setReadcnt(rcnt);
vc.add(bda);
}catch(Exception e) {
System.out.println("자료 가져 오기 오류" +e);
}finally{
pool.freeConnection(conn, pstmt, rs);
}
return bda;
}
}
// replysave.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
request.setCharacterEncoding("EUC-KR");
%>
<jsp:useBean id="data" class="pack.BoardData" />
<jsp:setProperty name="data" property="*" />
<jsp:useBean id="mgr" class="pack.BoardReplyMgr" />
<%
String spage = request.getParameter("page");
// 원글의 onum과 nested를 1씩 증가
int gnum = data.getGnum();
int onum = data.getOnum() +1;
int nst=data.getNested() +1;
//gnum 그룹의 onum 값 증가된 자료를 DB에 저장
mgr.updateOnum(gnum, onum);
// 댓글 저장
data.setOnum(onum);
data.setNested(nst);
data.setGnum(gnum);
data.setBip(request.getRemoteAddr());
data.setNum(mgr.currentMaxNum() +1);
data.setDate();
mgr.saveReplyData(data);
response.sendRedirect("boardlist.jsp?page=" + spage);
%>
// reply.jsp
<%@ page contentType="text/html; charset=EUC-KR" %>
<jsp:useBean id="data" class="pack.BoardData" />
<jsp:useBean id="mgr" class="pack.BoardReplyMgr" />
<%!
int snum;
String spage;
%>
<html><head><title>댓글</title>
<link rel="stylesheet" type="text/css" href="board.css">
<script>
function check(){
if(frm.name.value==""){
alert("이름을 입력하세요");
frm.name.focus();
}else if(frm.pass.value ==""){
alert("비밀번호를 입력하세요");
frm.pass.focus();
}else if(frm.mail.value ==""){
alert("이메일을 입력하세요");
frm.mail.focus();
}else if(frm.cont.value ==""){
alert("내용을 입력하세요");
frm.cont.focus();
}else
frm.submit();
}
</script>
</head>
<body topmargin=50 leftmargin=50 onLoad=frm.name.focus()>
<center><h2>*** 답글 쓰기 ***</h2>
<%
request.setCharacterEncoding("EUC-KR");
snum = Integer.parseInt(request.getParameter("num"));
spage = request.getParameter("page");
//원글의 gnum, onum, nested, title 가져오기
data = mgr.getData(snum);
int gnum = data.getGnum();
int onum = data.getOnum();
int nested = data.getNested();
String title = data.getTitle();
%>
<form name=frm method=post action="editsave.jsp">
<table border=4 bordercolor="#AAAAAA" style="border-collapse=collapse" width=530 cellspacing="1" cellpadding="0">
<tr><td colspan=2 height=50 align=center><img src="images/bar.gif"></td></tr>
<tr bgcolor="#BBEEBB">
<input type="hidden" name="num" value="<%=snum %>">
<input type="hidden" name="page" value="<%=spage %>">
<input type="hidden" name="gnum" value="<%=gnum %>">
<input type="hidden" name="onum" value="<%=onum %>">
<input type="hidden" name="nested" value="<%=nested %>">
<td align=center width=100>이 름</td>
<td width=430><input name=name size=15></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center >암 호</td>
<td width=430><input type=password name=pass size=15></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center>메 일</td>
<td><input name=mail size=25></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center>제 목</td>
<td><input name=title size=50 value="[RE] <%=title %>"></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center>내 용</td>
<td><textarea name=cont cols=50 rows=12></textarea></td>
</tr>
<tr bgcolor="#BBEEBB">
<td colspan=2 align=center height=30>
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="메 인" onClick="location.href('index.jsp')"> &
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="작 성" onClick="check()" > &
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="목 록" onClick="location.href('boardlist.jsp?page=<%=spage %>')">
</td>
</tr>
<tr><td colspan=2 height=50 align=center><img src="images/bar.gif"></td></tr>
</table></form>
</center></body></html>
// edit.jsp
<%@ page contentType="text/html; charset=EUC-KR" %>
<jsp:useBean id="data" class="pack.BoardData" />
<jsp:useBean id="mgr" class="pack.editMgr" />
<%!
int snum;
String spage;
%>
<html><head><title>글고침</title>
<link rel="stylesheet" type="text/css" href="board.css">
<script>
function check(){
if(frm.name.value==""){
alert("이름을 입력하세요");
frm.name.focus();
}else if(frm.pass.value ==""){
alert("비밀번호를 입력하세요");
frm.pass.focus();
}else if(frm.mail.value ==""){
alert("이메일을 입력하세요");
frm.mail.focus();
}else if(frm.cont.value ==""){
alert("내용을 입력하세요");
frm.cont.focus();
}else
frm.submit();
}
</script>
</head>
<body topmargin=50 leftmargin=50 onLoad=frm.name.focus()>
<center><h2>*** 글 수정 ***</h2>
<%
request.setCharacterEncoding("EUC-KR");
snum = Integer.parseInt(request.getParameter("num"));
spage = request.getParameter("page");
//원글의 gnum, onum, nested, title 가져오기
data = mgr.getData(snum);
int gnum = data.getGnum();
int onum = data.getOnum();
int nested = data.getNested();
String title = data.getTitle();
String cont = data.getCont();
String mail = data.getMail();
String name = data.getName();
String pass = data.getPass();
%>
<form name=frm method=post action="editsave.jsp">
<table border=4 bordercolor="#AAAAAA" style="border-collapse=collapse" width=530 cellspacing="1" cellpadding="0">
<tr><td colspan=2 height=50 align=center><img src="images/bar.gif"></td></tr>
<tr bgcolor="#BBEEBB">
<input type="hidden" name="num" value="<%=snum %>">
<input type="hidden" name="page" value="<%=spage %>">
<input type="hidden" name="gnum" value="<%=gnum %>">
<input type="hidden" name="onum" value="<%=onum %>">
<input type="hidden" name="nested" value="<%=nested %>">
<td align=center width=100>이 름</td>
<td width=430><input name=name size=15 value="<%=name %>"></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center >암 호</td>
<td width=430><input type=password name=pass size=15 value="<%=pass %>"></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center>메 일</td>
<td><input name=mail size=25 value="<%=mail %>"></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center>제 목</td>
<td><input name=title size=50 value="<%=title %>"></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center>내 용</td>
<td><textarea name=cont cols=50 rows=12><%= cont %></textarea></td>
</tr>
<tr bgcolor="#BBEEBB">
<td colspan=2 align=center height=30>
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="메 인" onClick="location.href('index.jsp')"> &
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="작 성" onClick="check()" > &
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="목 록" onClick="location.href('boardlist.jsp?page=<%=spage %>')">
</td>
</tr>
<tr><td colspan=2 height=50 align=center><img src="images/bar.gif"></td></tr>
</table></form>
</center></body></html>
// editMgr.java
package pack;
import java.sql.*;
public class editMgr {
Connection conn;
PreparedStatement pstmt;
ResultSet rs;
DBConnectionMgr pool;
public editMgr() {
try {
pool = DBConnectionMgr.getInstance();
conn = pool.getConnection();
} catch (Exception e) {
System.out.println("editMgr DB연결 실패: "+e);
return;
}
}
public BoardData getData(int num) {
BoardData bda = new BoardData();
String sql = "select * from board where num = " + num;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
rs.next();
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
System.out.println(rs.getString(4));
System.out.println(rs.getString(5));
System.out.println(rs.getString(6));
System.out.println(rs.getString(7));
System.out.println(rs.getString(8));
bda.setTitle(rs.getString("title"));
bda.setGnum(rs.getInt("gnum"));
bda.setOnum(rs.getInt("onum"));
bda.setNested(rs.getInt("nested"));
bda.setCont(rs.getString("cont"));
bda.setMail(rs.getString("mail"));
bda.setPass(rs.getString("pass"));
bda.setName(rs.getString("name"));
} catch (Exception e) {
System.out.println("getData 오류 : " + e);
} finally {
pool.freeConnection(conn, pstmt, rs);
}
return bda;
}
}
// editsave.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" %>
<% request.setCharacterEncoding("EUC-KR"); %>
<jsp:useBean id="data" class="pack.BoardData" />
<jsp:setProperty property="*" name="data" />
<% // 키보드를 통해 5개 항목은 채움 : name, pass, mail, title, cont %>
<jsp:useBean id="mgr" class="pack.BoardMgr" />
<% // 키보드를 통해 5개 항목은 채움 : name, pass, mail, title, cont
data.setName(request.getParameter("name"));
data.setPass(request.getParameter("pass"));
data.setMail(request.getParameter("mail"));
data.setTitle(request.getParameter("title"));
data.setCont(request.getParameter("cont"));
data.setBip(request.getRemoteAddr()); // IP를 취한다.
out.println("<html><body>");
if(mgr.updateData(data)) {
out.println("저장성공");
}else {
out.println("저장실패");
}
response.sendRedirect("boardcontent.jsp?snum="+request.getParameter("num")+"&page="+request.getParameter("spage"));
out.println("</html></body>");
%>