20220317 º¹½À


ÁÖ¼Ò·Ï °ü¸® ÇÁ·Î±×·¥ ( DB¿¬µ¿ & Web )

     index.jsp => ÁÖ¼Ò·Ï ½ÇÇà ¸ÞÀÎ ( ÁÖ¼Ò·Ï ¸ñ·ÏÀ¸·Î ¹Ù·Î À̵¿ )

     ab_ctl.jsp => ÄÁÆ®·Ñ·¯ ( commandÀÇ °ª¿¡ µû¶ó È­¸é À̵¿ ¹× ¸Þ¼­µå ½ÇÇà )

     ab_list.jsp => ÁÖ¼Ò·Ï ¸ñ·Ï ( DB ¿¬°á ÈÄ, TableÀÇ µ¥ÀÌÅÍ °¡Á®¿Í¼­ È­¸é¿¡ Ç¥±â )

     ab_form.jsp => »õ ÁÖ¼Ò·Ï ÀÔ·Â ( AddrBook °´Ã¼¿¡ ÀԷµ¥ÀÌÅÍ ÀúÀå ÈÄ, DB¿¡ ÀúÀå )

     ab_edit_form.jsp => ÁÖ¼Ò·Ï ¼öÁ¤ ¹× »èÁ¦ ( ÁÖ¼Ò·Ï ¼±Åà ÈÄ, ÇØ´ç ÁÖ¼Ò·ÏÀÇ µ¥ÀÌÅ͸¸ °¡Á®¿Í¼­ ¼öÁ¤/»èÁ¦ ÁøÇà )

     ab_error.jsp => ¿À·ùó¸®

     ab.css => ½ºÅ¸ÀÏ ²Ù¹Ì±â

     addrbook(Package)
     AddrBook => DBÀÇ Çʵ忡 ÇØ´çÇÏ´Â ¸â¹ö º¯¼öµé & getter/setter
     AddrBean => DB connect, disconnect, insert, select µîÀÇ ¸Þ¼­µå ½ÇÇà


ab_ctl.jsp

     <jsp:useBean id="ab" class="addrbook.AddrBean" /> => AddrBeanÀÇ ¸Þ¼­µå¸¦ ½ÇÇàÇϱâ À§ÇÑ °´Ã¼ »ý¼º.
     <jsp:useBean id="abk" class="addrbook.AddrBook" /> => AddrBookÀÇ °´Ã¼ »ý¼º ÈÄ setProperty·Î º¯¼ö °ª ¼¼ÆÃ.
     <jsp:setProperty property="*" name="abk" /> => ab_form.jsp ¿¡¼­ ÀÔ·ÂÇÑ ³»¿ëÀ» abk °´Ã¼¿¡ ¼¼ÆÃ.

     String command = request.getParameter("command"); => command¸¦ ÀԷ¹ÞÀ½.
     String ab_id = request.getParameter("ab_id");

     switch(command){
     case "select" :
          ArrayList< AddrBook > datas = ab.selectDB(); => selectDB ¸Þ¼­µå¸¦ ÅëÇØ DataBase ¾ÈÀÇ ¸ðµç µ¥ÀÌÅÍ °¡Á®¿À±â.
          request.setAttribute("datas", datas); => »ó±â µ¥ÀÌÅ͵éÀ» ´ãÀº "datas" ¹è¿­À» °®°í ab_list.jsp ·Î À̵¿.
          pageContext.forward("ab_list.jsp");
          break;
     case "insert" :
          if(ab.insertDB(abk)){ => insertDB() ¸Þ¼­µå¸¦ ÅëÇØ abk( ab_form.jsp¿¡¼­ ÀÔ·ÂÇÑ ³»¿ëÀÌ ÀúÀåµÈ AddrBook °´Ã¼ )ÀÇ ³»¿ëÀ» Database¿¡ ÀúÀå.
               response.sendRedirect("ab_ctl.jsp?command=select");
          }else{
               pageContext.forward("ab_form.jsp");
          }
          break;
     case "update" :
          if( ab.updateDB(abk)){
               response.sendRedirect("ab_ctl.jsp?command=select");
          }else{
               response.sendRedirect("ab_ctl.jsp?command=edit&ab_id="+ab_id);
          }
          out.println("Hello Update");
          break;
     case "delete" :
          if( ab.deleteDB(ab_id) ){
               response.sendRedirect("ab_ctl.jsp?command=select");
          }else{
               response.sendRedirect("ab_ctl.jsp?command=edit&ab_id="+ab_id);
          }
          break;
     case "edit" :
          AddrBook abook = ab.getDB(ab_id); => getDB() ¸Þ¼­µå¸¦ ÅëÇØ À¯Àú°¡ ¼±ÅÃÇÑ ·¹Äڵ带 DB¿¡¼­ ãÀº ÈÄ, ÇØ´ç ·¹ÄÚµåÀÇ ³»¿ëÀ» abook °´Ã¼¿¡ ÀúÀå.
          request.setAttribute("abook",abook); => »ó±â °´Ã¼¸¦ °®°í ab_edit_form.jsp ·Î À̵¿ ÈÄ, ¼öÁ¤/»èÁ¦ ÀÛ¾÷ ÁøÇà.
          pageContext.forward("ab_edit_form.jsp");
          break;
     default :
          out.println("<script>alert('Please check Parameter out ^^')</script>"); => À߸øµÈ command ÀԷ½à ¿À·ù ½ºÅ©¸³Æ® Ç¥±â.
          break;
     }

ab_list.jsp

     <jsp:useBean id="datas" class="java.util.ArrayList" scope="request"/> => ab_ctl.jsp ¿¡¼­ °¡Á®¿Â ¹è¿­ datas °´Ã¼¸¦ »ý¼º.

     <% for(AddrBook ab : (ArrayList<AddrBook>)datas){ %>

          <td><%=ab.getAb_id() %></a></td>
          <td><a href="/0320_14/ab_ctl.jsp?command=edit&ab_id=<%=ab.getAb_id()%>"><%=ab.getAb_name() %></a></td> => command&ab_id °ª ¼³Á¤ ÈÄ, ¸µÅ©·Î À̵¿

     <% } %>


ab_form.jsp

     <form method="post" action="ab_ctl.jsp"> => action¿¡ ÇØ´çÇÏ´Â ÆäÀÌÁö·Î À̵¿.
     <input type="hidden" name="command" value="insert"> => »ó±â ÆäÀÌÁö·Î À̵¿Çϸ鼭 nameÀÇ °ªÀ» value·Î ÁöÁ¤ (hidden).
          <table width=100% border=1>
               <input type="text" name="º¯¼ö¸í"> ÅؽºÆ® ÀÔ·Â.
               <input type="radio" name="º¯¼ö¸í"> ¶óµð¿À ¹öÆ°.
               <select name="º¯¼ö¸í">
                    <option value="" selected> ¼±Åøñ·Ï.
                    <option value="">
               </select>
               <input type="submit" value="µî·Ï¹öÆ°"> Àü¼Û¹öÆ°.
          </table>
     </form>

ab_edit_form.jsp

     <jsp:useBean id="abook" class="addrbook.AddrBook" scope="request" /> => list¿¡¼­ ¼±ÅÃµÈ ·¹ÄÚµåÀÇ Á¤º¸°¡ ´ã±ä AddrBook °´Ã¼.
     <head>
     function delCheck(){
          result = confirm('Á¤¸»·Î »èÁ¦ÇÏ°Ú½À´Ï±î?'); => Delete ¹öÆ° Ŭ¸¯½Ã ½ÇÇàµÇ´Â ½ºÅ©¸³Æ® ¹®±¸.
          if(result == true)
          {
               document.form1.command.value="delete";
               document.form1.submit();
          } else {
               return;
          }
     }
     </head>

     <form method="post" action="ab_ctl.jsp" name="form1">
     <input type="hidden" name="command" value="update" name="ab_id" value=<%=abook.getAb_id() %>>
     <input type="submit" value="Update"> | <input type="button" value="Delete" onClick="delCheck()"> => Ŭ¸¯½Ã headÀÇ delCheck() ½ÇÇà.
     </form>

AddrBean.java

     try{ connect();

     } catch (~Exception e) {
          e.printStackTrace();
     }finally {
          disconnect(); => ¿¬°áÇßÀ¸¸é Ç×»ó finally·Î ¿¬°áÇØÁ¦.
     }

     Connection conn = null;
     PreparedStatement pstmt = null;

     Class.forName("com.mysql.cj.jdbc.Driver");
     conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db53", "id", "pw");

     pstmt=conn.prepareStatement("select * from addrbook");
     ResultSet rs = pstmt.executeQuery();
     while(rs.next()) {
          AddrBook ab = new AddrBook();
          ab.setAb_id(rs.getInt("ab_id"));
          ab.setAb_name(rs.getString("ab_name"));
          datas.add(ab);
     }

     pstmt=conn.prepareStatement("insert into addrbook(ab_name,ab_email,ab_tel,ab_birth,ab_comdept,ab_memo) values(?,?,?,?,?,?)");
     pstmt.setString(1, abk.getAb_name());
     pstmt.setString(2, abk.getAb_email());
     pstmt.executeUpdate();

     pstmt=conn.prepareStatement("select * from addrbook where ab_id=?");
     pstmt.setInt(1, ab_idx);
     ResultSet rs =pstmt.executeQuery();
     rs.next();
     abook.setAb_id(rs.getInt("ab_id"));
     abook.setAb_name(rs.getString("ab_name"));

     pstmt=conn.prepareStatement("update addrbook set ab_name=?,ab_email=?,ab_comdept=?,ab_birth=?,ab_tel=?,ab_memo=? where ab_id=?");
     pstmt.setString(1, abk.getAb_name());
     pstmt.setString(2, abk.getAb_email());
     pstmt.executeUpdate();

     pstmt=conn.prepareStatement("delete from addrbook where ab_id=?");
     pstmt.setString(1, ab_id);
     pstmt.executeUpdate();