- model 1기법
- jsp에서 사용되는 자바 소스 코드를 모듈화
- beans 역할에 클래스에 이름 구분
- dto(data transfer object) : to
- :데이터 클레스
- dao(data access object)
- :테이터 처리용 클래스
insert update delete select - :페이지 : 메서드
1. 프로젝트 준비
기본적인 셋팅
2.model1
model1으로 게시판 만들기
DTO
package model1;
// 데이터용 클래스 만듦
public class BoardTO {
private String seq;
private String subject;
private String writer;
private String mail;
private String password;
private String content;
private String hit;
private String wip;
private String wdate;
private int wgap;
public String getSeq() {
return seq;
}
public String getSubject() {
return subject;
}
public String getWriter() {
return writer;
}
public String getMail() {
return mail;
}
public String getPassword() {
return password;
}
public String getContent() {
return content;
}
public String getHit() {
return hit;
}
public String getWip() {
return wip;
}
public String getWdate() {
return wdate;
}
public int getWgap() {
return wgap;
}
public void setSeq(String seq) {
this.seq = seq;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setWriter(String writer) {
this.writer = writer;
}
public void setMail(String mail) {
this.mail = mail;
}
public void setPassword(String password) {
this.password = password;
}
public void setContent(String content) {
this.content = content;
}
public void setHit(String hit) {
this.hit = hit;
}
public void setWip(String wip) {
this.wip = wip;
}
public void setWdate(String wdate) {
this.wdate = wdate;
}
public void setWgap(int wgap) {
this.wgap = wgap;
}
}
DAO
package model1;
//데이터 처리용 클래스 만듦
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class BoardDAO {
private DataSource dataSource;
//생성자에서 tomcat pooling 설정
public BoardDAO() {
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
this.dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
} catch (NamingException e) {
System.out.println("에러 : "+ e.getMessage());
}
}
/*각 페이지에 필요한 데이터를 리턴하는 메서드를 정의*/
public void boardWrite(){
//필요없음
}
//
public int boardWriteOk(BoardTO to){
Connection conn = null;
PreparedStatement pstmt = null;
int flag = 1;
try{
conn = dataSource.getConnection();
//DB에 데이터쓰기
String sql = "insert into board1 values (board_seq.nextval, ?, ?, ?, ?, ?, 0, ?, sysdate)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, to.getSubject());
pstmt.setString(2, to.getWriter());
pstmt.setString(3, to.getMail());
pstmt.setString(4, to.getPassword());
pstmt.setString(5, to.getContent());
pstmt.setString(6, to.getWip());
if(pstmt.executeUpdate() ==1){
flag =0;
}
}catch(SQLException e){
System.out.println("에러 : " + e.getMessage());
}finally{
if(pstmt !=null)try{pstmt.close();}catch(SQLException e){};
if(conn !=null)try{conn.close();}catch(SQLException e){};
}
return flag;
}
//
public ArrayList<BoardTO> boardList(){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
ArrayList<BoardTO> result = new ArrayList<>();
try{
conn = dataSource.getConnection();
//리스트창 데이터 출력
String sql = "select seq, subject, writer, to_char(wdate,'YY.MM.DD') wdate, hit, trunc(sysdate-wdate) wgap from board1 order by seq desc";
pstmt = conn.prepareStatement(sql);
rs= pstmt.executeQuery();
while(rs.next()){
BoardTO to = new BoardTO();
to.setSeq(rs.getString("seq"));
to.setSubject(rs.getString("subject"));
to.setWriter(rs.getString("writer"));
to.setWdate(rs.getString("wdate"));
to.setHit(rs.getString("hit"));
to.setWgap(rs.getInt("wgap"));
result.add(to);
}
}catch(SQLException e){
System.out.println("에러 : " + e.getMessage());
}finally{
if(rs!=null)try {rs.close();} catch (SQLException e) {}
if(pstmt!=null)try {pstmt.close();} catch (SQLException e) {}
if(conn!=null)try {conn.close();} catch (SQLException e) {}
}
return result;
}
//
public BoardTO boardView(BoardTO to){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
conn = this.dataSource.getConnection();
//조회수 증가
String sql = "update board1 set hit=hit+1 where seq=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, to.getSeq());
pstmt.executeUpdate();
//view창 데이터 출력
sql = "select subject, writer, mail, wip, wdate, hit, content from board1 where seq=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, to.getSeq());
rs = pstmt.executeQuery();
if(rs.next()){
to.setSubject(rs.getString("subject"));
to.setWriter(rs.getString("writer"));
to.setMail(rs.getString("mail")==null?"":rs.getString("mail"));
to.setWip(rs.getString("wip"));
to.setWdate(rs.getString("wdate"));
to.setHit(rs.getString("hit"));
to.setContent(rs.getString("content")==null?"":rs.getString("content").replaceAll("\n", "<br>")); //엔터키 처리
}
}catch(SQLException e){
System.out.println("에러 : " + e.getMessage());
}finally{
if(rs!=null)try {rs.close();} catch (SQLException e) {}
if(pstmt!=null)try {pstmt.close();} catch (SQLException e) {}
if(conn!=null)try {conn.close();} catch (SQLException e) {}
}
return to;
}
//
public BoardTO boardModify(BoardTO to){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
conn = this.dataSource.getConnection();
//modify창 데이터 출력
String sql = "select subject, writer, mail,content from board1 where seq=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, to.getSeq());
rs = pstmt.executeQuery();
if(rs.next()){
to.setSubject(rs.getString("subject"));
to.setWriter(rs.getString("writer"));
to.setMail(rs.getString("mail")==null?"":rs.getString("mail"));
to.setContent(rs.getString("content")==null?"":rs.getString("content").replaceAll("\n", "<br>"));
}
}catch(SQLException e){
System.out.println("에러 : " + e.getMessage());
}finally{
if(rs!=null)try {rs.close();} catch (SQLException e) {}
if(pstmt!=null)try {pstmt.close();} catch (SQLException e) {}
if(conn!=null)try {conn.close();} catch (SQLException e) {}
}
return to;
}
//
public int boardModifyOk(BoardTO to){
Connection conn = null;
PreparedStatement pstmt = null;
int flag = 2;
try{
conn = this.dataSource.getConnection();
//데이터 수정
String sql = "update board1 set subject=?, mail=?, content=? where seq=? and password=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, to.getSubject());
pstmt.setString(2, to.getMail());
pstmt.setString(3, to.getContent());
pstmt.setString(4, to.getSeq());
pstmt.setString(5, to.getPassword());
int result = pstmt.executeUpdate();
if(result ==0){
flag =1;
}else if(result ==1){
flag =0;
}
}catch(SQLException e){
System.out.println("에러 : " + e.getMessage());
}finally{
if(pstmt !=null)try{pstmt.close();}catch(SQLException e){};
if(conn !=null)try{conn.close();}catch(SQLException e){};
}
return flag;
}
//
public BoardTO boardDelete(BoardTO to){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
conn = this.dataSource.getConnection();
//delete창 출력
String sql = "select subject, writer, mail from board1 where seq=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, to.getSeq());
rs = pstmt.executeQuery();
if(rs.next()){
to.setSubject(rs.getString("subject"));
to.setWriter(rs.getString("writer"));
to.setMail(rs.getString("mail")==null?"":rs.getString("mail"));
}
}catch(SQLException e){
System.out.println("에러 : " + e.getMessage());
}finally{
if(rs!=null)try {rs.close();} catch (SQLException e) {}
if(pstmt!=null)try {pstmt.close();} catch (SQLException e) {}
if(conn!=null)try {conn.close();} catch (SQLException e) {}
}
return to;
}
//
public int boardDeleteOk(BoardTO to){
Connection conn = null;
PreparedStatement pstmt = null;
int flag = 2;
try{
conn = this.dataSource.getConnection();
//delete 처리
String sql = "delete from board1 where seq=? and password=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, to.getSeq());
pstmt.setString(2, to.getPassword());
int result = pstmt.executeUpdate();
if(result ==0){
flag =1;
}else if(result ==1){
flag =0;
}
}catch(SQLException e){
System.out.println("에러 : " + e.getMessage());
}finally{
if(pstmt!=null)try {pstmt.close();} catch (SQLException e) {}
if(conn!=null)try {conn.close();} catch (SQLException e) {}
}
return flag;
}
}
3. board_write1 :변화 없음
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='../images/common.css'>
<script type="text/javascript">
function ChkForm() {
//필수 입력값 검사
if(document.wfrm.subject.value.trim() ==""){
alert('제목을 입력하셔야 합니다.');
return false;
}
if(document.wfrm.writer.value.trim() ==""){
alert('이름을 입력하셔야 합니다.');
return false;
}
if(document.wfrm.password.value.trim() ==""){
alert('암호를 입력하셔야 합니다.');
return false;
}
}
</script>
</head>
<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
<td bgcolor='#ffffff' style='padding:20'>
<form action='board_write1_ok.jsp' method='post' name='wfrm' onSubmit='return ChkForm(this)'>
<table width='100%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>제목</font> :
</td>
<td>
<input type='text' name='subject' size='80' class='form'>
</td>
</tr>
<tr>
<td colspan='2' class='imgline'></td>
</tr>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>작성자</font> :
</td>
<td>
이름 <input type='text' name='writer' size='10' maxlength='10' class='form'> /
메일 <input type='text' name='mail' size='40' maxlength='70' class='form'> /
암호 <input type='password' name='password' size='10' maxlength='10' class='form'>
</td>
</tr>
<tr>
<td colspan='2' class='imgline'></td>
</tr>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>내용</font> :
</td>
<td>
<textarea name='content' style='width:620;height:300' class='form'></textarea>
</td>
</tr>
<tr>
<td colspan='2' class='gline'></td>
</tr>
</table>
<table width='100%' cellpadding='0' cellspacing='0' border='0'>
<tr>
<td width='500' height='30'> </td>
<td align='right'>
<input type='image' src='../images/btn_wri.gif' border='0'>
<a href='board_list1.jsp'><img src='../images/btn_list.gif' border='0'></a>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>
</body>
</html>
4. board_write1_ok
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="model1.BoardTO"%>
<%@page import="model1.BoardDAO"%>
<%
request.setCharacterEncoding("utf-8");
//데이터 클래스를 생성, 클라이언트 정보 넣음
BoardTO to = new BoardTO();
to.setSubject(request.getParameter("subject"));
to.setWriter(request.getParameter("writer"));
to.setMail(request.getParameter("mail"));
to.setPassword(request.getParameter("password"));
to.setContent(request.getParameter("content"));
to.setWip(request.getRemoteAddr());
//DAO클래스에서 write_ok에서 필요한 DB데이터를 처리해서 리턴
BoardDAO dao = new BoardDAO();
int flag = dao.boardWriteOk(to);
//성공실패 메시지
out.println("<script type='text/javascript'>");
if(flag ==0){
out.println("alert('글쓰기에 성공했습니다')");
out.println("location.href='board_list1.jsp';");
}else{
out.println("alert('글쓰기에 실패했습니다')");
out.println("history.back()");
}
out.println("</script>");
%>
5. board_list1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="model1.BoardTO"%>
<%@page import="model1.BoardDAO"%>
<%@page import="java.util.ArrayList"%>
<%
request.setCharacterEncoding("utf-8");
//DAO클래스에서 list에서 필요한 DB데이터를 처리해서 ArrayList로 리턴
BoardDAO dao = new BoardDAO();
ArrayList<BoardTO> lists = dao.boardList();
//ArrayList 데이터 result에 담음
StringBuffer result = new StringBuffer();
for(BoardTO to: lists){
String seq = to.getSeq();
String subject = to.getSubject();
String writer = to.getWriter();
String wdate = to.getWdate();
String hit = to.getHit();
int wgap = to.getWgap();
result.append("<table width='100%' border='0' cellpadding='0' cellspacing='0'>");
result.append("<tr>");
result.append(" <td height='1'></td>");
result.append("</tr>");
result.append("<tr>");
result.append(" <td>");
result.append(" <table width='100%' border='0' cellpadding='0' cellspacing='0'>");
result.append(" <tr height='25' onMouseOver=\"this.className='evencell'\" onMouseOut=\"this.className=''\">");
result.append(" <td width='40' align='center'>"+seq+"</td>");
result.append(" <td>");
result.append(" <span style='width:370' class='elltxt'>");
result.append(" <a href='board_view1.jsp?seq="+seq+"'>"+subject+"</a>");
//new이미지 24시간
if(wgap == 0) {
result.append(" <img src='../images/ico_n.gif' width='8' height='8' border='0' hspace='3'>");
}
result.append(" </span>");
result.append(" </td>");
result.append(" <td width='100' align='center'>"+writer+"</td>");
result.append(" <td width='80' align='center'>"+wdate+"</td>");
result.append(" <td width='50' align='center'>"+hit+"</td>");
result.append(" </tr>");
result.append(" </table>");
result.append(" </td>");
result.append("</tr>");
result.append("<tr>");
result.append(" <td height='1'></td>");
result.append("</tr>");
result.append("<tr>");
result.append(" <td align='center' class='imgline'></td>");
result.append("</tr>");
result.append("</table>");
}
%>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='../images/common.css'>
</head>
<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
<td bgcolor='#ffffff' style='padding:20'>
<table width='100%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<td align='center' class='gline'></td>
</tr>
<tr>
<td align='center'>
<table width='100%' border='0' cellpadding='0' cellspacing='0' class='titlecell'>
<tr height='25' align='center'>
<td width='40'>No</td>
<td>제목</td>
<td width='100'>이름</td>
<td width='80'>등록일</td>
<td width='50'>조회수</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align='center' class='gline'></td>
</tr>
<tr>
<td height='3'></td>
</tr>
</table>
<!-- 리스트 시작 -->
<%= result %>
<!-- 리스트 끝 -->
<table width='100%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<td width='500' height='30'> </td>
<td align='right'>
<a href='board_write1.jsp'><img src='../images/btn_wri.gif' border='0'></a>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>
</body>
</html>
6. board_view1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="model1.BoardTO"%>
<%@page import="model1.BoardDAO"%>
<%
request.setCharacterEncoding("utf-8");
//선택글 번호 받아서 TO에 넣음
BoardTO to = new BoardTO();
to.setSeq(request.getParameter("seq"));
//DAO View에서 필요한 DB데이터를 처리해서 리턴
BoardDAO dao = new BoardDAO();
to = dao.boardView(to);
String seq = to.getSeq();
String subject =to.getSubject();
String writer = to.getWriter();
String mail = to.getMail();
String wip = to.getWip();
String wdate = to.getWdate();
String hit = to.getHit();
String content = to.getContent();
%>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='../images/common.css'>
</head>
<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
<td bgcolor='#ffffff' style='padding:20'>
<table width='100%' cellpadding='0' cellspacing='0' border='0' align='center'>
<tr>
<td height='25'><font class='titdot'>• </font><font class='title'>제목</font> | <%=subject %></td>
</tr>
<tr>
<td class='imgline'></td>
</tr>
<tr>
<td height='25'><font class='titdot'>• </font><font class='title'>이름</font> | <%=writer %>(<%=mail %>) (<%=wip %>)</td>
</tr>
<tr>
<td class='imgline'></td>
</tr>
<tr>
<td height='25'>
<table width='100%' cellpadding='0' cellspacing='0' border='0' align='center'>
<tr>
<td><font class='titdot'>•</font><font class='title'> 날짜</font> | <%=wdate %></td>
<td align='right'><font class='titdot'>• </font><font class='title'>조회</font> | <%=hit %></td>
</tr>
</table>
</td>
</tr>
<tr>
<td class='imgline'></td>
</tr>
<tr>
<td height='100'><%=content %></td>
</tr>
</table>
<table width='100%' border='0' cellspacing='0' cellpadding='0' align='center'>
<tr>
<td id='tailarea'></td>
</tr>
</table>
<table width='100%' border='0' cellspacing='1' cellpadding='1'>
<tr>
<td colspan='2' class='gline'></td>
</tr>
<tr>
<td width='500' height='30'>
<a href='board_write1.jsp'><img src='../images/btn_wri.gif' border='0'></a>
<a href='board_modify1.jsp?seq=<%=seq%>'><img src='../images/btn_mod.gif' border='0'></a> <%-- 글수정 URL seq녛기 --%>
<a href='board_delete1.jsp?seq=<%=seq%>'><img src='../images/btn_del.gif' border='0'></a> <%-- 글삭제 URL seq녛기 --%>
</td>
<td align='right'>
<a href='board_list1.jsp'><img src='../images/btn_list.gif' border='0'></a>
</td>
</tr>
<tr>
<td colspan='2' class='gline'></td>
</tr>
</table>
</td>
</tr>
</table>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>
</body>
</html>
7. board_modify1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="model1.BoardTO"%>
<%@page import="model1.BoardDAO"%>
<%
request.setCharacterEncoding("utf-8");
//선택글 번호 받아서 TO에 넣음
BoardTO to = new BoardTO();
to.setSeq(request.getParameter("seq"));
//DAO modify에서 필요한 DB데이터를 처리해서 리턴
BoardDAO dao = new BoardDAO();
to = dao.boardModify(to);
String seq = to.getSeq();
String subject =to.getSubject();
String writer = to.getWriter();
String mail = to.getMail();
String content = to.getContent();
%>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='../images/common.css'>
<script type="text/javascript">
function ChkForm(form) {
//필수 입력값 검사
if(document.wfrm.password.value.trim() ==""){
alert('암호를 입력하셔야 합니다.');
return false;
}
}
</script>
</head>
<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
<td bgcolor='#ffffff' style='padding:20'>
<form action='board_modify1_ok.jsp' method='post' name='wfrm' onSubmit='return ChkForm(this)'>
<input type='hidden' name='seq' value='<%=seq%>'> <%-- seq를 히든으로 보냄 --%>
<table width='100%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>제목</font> :
</td>
<td>
<input type='text' name='subject' value='<%=subject %>' size='80' class='form'>
</td>
</tr>
<tr>
<td colspan='2' class='imgline'></td>
</tr>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>작성자</font> :
</td>
<td>
이름 <input type='text' name='writer' value='<%=writer %>' size='10' maxlength='10' class='form' readonly> /
메일 <input type='text' name='mail' value='<%=mail %>' size='40' maxlength='70' class='form'> /
암호 <input type='password' name='password' size='10' maxlength='10' class='form'>
</td>
</tr>
<tr>
<td colspan='2' class='imgline'></td>
</tr>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>내용</font> :
</td>
<td>
<textarea name='content' style='width:620;height:300' class='form'><%=content %></textarea>
</td>
</tr>
<tr>
<td colspan='2' class='gline'></td>
</tr>
</table>
<table width='100%' cellpadding='0' cellspacing='0' border='0'>
<tr>
<td width='500' height='30'> </td>
<td align='right'>
<input type='image' src='../images/btn_mod.gif' border='0'>
<a href='board_list1.jsp'><img src='../images/btn_list.gif' border='0'></a>
<a href='javascript:history.back();'><img src='../images/btn_view.gif' border='0'></a>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>
</body>
</html>
8. board_modify1_ok
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="model1.BoardTO"%>
<%@page import="model1.BoardDAO"%>
<%
request.setCharacterEncoding("utf-8");
//데이터 클래스를 생성, 클라이언트 정보 넣음
BoardTO to = new BoardTO();
to.setSeq(request.getParameter("seq"));
to.setSubject(request.getParameter("subject"));
to.setMail(request.getParameter("mail"));
to.setPassword(request.getParameter("password"));
to.setContent(request.getParameter("content"));
//DAO클래스에서 modify_ok에서 필요한 DB데이터를 처리해서 리턴
BoardDAO dao = new BoardDAO();
int flag = dao.boardModifyOk(to);
//성공실패 메시지
out.println("<script type='text/javascript'>");
if(flag ==0){
out.println("alert('글수정에 성공했습니다')");
out.println("location.href='board_list1.jsp';");
}else if(flag==1){
out.println("alert('비밀번호가 잘못되었습니다.')");
out.println("history.back();");
}else{
out.println("alert('글수정에 실패했습니다')");
out.println("history.back();");
}
out.println("</script>");
%>
9. board_delete1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="model1.BoardTO"%>
<%@page import="model1.BoardDAO"%>
<%
request.setCharacterEncoding("utf-8");
//선택글 번호 받아서 TO에 넣음
BoardTO to = new BoardTO();
to.setSeq(request.getParameter("seq"));
//DAO delete에서 필요한 DB데이터를 처리해서 리턴
BoardDAO dao = new BoardDAO();
to = dao.boardDelete(to);
String seq = to.getSeq();
String subject =to.getSubject();
String writer = to.getWriter();
String mail = to.getMail();
%>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='../images/common.css'>
<script type="text/javascript">
function ChkForm(form) {
//필수 입력값 검사
if(document.wfrm.password.value.trim() ==""){
alert('암호를 입력하셔야 합니다.');
return false;
}
}
</script>
</head>
<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
<td bgcolor='#ffffff' style='padding:20'>
<form action='board_delete1_ok.jsp' method='post' name='wfrm' onSubmit='return ChkForm(this)'>
<input type='hidden' name='seq' value='<%=seq%>'> <%-- seq를 히든으로 보냄 --%>
<table width='100%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>제목</font> :
</td>
<td>
<input type='text' name='subject' value='<%=subject %>' size='80' class='form' readonly>
</td>
</tr>
<tr>
<td colspan='2' class='imgline'></td>
</tr>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>작성자</font> :
</td>
<td>
이름 <input type='text' name='writer' value='<%=writer %>' size='10' maxlength='10' class='form' readonly> /
메일 <input type='text' name='mail' value='<%=mail %>' size='40' maxlength='70' class='form' readonly> /
암호 <input type='password' name='password' size='10' maxlength='10' class='form'>
</td>
</tr>
<tr>
<td colspan='2' class='gline'></td>
</tr>
</table>
<table width='100%' cellpadding='0' cellspacing='0' border='0'>
<tr>
<td width='500' height='30'> </td>
<td align='right'>
<input type='image' src='../images/btn_del.gif' border='0'>
<a href='board_list1.jsp'><img src='../images/btn_list.gif' border='0'></a>
<a href='javascript:history.back();'><img src='../images/btn_view.gif' border='0'></a>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>
</body>
</html>
10. board_delete1_ok
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="model1.BoardTO"%>
<%@page import="model1.BoardDAO"%>
<%
request.setCharacterEncoding("utf-8");
//데이터 클래스를 생성, 클라이언트 정보 넣기
BoardTO to = new BoardTO();
to.setSeq(request.getParameter("seq"));
to.setPassword(request.getParameter("password"));
//DAO클래스에서 delete_ok에서 필요한 DB데이터를 처리해서 리턴
BoardDAO dao = new BoardDAO();
int flag = dao.boardDeleteOk(to);
//성공실패 메시지
out.println("<script type='text/javascript'>");
if(flag ==0){
out.println("alert('글삭제에 성공했습니다')");
out.println("location.href='board_list1.jsp';");
}else if(flag==1){
out.println("alert('비밀번호가 잘못되었습니다.')");
out.println("history.back();");
}else{
out.println("alert('글삭제에 실패했습니다')");
out.println("history.back();");
}
out.println("</script>");
%>
11. 페이지 네비게이션 게시판 model1
폴더구성
BoardTO : 데이터용 클래스
package model1;
// 데이터용 클래스 만듦
public class BoardTO {
private String seq;
private String subject;
private String writer;
private String mail;
private String password;
private String content;
private String hit;
private String wip;
private String wdate;
private int wgap;
public String getSeq() {
return seq;
}
public String getSubject() {
return subject;
}
public String getWriter() {
return writer;
}
public String getMail() {
return mail;
}
public String getPassword() {
return password;
}
public String getContent() {
return content;
}
public String getHit() {
return hit;
}
public String getWip() {
return wip;
}
public String getWdate() {
return wdate;
}
public int getWgap() {
return wgap;
}
public void setSeq(String seq) {
this.seq = seq;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setWriter(String writer) {
this.writer = writer;
}
public void setMail(String mail) {
this.mail = mail;
}
public void setPassword(String password) {
this.password = password;
}
public void setContent(String content) {
this.content = content;
}
public void setHit(String hit) {
this.hit = hit;
}
public void setWip(String wip) {
this.wip = wip;
}
public void setWdate(String wdate) {
this.wdate = wdate;
}
public void setWgap(int wgap) {
this.wgap = wgap;
}
}
BoardListTO : 페이징 데이터용 클래스
package model1;
//페이징 데이터용 클래스
import java.util.ArrayList;
public class BoardListTO {
private int cpage;
private int recordPerPage;
private int blockPerPage;
private int totalPage;
private int startBlock;
private int endBlock;
private ArrayList<BoardTO> boardList; //한페이지 안의 모든 데이터
//생성자 초기설정
public BoardListTO(){
this.cpage= 1;
this.recordPerPage = 10;
this.blockPerPage = 5;
this.totalPage = 1;
}
public int getCpage() {
return cpage;
}
public int getRecordPerPage() {
return recordPerPage;
}
public int getBlockPerPage() {
return blockPerPage;
}
public int getTotalPage() {
return totalPage;
}
public int getStartBlock() {
return startBlock;
}
public int getEndBlock() {
return endBlock;
}
public ArrayList<BoardTO> getBoardList() {
return boardList;
}
public void setCpage(int cpage) {
this.cpage = cpage;
}
public void setRecordPerPage(int recordPerPage) {
this.recordPerPage = recordPerPage;
}
public void setBlockPerPage(int blockPerPage) {
this.blockPerPage = blockPerPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public void setStartBlock(int startBlock) {
this.startBlock = startBlock;
}
public void setEndBlock(int endBlock) {
this.endBlock = endBlock;
}
public void setBoardList(ArrayList<BoardTO> boardList) {
this.boardList = boardList;
}
}
BoardDAO : 데이터처리 클래스
package model1;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class BoardDAO {
private DataSource dataSource = null;
public BoardDAO(){
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
this.dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
} catch (NamingException e) {
System.out.println("에러 : "+ e.getMessage());
}
}
public void boardWrite(){
}
public int boardWriteOk(BoardTO to){
return 1;
}
public BoardListTO boardList(BoardListTO listTO){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
int cpage = listTO.getCpage();
int recordPerPage = listTO.getRecordPerPage();
int blockPerPage = listTO.getBlockPerPage();
conn = this.dataSource.getConnection();
String sql = "select seq, subject, writer, to_char(wdate, 'YY.MM.DD') wdate, hit, trunc(sysdate-wdate) wgap from board1 order by seq desc";
pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = pstmt.executeQuery();
//전체데이터 갯수
rs.last();
int totalRecord = rs.getRow();
rs.beforeFirst();
//전체 페이지 갯수
listTO.setTotalPage((totalRecord -1)/recordPerPage+1);
//페이지를 넘길떄 처음 읽을 데이터의 행
int skip = (cpage -1)*recordPerPage;
if(skip !=0) rs.absolute(skip);
//recordPerPage만큼 짤라서 화면에 출력
ArrayList<BoardTO> boardList = new ArrayList<>();
for(int i =0;i<recordPerPage && rs.next();i++){
BoardTO to = new BoardTO();
to.setSeq(rs.getString("seq"));
to.setSubject(rs.getString("subject"));
to.setWriter(rs.getString("writer"));
to.setWdate(rs.getString("wdate"));
to.setHit(rs.getString("hit"));
to.setWgap(rs.getInt("wgap"));
boardList.add(to);
}
//화면하단의 페이지 블락 시작, 끝
listTO.setBoardList(boardList);
listTO.setStartBlock(((cpage-1)/blockPerPage)*blockPerPage+1);
listTO.setEndBlock(((cpage-1)/blockPerPage)*blockPerPage+blockPerPage);
if(listTO.getEndBlock() >= listTO.getTotalPage()){
listTO.setEndBlock(listTO.getTotalPage());
}
} catch (SQLException e) {
System.out.println("에러 : "+ e.getMessage());
} finally{
if(rs!=null)try {rs.close();} catch (SQLException e) {}
if(pstmt!=null)try {pstmt.close();} catch (SQLException e) {}
if(conn!=null)try {conn.close();} catch (SQLException e) {}
}
return listTO;
}
public BoardTO boardView(BoardTO to){
return null;
}
public BoardTO boardModify(BoardTO to){
return null;
}
public int boardModifyOk(BoardTO to){
return 1;
}
public BoardTO boardDelete(BoardTO to){
return null;
}
public int boardDeleteOk(BoardTO to){
return 1;
}
}
12.board_list1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="model1.BoardTO" %>
<%@ page import="model1.BoardListTO" %>
<%@ page import="model1.BoardDAO" %>
<%@ page import="java.util.ArrayList" %>
<%
request.setCharacterEncoding("utf-8");
// 페이지 번호
int cpage = 1;
if(request.getParameter("cpage") != null && !request.getParameter("cpage").equals("")) {
cpage = Integer.parseInt(request.getParameter("cpage"));
}
//페이지네비게이션 초기값 지정
BoardListTO listTO = new BoardListTO();
listTO.setCpage(cpage);
//DAO list에서 현재페이지에 맞는 페이지네비게이션 값 리턴
BoardDAO dao = new BoardDAO();
listTO = dao.boardList(listTO);
int totalPage =listTO.getTotalPage();
int blockPerPage = listTO.getBlockPerPage();
int startBlock = listTO.getStartBlock();
int endBlock = listTO.getEndBlock();
ArrayList<BoardTO> boardList = listTO.getBoardList();
StringBuffer result = new StringBuffer();
//레코드 데이터 리턴
for(BoardTO to : boardList) {
String seq = to.getSeq();
String subject = to.getSubject();
String writer = to.getWriter();
String wdate = to.getWdate();
String hit = to.getHit();
int wgap = to.getWgap();
result.append("<table width='100%' border='0' cellpadding='0' cellspacing='0'>");
result.append("<tr>");
result.append(" <td height='1'></td>");
result.append("</tr>");
result.append("<tr>");
result.append(" <td>");
result.append(" <table width='100%' border='0' cellpadding='0' cellspacing='0'>");
result.append(" <tr height='25' onMouseOver=\"this.className='evencell'\" onMouseOut=\"this.className=''\">");
result.append(" <td width='40' align='center'>" + seq + "</td>");
result.append(" <td>");
result.append(" <span style='width:370' class='elltxt'>");
result.append(" <a href='board_view1.jsp?cpage=" + cpage + "&seq=" + seq + "'>" + subject + "</a>");
if(wgap == 0) {
result.append(" <img src='../images/ico_n.gif' width='8' height='8' border='0' hspace='3'>");
}
result.append(" </span>");
result.append(" </td>");
result.append(" <td width='100' align='center'>" + writer + "</td>");
result.append(" <td width='80' align='center'>" + wdate + "</td>");
result.append(" <td width='50' align='center'>" + hit + "</td>");
result.append(" </tr>");
result.append(" </table>");
result.append(" </td>");
result.append("</tr>");
result.append("<tr>");
result.append(" <td height='1'></td>");
result.append("</tr>");
result.append("<tr>");
result.append(" <td align='center' class='imgline'></td>");
result.append("</tr>");
result.append("</table>");
}
%>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='../images/common.css'>
</head>
<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
<td bgcolor='#ffffff' style='padding:20'>
<table width='100%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<td align='center' class='gline'></td>
</tr>
<tr>
<td align='center'>
<table width='100%' border='0' cellpadding='0' cellspacing='0' class='titlecell'>
<tr height='25' align='center'>
<td width='40'>No</td>
<td>제목</td>
<td width='100'>이름</td>
<td width='80'>등록일</td>
<td width='50'>조회수</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align='center' class='gline'></td>
</tr>
<tr>
<td height='3'></td>
</tr>
</table>
<%=result %>
<table width='100%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<td width='500' height='30'>
<!-- 페이지 네비게이션 -->
<%
if(startBlock == 1) {
out.println("<img src='../images/moveset/p_left_arrow_01_off.gif' align='absmiddle' hspace='2' border='0'>");
} else {
out.println("<a href='board_list1.jsp?cpage=" + (startBlock - blockPerPage) + "'><img src='../images/moveset/p_left_arrow_01_off.gif' align='absmiddle' hspace='2' border='0'></a>");
}
// 이전 페이지 가기
if(cpage == 1) {
out.println("<img src='../images/moveset/p_left_arrow_02_off.gif' align='absmiddle' hspace='8' border='0'>");
} else {
out.println("<a href='board_list1.jsp?cpage=" + (cpage - 1) + "'><img src='../images/moveset/p_left_arrow_02.gif' align='absmiddle' hspace='8' border='0'></a>");
}
out.println("<img src='../images/moveset/p_orange.gif' align='absmiddle' hspace='3' border='0'>");
// 현재 페이지 표시
for(int i=startBlock ; i<=endBlock ; i++) {
if (cpage == i) {
out.println("<b><u>[" + i + "]</u></b> ");
} else {
out.println("<a href='board_list1.jsp?cpage=" + i + "'>" + i + "</a> ");
}
}
out.println("<img src='../images/moveset/p_orange.gif' align='absmiddle' hspace='3' border='0'>");
if(cpage == totalPage) {
out.println("<img src='../images/moveset/p_right_arrow_02_off.gif' align='absmiddle' hspace='8' border='0'>");
} else {
out.println("<a href='board_list1.jsp?cpage=" + (cpage + 1) + "'><img src='../images/moveset/p_right_arrow_02.gif' align='absmiddle' hspace='8' border='0'></a>");
}
if(endBlock == totalPage) {
out.println("<img src='../images/moveset/p_right_arrow_01_off.gif' align='absmiddle' hspace='2' border='0'>");
} else {
out.println("<a href='board_list1.jsp?cpage=" + (startBlock + blockPerPage) + "'><img src='../images/moveset/p_right_arrow_01_off.gif' align='absmiddle' hspace='2' border='0'></a>");
}
%>
</td>
<td align='right'>
<a href='board_write1.jsp?cpage=<%=cpage %>'><img src='../images/btn_wri.gif' border='0'></a>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>
</body>
</html>
댓글 없음:
댓글 쓰기