note

방명록 MODEL1 방식 ORACLE 본문

JSP/기본

방명록 MODEL1 방식 ORACLE

투한 2012. 2. 13. 09:22



DB연동 패턴 (Dao.java)
Date Access Object(DAO) 


1.목록 (ArrayList + 자바빈)
2.상세정보 (자바빈, ex select * from guestbook where num =?)
3.insert
4.update (1.인증 -> 2.update)
5.delete (1.인증 -> 2.delete) 



DB생성

create table guestbook(

num number primary key,

register date not null,

name varchar2(20) not null,

email varchar2(40),

passwd varchar2(10) not null,

content varchar2(4000) not null

);

create sequence guestbook_seq;










글 등록

write.jsp

 







write.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.guestbook.dao.GuestbookDao" %>
<%@ page import="java.sql.Timestamp" %>
<%
	request.setCharacterEncoding("UTF-8");
%>
<jsp:useBean id="book" class="com.guestbook.domain.Guestbook">
	<jsp:setProperty name="book" property="*" />
</jsp:useBean>
<%
	//등록일
	book.setRegister(new Timestamp(System.currentTimeMillis()));
	GuestbookDao manager  = GuestbookDao.getInstance();
	manager.insert(book);
%>
<script type="text/javascript">
	alert("방명록에 글을 등록하였습니다.");
	location.href="list.jsp";
</script>


writeForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>글쓰기</title>
<script type="text/javaScript">
   <!-- 
    function checkIt() {
        var user = document.userinput;
               
        if(!user.name.value) {
            alert("사용자 이름을 입력하세요");
            user.name.focus();
            return false;
        }
        if(!user.passwd.value ) {
            alert("비밀번호를 입력하세요");
            user.passwd.focus();
            return false;
        }
        if(!user.content.value ) {
            alert("내용을 입력하세요");
            user.content.focus();
            return false;
        }
       return true;
    }
//-->
</script>
</head>
<body>
<center>
<form name="userinput" action="write.jsp" method="post" onsubmit="return checkIt()">
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
    <td>이름</td>
    <td><input type="text" name="name" size="10"></td>
</tr>
<tr>
    <td>암호</td>
    <td><input type="password" name="passwd" size="10"></td>
</tr>
<tr>
    <td>이메일</td>
    <td><input type="text" name="email" size="30"></td>
</tr>
<tr>
    <td>내용</td>
    <td><textarea name="content" rows="5" cols="50"></textarea></td>
</tr>
<tr>
    <td colspan="2"><input type="submit" value="글남기기"></td>
</tr>
</table>
</form>
</center>
</body>
</html>







수정

writeForm.jsp



updateForm.jsp



list.jsp















updateForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.guestbook.dao.GuestbookDao" %>
<%@ page import="com.guestbook.domain.Guestbook" %>
<%
	int num = Integer.parseInt(request.getParameter("num"));
	GuestbookDao manager = GuestbookDao.getInstance();
	Guestbook book = manager.getGuestBook(num);
	if(book.getEmail() == null){
		book.setEmail("");
	}
%>
<!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>
<script type="text/javascript">
<!--
function checkIt(){
	var user = document.userinput;
	
	if(!user.name.value){
		alert("사용자 이름을 입력하세요");
		user.name.focus();
		return false;
	}
	if(!user.passwd.value){
		alert("비밀번호를 입력하세요");
		user.passwd.focus();
		return false;
	}
	if(!user.content.value){
		alert("내용을 입력하세요");
		user.content.focus();
		return false;
	}
}
//-->
</script>
</head>
<body>
<form action="update.jsp" method="post" name="userinput" onSubmit="return checkIt()">
<input type="hidden" name="num" value="<%=num %>">
<table width="100%" border="1" cellpadding="0" cellspacing="0">
	<tr>
		<td>암호</td>
		<td><input type="password" name="passwd" size="10"><br>
		글을 쓸때 입력한 암호와 동일해야 글이 수정됩니다.</td>
	</tr>
	<tr>
		<td>이름</td>
		<td><input type="text" name="name" size="10" value="<%=book.getName() %>"></td>
	</tr>
	<tr>
		<td>이메일</td>
		<td><input type="text" name="email" size="30" value="<%=book.getEmail() %>"></td>
	</tr>
	<tr>
		<td>내용</td>
		<td><textarea name="content" rows="5" cols="50"><%=book.getContent() %></textarea></td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" value="글 수정하기"></td>
	</tr>
	
</table>
</form>
</body>
</html>


update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.guestbook.dao.GuestbookDao" %>
<%@ page import="com.guestbook.domain.Guestbook" %>
<%@ page import="java.sql.Timestamp" %>
<%
	request.setCharacterEncoding("UTF-8");
%>
<jsp:useBean id="book" class="com.guestbook.domain.Guestbook">
	<jsp:setProperty name="book" property="*" />
</jsp:useBean>
<%
	GuestbookDao manager = GuestbookDao.getInstance();
	int check = manager.userCheck(book.getNum(),book.getPasswd());
	
	if(check == 1){
		manager.update(book);
%>
<script type="text/javascript">
alert("글을 수정하였습니다.");
location.href="list.jsp";
</script>
<%
	}else{
%>
<script type="text/javascript">
alert("암호가 다릅니다.");
history.go(-1);
</script>
<%} %>





 


삭제





delete.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "com.guestbook.dao.GuestbookDao" %>
<%@ page import = "com.guestbook.domain.Guestbook" %>
<%
	request.setCharacterEncoding("UTF-8");
	int num = Integer.parseInt(request.getParameter("num"));
	String passwd = request.getParameter("passwd");
	
	GuestbookDao manager = GuestbookDao.getInstance();
	int check = manager.userCheck(num,passwd);
	if(check  ==1){
		manager.delete(num);
%>
<script type="text/javascript">
alert("글을 삭제하였습니다.");
location.href="list.jsp";
</script>
<%}else{ %>
<script type="text/javascript">
alert("암호가 다릅니다");
history.go(-1);
</script>
<% } %>


deleteForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "com.guestbook.dao.GuestbookDao" %>
<%@ page import = "com.guestbook.domain.Guestbook" %>
<%
    int num = Integer.parseInt(request.getParameter("num"));
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>글삭제</title></head>
<body>
<form action="delete.jsp" method="post">
<input type="hidden" name="num" value="<%= num %>">
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
    <td>암호</td>
    <td><input type="password" name="passwd" size="10"><br>
    글을 쓸때 입력한 암호와 동일해야 글이 삭제됩니다.</td>
</tr>
<tr>
    <td colspan="2"><input type="submit" value="글삭제하기"></td>
</tr>
</table>
</form>
</body>
</html>



목록


list.jsp작성을 위한 













list.jsp




list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import ="com.guestbook.dao.GuestbookDao" %>
<%@ page import ="com.guestbook.domain.Guestbook" %>
<%@ page import ="java.util.List" %>
<%@ page import ="java.text.SimpleDateFormat" %>

<%
	int pageSize=10;
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
%>
<%
	//페이지 번호 처리
	String pageNum = request.getParameter("pageNum");
	if(pageNum == null){
		pageNum = "1";
	}
	//페이지 계산
	int currentPage = Integer.parseInt(pageNum);
	int startRow = (currentPage -1) * pageSize + 1;
	int endRow = currentPage * pageSize;
	int count= 0;
	
	List<Guestbook> bookList = null;
	GuestbookDao manager = GuestbookDao.getInstance();
	count = manager.getCount();
	
	if(count>0){
		bookList = manager.getList(startRow,endRow);
	}
%>
<!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>
<body>
<table width="100">
<tr>
	<td><a href="writeForm.jsp">글쓰기</a></td>
</tr>
</table>
<%
	if(count ==0){
%>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
	<tr>
		<td bgcolor="#e9e9e9">방명록에 저장된 글이 없습니다.</td>
	</tr>
</table>
<%
	}else{
%>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<!-- 목록 출력 시작 -->
<%
	for(int i = 0; i<bookList.size(); i++){
		Guestbook book = bookList.get(i);
%>
<tr>
	<td bgcolor="#e9e9e9">
	<b><%= book.getName() %>(<%= (book.getEmail() == null)?"" : book.getEmail() %>)</b>
	
	- <font size="2">
	<%= formatter.format(book.getRegister()) %>
	<a href="updateForm.jsp?num=<%=book.getNum() %>">[수정]</a>
	<a href="deleteForm.jsp?num=<%=book.getNum() %>">[삭제]</a>
	</font>
	</td>
</tr>
<tr>
	<td><%= book.getContent() %></td>
</tr>
<%}%>
<!-- 목록 출력 끝 -->
</table>
<%} %>
<!-- 페이징 처리 시작 -->
<div align="center">
<%
	if(count>0){
		int pageBlock=10;
		
		int pageCount = (count -1) / pageSize + 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 -1 %>">[이전]</a>
<%}
	for(int i =startPage ; i<=endPage; i++){
		if(i == currentPage){
%>
<font size="2" color="#666">[<%=i %>]</font>
<%}else{ %>
<a href="list.jsp?pageNum=<%=i %>">[<%=i %>]</a>
<%
	}
}
	if(endPage<pageCount){ %>
	<a href="list.jps?pageNum=<%= startPage+pageBlock %>">[다음]</a>
<%
	}
}
%>
</div>
<!-- 페이징 처리 끝 -->
</body>
</html>