2016년 8월 3일 수요일

03day JSP

디자인이나온 웹에 프로그래밍코드를 넣을 때 고려사항

  • 데이터베이스
    • 테이블 설계

      • 번호 seq            number                            not null   primary key
      • 제목 subject       varchar2(100)                     not null
      • 이름 writer         varchar2(12)                      not null
      • 메일 mail           varchar2(50)
      • 암호 password    varchar2(12)                      not null
      • 내용 content      varchar2(2000) / long / clob
      • 조회수 hit          number                            not null
      • 아이피 wip         varchar2(15)                      not null
      • 등록일 wdate      date                                not null 


create table board1(
seq            number                            not null   primary key,
subject       varchar2(100)                     not null,
writer          varchar2(12)                      not null,
mail           varchar2(50),
password    varchar2(12)                      not null,
content      varchar2(2000),
hit            number                            not null,
wip           varchar2(15)                      not null,
wdate        date                                not null 
);
    • 사용자 생성 
      • conn system/123456  :db최고 관리자
      • conn /as sysdba        :os 최고 관리자

create user project identified by project;
grant connect, resource to project;
conn project/project
create table ~~
create sequence board_seq;
--테스트 행 삽입
insert into board1 values (board_seq.nextval, '제목', '이름', 'test@test.com', '1234', '내용',
0, '000.000.000.000', sysdate);
  • 페이지간 흐름 설계 : 페이지 네비게이션
    • 글목록                (board_list1.jsp)
      • 글쓰기         (board_write1.jsp)   +(DB처리→list) 숨김페이지
        board_write1_ok.jsp
      • 자세히보기   (board_view1.jsp)
        • 글수정  (board_modify1.jsp) +(DB처리→view)  숨김페이지
          board_modify1_ok.jsp
        • 글삭제  (board_delete1.jsp)  +(DB처리→list)  숨김페이지
          board_delete1_ok.jsp


1.context
<?xml version="1.0" encoding="utf-8" ?>
<Context>
    <!-- 톰캣 환경 설정 -->
    <Resource
        name="jdbc/oracle"
        auth="Container"
        type="javax.sql.DataSource"
        driverClassName="oracle.jdbc.driver.OracleDriver"
        url="jdbc:oracle:thin:@Localhost:1521:orcl"
        username="project"
        password="project"
        maxTotal="20"
        maxWaitMillis="5000" />

</Context>


2.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'>&#149;&nbsp;</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'>&#149;&nbsp;</font>
                <font class='title'>작성자</font> :
            </td>
            <td>
                이름&nbsp;&nbsp;<input type='text' name='writer' size='10' maxlength='10' class='form'>&nbsp;&nbsp;/&nbsp;
                메일&nbsp;&nbsp;<input type='text' name='mail' size='40' maxlength='70' class='form'>&nbsp;&nbsp;/&nbsp;
                암호&nbsp;&nbsp;<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'>&#149;&nbsp;</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'>&nbsp;</td>
            <td align='right'>
                <input type='image' src='../images/btn_wri.gif' border='0'>&nbsp;
                <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>


3.board_write1_ok  숨김페이지
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.PreparedStatement" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "javax.naming.Context" %>
<%@ page import = "javax.naming.InitialContext" %>
<%@ page import = "javax.naming.NamingException" %>
<%@ page import = "javax.sql.DataSource" %>
<%-- 숨김페이지라서 HTML 코드 없음 --%>
<%
    request.setCharacterEncoding("utf-8");
    //클라이언트 정보 받기
    String subject =request.getParameter("subject");
    String writer =request.getParameter("writer");
    String mail =request.getParameter("mail");
    String password =request.getParameter("password");
    String content =request.getParameter("content");
    String wip = request.getRemoteAddr();  //클라이언트 아이피
    
    //데이터베이스 연결
    Connection conn = null;
    PreparedStatement pstmt = null;
    int flag = 1;  //데이터베이스 입력 성공실패 여부  성공0, 실패 1
    
    try{
        Context initCtx = new InitialContext();
        Context envCtx = (Context)initCtx.lookup("java:comp/env");
        DataSource dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
        
        conn = dataSource.getConnection();
        
        String sql = "insert into board1 values (board_seq.nextval, ?, ?, ?, ?, ?, 0, ?, sysdate)";
        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, subject);
        pstmt.setString(2, writer);
        pstmt.setString(3, mail);
        pstmt.setString(4, password);
        pstmt.setString(5, content);
        pstmt.setString(6, wip);
        
        //executeUpdate 리턴 값: 영향 받은 행수
        int result = pstmt.executeUpdate();
        if(result ==1){
            flag =0;
        }
        
    }catch(NamingException e){
        System.out.println("에러 : " + e.getMessage());
    }catch(SQLException e){
        System.out.println("에러 : " + e.getMessage());
    }finally{
        if(pstmt !=null)pstmt.close();
        if(conn !=null)conn.close();
    }
    
    //성공시 리스트페이지 , 실패시 이전페이지로 이동 자바스크립트
    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>");
    
%>


4.board_list1
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.PreparedStatement" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.SQLException" %>

<%@ page import = "javax.naming.Context" %>
<%@ page import = "javax.naming.InitialContext" %>
<%@ page import = "javax.naming.NamingException" %>
<%@ page import = "javax.sql.DataSource" %>
<%
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    StringBuffer result = new StringBuffer();
    
    try{
        Context initCtx = new InitialContext();
        Context envCtx = (Context)initCtx.lookup("java:comp/env");
        DataSource dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
        
        conn = dataSource.getConnection();
        
        String sql = "select seq, subject, writer, to_char(wdate,'YY.MM.DD') wdate, hit from board1 order by seq desc";
        pstmt = conn.prepareStatement(sql);
        
        rs= pstmt.executeQuery();
        while(rs.next()){
            String seq = rs.getString("seq");
            String subject = rs.getString("subject");
            String writer = rs.getString("writer");
            String wdate = rs.getString("wdate");
            String hit = rs.getString("hit");
            
            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>");
            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>");            
        }
        
    }catch(NamingException e){
        System.out.println("에러 : " + e.getMessage());
    }catch(SQLException e){
        System.out.println("에러 : " + e.getMessage());
    }finally{
        if(rs!=null) rs.close();
        if(pstmt!=null) pstmt.close();
        if(conn!=null) conn.close();
    }

%>
<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'>&nbsp;</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>


5.board_view1
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.PreparedStatement" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.SQLException" %>

<%@ page import = "javax.naming.Context" %>
<%@ page import = "javax.naming.InitialContext" %>
<%@ page import = "javax.naming.NamingException" %>
<%@ page import = "javax.sql.DataSource" %>

<%
    //선택한 글의 seq 받아오기
    request.setCharacterEncoding("utf-8");
    String seq = request.getParameter("seq");
    
    String subject ="";
    String writer = "";
    String mail = "";
    String wip = "";
    String wdate = "";
    String hit = "";
    String content = "";
    
    //데이터베이스 연결
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    
    try{
        Context initCtx = new InitialContext();
        Context envCtx = (Context)initCtx.lookup("java:comp/env");
        DataSource dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
        
        conn = dataSource.getConnection();
        
        //조회수 증가
        String sql = "update board1 set hit=hit+1 where seq=?";
        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, seq);
        pstmt.executeUpdate();
        
        //view창 출력
        sql = "select subject, writer, mail, wip, wdate, hit, content from board1 where seq=?";
        pstmt=conn.prepareStatement(sql);
        pstmt.setString(1, seq);
        
        rs = pstmt.executeQuery();
        if(rs.next()){
            subject = rs.getString("subject");
            writer = rs.getString("writer");
            mail = rs.getString("mail")==null?"":rs.getString("mail");   //공백처리
            wip = rs.getString("wip");
            wdate = rs.getString("wdate");
            hit = rs.getString("hit");
            content = rs.getString("content")==null?"":rs.getString("content").replaceAll("\n", "<br>"); //엔터키 처리
        }
        
    }catch(NamingException e){
        System.out.println("에러 : " + e.getMessage());
    }catch(SQLException e){
        System.out.println("에러 : " + e.getMessage());
    }finally{
        if(rs!=null) rs.close();
        if(pstmt!=null) pstmt.close();
        if(conn!=null) conn.close();
    }
%>

<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'>&#149;&nbsp;</font><font class='title'>제목</font> | <%=subject %></td>
        </tr>
        <tr>
            <td class='imgline'></td>
        </tr>
        <tr>
            <td height='25'><font class='titdot'>&#149;&nbsp;</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'>&#149;</font><font class='title'> 날짜</font> | <%=wdate %></td>
                    <td align='right'><font class='titdot'>&#149;&nbsp;</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>


6.board_delete1
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.PreparedStatement" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.SQLException" %>

<%@ page import = "javax.naming.Context" %>
<%@ page import = "javax.naming.InitialContext" %>
<%@ page import = "javax.naming.NamingException" %>
<%@ page import = "javax.sql.DataSource" %>

<%
    //선택한 글의 seq 받아오기
    request.setCharacterEncoding("utf-8");
    String seq = request.getParameter("seq");
    
    String subject ="";
    String writer = "";
    String mail = "";

    
    //데이터베이스 연결
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    
    try{
        Context initCtx = new InitialContext();
        Context envCtx = (Context)initCtx.lookup("java:comp/env");
        DataSource dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
        
        conn = dataSource.getConnection();
        
        //delete창 출력
        String sql = "select subject, writer, mail from board1 where seq=?";
        pstmt=conn.prepareStatement(sql);
        pstmt.setString(1, seq);
        
        rs = pstmt.executeQuery();
        if(rs.next()){
            subject = rs.getString("subject");
            writer = rs.getString("writer");
            mail = rs.getString("mail")==null?"":rs.getString("mail");
        }
        
    }catch(NamingException e){
        System.out.println("에러 : " + e.getMessage());
    }catch(SQLException e){
        System.out.println("에러 : " + e.getMessage());
    }finally{
        if(rs!=null) rs.close();
        if(pstmt!=null) pstmt.close();
        if(conn!=null) conn.close();
    }
%>

<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'>&#149;&nbsp;</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'>&#149;&nbsp;</font>
                <font class='title'>작성자</font> :
            </td>
            <td>
                이름&nbsp;&nbsp;<input type='text' name='writer' value='<%=writer %>' size='10' maxlength='10' class='form' readonly>&nbsp;&nbsp;/&nbsp;
                메일&nbsp;&nbsp;<input type='text' name='mail' value='<%=mail %>' size='40' maxlength='70' class='form' readonly>&nbsp;&nbsp;/&nbsp;
                암호&nbsp;&nbsp;<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'>&nbsp;</td>
            <td align='right'>
                <input type='image' src='../images/btn_del.gif' border='0'>&nbsp;
                <a href='board_list1.jsp'><img src='../images/btn_list.gif' border='0'></a>&nbsp;
                <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>


7.board_delete1_ok
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.PreparedStatement" %>
<%@ page import = "java.sql.SQLException" %>

<%@ page import = "javax.naming.Context" %>
<%@ page import = "javax.naming.InitialContext" %>
<%@ page import = "javax.naming.NamingException" %>
<%@ page import = "javax.sql.DataSource" %>
<%-- 숨김페이지라서 HTML 코드 없음 --%>
<%
    request.setCharacterEncoding("utf-8");
    //클라이언트 정보 받기
    String seq =request.getParameter("seq");
    String password =request.getParameter("password");
    
    //데이터베이스 연결
    Connection conn = null;
    PreparedStatement pstmt = null;
    int flag = 2;  //데이터베이스 입력 성공실패 여부  성공0, 비번다름 1
    
    try{
        Context initCtx = new InitialContext();
        Context envCtx = (Context)initCtx.lookup("java:comp/env");
        DataSource dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
        
        conn = dataSource.getConnection();
        
        String sql = "delete from board1 where seq=? and password=?";
        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, seq);
        pstmt.setString(2, password);
        
        //executeUpdate 리턴 값: 영향 받은 행수
        int result = pstmt.executeUpdate();
        if(result ==0){
            flag =1;
        }else if(result ==1){
            flag =0;
        }
        
    }catch(NamingException e){
        System.out.println("에러 : " + e.getMessage());
    }catch(SQLException e){
        System.out.println("에러 : " + e.getMessage());
    }finally{
        if(pstmt !=null)pstmt .close();
        if(conn !=null)conn.close();
    }
    
    //성공시 리스트페이지 , 실패시 이전페이지로 이동 자바스크립트
    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>");
    
%>


8.board_modify1
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.PreparedStatement" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.SQLException" %>

<%@ page import = "javax.naming.Context" %>
<%@ page import = "javax.naming.InitialContext" %>
<%@ page import = "javax.naming.NamingException" %>
<%@ page import = "javax.sql.DataSource" %>

<%
    //선택한 글의 seq 받아오기
    request.setCharacterEncoding("utf-8");
    String seq = request.getParameter("seq");
    
    String subject ="";
    String writer = "";
    String mail = "";
    String content = "";

    
    //데이터베이스 연결
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    
    try{
        Context initCtx = new InitialContext();
        Context envCtx = (Context)initCtx.lookup("java:comp/env");
        DataSource dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
        
        conn = dataSource.getConnection();
        
        //modify창 출력
        String sql = "select subject, writer, mail,content from board1 where seq=?";
        pstmt=conn.prepareStatement(sql);
        pstmt.setString(1, seq);
        
        rs = pstmt.executeQuery();
        if(rs.next()){
            subject = rs.getString("subject");
            writer = rs.getString("writer");
            mail = rs.getString("mail")==null?"":rs.getString("mail");
            content = rs.getString("content")==null?"":rs.getString("content").replaceAll("\n", "<br>");
        }
        
    }catch(NamingException e){
        System.out.println("에러 : " + e.getMessage());
    }catch(SQLException e){
        System.out.println("에러 : " + e.getMessage());
    }finally{
        if(rs!=null) rs.close();
        if(pstmt!=null) pstmt.close();
        if(conn!=null) conn.close();
    }
%>
<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'>&#149;&nbsp;</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'>&#149;&nbsp;</font>
                <font class='title'>작성자</font> :
            </td>
            <td>
                이름&nbsp;&nbsp;<input type='text' name='writer' value='<%=writer %>' size='10' maxlength='10' class='form' readonly>&nbsp;&nbsp;/&nbsp;
                메일&nbsp;&nbsp;<input type='text' name='mail' value='<%=mail %>' size='40' maxlength='70' class='form'>&nbsp;&nbsp;/&nbsp;
                암호&nbsp;&nbsp;<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'>&#149;&nbsp;</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'>&nbsp;</td>
            <td align='right'>
                <input type='image' src='../images/btn_mod.gif' border='0'>&nbsp;
                <a href='board_list1.jsp'><img src='../images/btn_list.gif' border='0'></a>&nbsp;
                <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>


9.board_modify1_ok
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.PreparedStatement" %>
<%@ page import = "java.sql.SQLException" %>

<%@ page import = "javax.naming.Context" %>
<%@ page import = "javax.naming.InitialContext" %>
<%@ page import = "javax.naming.NamingException" %>
<%@ page import = "javax.sql.DataSource" %>
<%-- 숨김페이지라서 HTML 코드 없음 --%>
<%
    request.setCharacterEncoding("utf-8");
    //클라이언트 정보 받기
    String seq =request.getParameter("seq");
    String subject =request.getParameter("subject");
    String mail =request.getParameter("mail");
    String password =request.getParameter("password");
    String content =request.getParameter("content");
    
    //데이터베이스 연결
    Connection conn = null;
    PreparedStatement pstmt = null;
    int flag = 2;  //데이터베이스 입력 성공실패 여부  성공0, 비번다름 1
    
    try{
        Context initCtx = new InitialContext();
        Context envCtx = (Context)initCtx.lookup("java:comp/env");
        DataSource dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
        
        conn = dataSource.getConnection();
        
        String sql = "update board1 set subject=?, mail=?, content=? where seq=? and password=?";
        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, subject);
        pstmt.setString(2, mail);
        pstmt.setString(3, content);
        pstmt.setString(4, seq);
        pstmt.setString(5, password);
        
        //executeUpdate 리턴 값: 영향 받은 행수
        int result = pstmt.executeUpdate();
        if(result ==0){
            flag =1;
        }else if(result ==1){
            flag =0;
        }
        
    }catch(NamingException e){
        System.out.println("에러 : " + e.getMessage());
    }catch(SQLException e){
        System.out.println("에러 : " + e.getMessage());
    }finally{
        if(pstmt !=null)pstmt.close();
        if(conn !=null)conn.close();
    }
    
    //성공시 리스트페이지 , 실패시 이전페이지로 이동 자바스크립트
    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>");
    
%>


댓글 없음:

댓글 쓰기