개발 ON
  • [정보처리산업기사 | 실기] 수강신청 싸이트
    2023년 08월 08일 19시 30분 27초에 업로드 된 글입니다.
    작성자: 이주여이
    1. 프로젝트 생성 후, 환경설정에서 html, jsp, css, workspace에 UTF-8로 인코딩하기
    2. 톰캣 연동
    • 유효성 검사
      • input type text: frm.id.value.length == 0
      • input type select: frm.lecturer.value == 0
      • input type radio: frm.week[0].checked == flase && frm.week[1].checked == false && ...
    • 버튼에 submit 추가하기
      • <input type="button" value="추가" onclick="addCheck()">
      • function addCheck() { ... document.frm.submit();}
    • a 링크에 파라미터 넣기
      • <a href="modify.jsp?id=<%=rs.getString(1) %>"><%=rs.getString(1) %></a>
    • DB에 저장된 값 불러올 때, input 옵션 설정하기
      • input text → value=<%=id %> (id는 스트립틀릿에서 생성한 변수 / String id = rs.getString(1); ...)
      • input select → value=<%=if(lecturer.equals("1")) out.println("selected") %> ...
      • input radio → value=<%=if(week.equals("1")) out.println("checked") %> ...

    insert.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>insert</title>
    <script type="text/javascript" src="check.js"></script>
    </head>
    <body>
    <jsp:include page="header.jsp" />
    <section style="width: 100%; height: 500px; text-align: center">
    <h2 style="text-align: center">교과목 추가</h2>
    <form action="action.jsp" name="frm" method="post" style="display: flex; align-items: center; justify-content: center" >
    <input type="hidden" name="mode" value="insert">
    <table border="1">
    <tr>
    	<td>과목코드</td>
    	<td><input type="text" name="id" style="width: 96%"></td>
    </tr>
    <tr>
    	<td>과목명</td>
    	<td><input type="text" name="name" style="width: 96%"></td>
    </tr>
    <tr>
    	<td>학점</td>
    	<td><input type="text" name="credit" style="width: 96%"></td>
    </tr>
    <tr>
    	<td>담당강사</td>
    	<td>
    		<select name="lecturer" style="width: 100%">
    			<option value="">담당강사 선택</option>
    			<option value="1">김교수</option>
    			<option value="2">이교수</option>
    			<option value="3">박교수</option>
    			<option value="4">우교수</option>
    			<option value="5">최교수</option>
    			<option value="6">임교수</option>
    		</select>
    	</td>
    </tr>
    <tr>
    	<td>요일</td>
    	<td>
    	<input type="radio" name="week" value="1">월
    	<input type="radio" name="week" value="2">화
    	<input type="radio" name="week" value="3">수
    	<input type="radio" name="week" value="4">목
    	<input type="radio" name="week" value="5">금
    	</td>
    </tr>
    <tr>
    	<td>시작시간</td>
    	<td><input type="text" name="start_hour" style="width: 96%"></td>
    </tr>
    <tr>
    	<td>종료시간</td>
    	<td><input type="text" name="end_hour" style="width: 96%"></td>
    </tr>
    <tr>
    	<td colspan="2">
    		<input type="button" value="추가" onclick="addCheck()">
    		<input type="button" value="목록" onclick="location.href='list.jsp'">
    	</td>
    </tr>
    </table>
    </form>
    </section>
    
    <jsp:include page="footer.jsp" />
    </body>
    </html>

    list.jsp

    <%@page import="java.sql.ResultSet"%>
    <%@page import="java.sql.PreparedStatement"%>
    <%@page import="DBPKG.Util"%>
    <%@page import="java.sql.Statement"%>
    <%@page import="java.sql.Connection"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>list</title>
    </head>
    <body>
    <jsp:include page="header.jsp" />
    
    <section style="width: 100%; height: 500px; text-align: center">
    <h2 style="text-align: center">교과목 목록 조회/수정</h2>
    <form style="display: flex; text-align: center; justify-content: center;">
    <input type="hidden" name="mode" value="list">
    	<table border="1">
    		<tr>
    			<td>과목 코드</td>
    			<td>과목명</td>
    			<td>학점</td>
    			<td>담당강사</td>
    			<td>요일</td>
    			<td>시작시간</td>
    			<td>종료시간</td>
    			<td>삭제</td>
    		</tr>
    		
    		<%
    		request.setCharacterEncoding("UTF-8");
    		try {
    			Connection c = Util.getConnection();
    			String sql = "SELECT ID, C.NAME, CREDIT, L.NAME, WEEK, START_HOUR, END_HOUR " + 
    							"FROM COURSE_TBL C, LECTURER_TBL L " +
    							"WHERE C.LECTURER = L.IDX  "+
    							"ORDER BY ID";
    			PreparedStatement pstmt = c.prepareStatement(sql);
    			ResultSet rs = pstmt.executeQuery();
    			while(rs.next()) {
    				String week = rs.getString(5);
    				switch(week) {
    				case "1":
    					week = "월";
    					break;
    				case "2":
    					week = "화";
    					break;
    				case "3":
    					week = "수";
    					break;
    				case "4":
    					week = "목";
    					break;
    				case "5":
    					week = "금";
    					break;
    				}
    				
    				String startHour = rs.getString(6);
    				String endHour = rs.getString(7);
    				while(startHour.length()<4) {
    					startHour = "0" + startHour;
    				}
    				
    				// substring는 두번째 인자의 -1번째 값을 리턴하기 때문에 index + 1이 되어야한다.
    				String start = startHour.substring(0,2) + "시" + startHour.substring(2,4) + "분";
    				String end = rs.getString(7).substring(0, 2) + "시" + rs.getString(7).substring(2,4) + "분";
    				%> 
    			<tr>
    			<td><a href="modify.jsp?id=<%=rs.getString(1) %>"><%=rs.getString(1) %></a></td>
    			<td><%=rs.getString(2) %></td>
    			<td><%=rs.getString(3) %></td>
    			<td><%=rs.getString(4) %></td>
    			<td><%=week %></td>
    			<td><%=start %></td>
    			<td><%=end %></td>
    			<td><a href="action.jsp?id=<%=rs.getString(1) %>&mode=delete">삭제</a></td>
    			</tr>
    				<%
    			}
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    		%>
    	</table>
    </form>
    </section>
    
    <jsp:include page="footer.jsp" />
    </body>
    </html>

    modify.jsp

    <%@page import="java.sql.ResultSet"%>
    <%@page import="java.sql.PreparedStatement"%>
    <%@page import="DBPKG.Util"%>
    <%@page import="java.sql.Connection"%>
    <%@page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>modify</title>
    <script type="text/javascript" src="check.js"></script>
    </head>
    <body>
    <jsp:include page="header.jsp" />
    
    <section style="width: 100%; height: 500px; text-align: center">
    <h2 style="text-align: center">교과목 수정</h2>
    <form action="action.jsp" name="frm" method="post" style="display: flex; align-items: center; justify-content: center" >
    <input type="hidden" name="mode" value="modify">
    <table border="1">
    <%
    	request.setCharacterEncoding("UTF-8");
    	String id = request.getParameter("id");
    
    	try {
    		Connection c = Util.getConnection();
    		String sql = "SELECT * FROM COURSE_TBL WHERE ID = ?";
    		PreparedStatement pstmt = c.prepareStatement(sql);
    		pstmt.setString(1, id); // sql의 '?'에 값을 넣는다.
    		ResultSet rs = pstmt.executeQuery();
    		rs.next();
    		
    		String lecturer = rs.getString(4);
    		String week = rs.getString(5);
    %>
    	<tr>
    	<td>과목코드</td>
    	<td><input type="text" name="id" value="<%=id %>" style="width: 96%" readonly></td>
    </tr>
    <tr>
    	<td>과목명</td>
    	<td><input type="text" name="name" value="<%=rs.getString(2) %>" style="width: 96%"></td>
    </tr>
    <tr>
    	<td>학점</td>
    	<td><input type="text" name="credit" value="<%=rs.getString(3) %>"  style="width: 96%"></td>
    </tr>
    <tr>
    	<td>담당강사</td>
    	<td>
    		<select name="lecturer" style="width: 100%">
    			<option value="">담당 강사</option>
    			<!-- out.println(): 웹 화면에 출력한다. -->
    			<option value="1" <%if(lecturer.equals("1")) out.println("selected"); %>>김교수</option>
    			<option value="2" <%if(lecturer.equals("2")) out.println("selected"); %>>이교수</option>
    			<option value="3" <%if(lecturer.equals("3")) out.println("selected"); %>>박교수</option>
    			<option value="4" <%if(lecturer.equals("4")) out.println("selected"); %>>우교수</option>
    			<option value="5" <%if(lecturer.equals("5")) out.println("selected"); %>>최교수</option>
    			<option value="6" <%if(lecturer.equals("6")) out.println("selected"); %>>임교수</option>
    		</select>
    	</td>
    </tr>
    <tr>
    	<td>요일</td>
    	<td>
    		<input type="radio" <%if(week.equals("1")) out.println("checked"); %> name="week" value="1">월
    		<input type="radio" <%if(week.equals("2")) out.println("checked"); %> name="week" value="2">화
    		<input type="radio" <%if(week.equals("3")) out.println("checked"); %> name="week" value="3">수
    		<input type="radio" <%if(week.equals("4")) out.println("checked"); %> name="week" value="4">목
    		<input type="radio" <%if(week.equals("5")) out.println("checked"); %> name="week" value="5">금
    	</td>
    </tr>
    <tr>
    	<td>시작시간</td>
    	<td><input type="text" name="start_hour" value="<%=rs.getString(6) %>" style="width: 96%"></td>
    </tr>
    <tr>
    	<td>종료시간</td>
    	<td><input type="text" name="end_hour" value="<%=rs.getString(7)  %>" style="width: 96%"></td>
    </tr>
    <tr>
    	<td colspan="2">
    		<input type="button" value="수정" onclick="modify()">
    		<input type="button" value="목록" onclick="esc()">
    	</td>
    </tr>
    <%
    	} catch(Exception e) {
    		e.printStackTrace();
    	}
    %>
    </table>
    </form>
    </section>
    
    <jsp:include page="footer.jsp" />
    </body>
    </html>

    action.jsp

    <%@page import="java.sql.ResultSet"%>
    <%@page import="java.sql.PreparedStatement"%>
    <%@page import="DBPKG.Util"%>
    <%@page import="java.sql.Connection"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%
    request.setCharacterEncoding("UTF-8");
    String mode = request.getParameter("mode");
    
    String id = request.getParameter("id");
    String name = request.getParameter("name");
    String credit = request.getParameter("credit");
    String lecturer = request.getParameter("lecturer");
    String week = request.getParameter("week");
    String start_hour = request.getParameter("start_hour");
    String end_hour = request.getParameter("end_hour");
    
    try {
    	Connection c = Util.getConnection();
    	String sql = "";
    	PreparedStatement pstmt = null;
    	
    	switch(mode) {
    	case "insert":
    		sql = "INSERT INTO COURSE_TBL VALUES(?, ?, ?, ?, ?, ?, ?)";
    		pstmt = c.prepareStatement(sql);
    		pstmt.setString(1, id);
    		pstmt.setString(2, name);
    		pstmt.setString(3, credit);
    		pstmt.setString(4, lecturer);
    		pstmt.setString(5, week);
    		pstmt.setString(6, start_hour);
    		pstmt.setString(7, end_hour);
    		pstmt.executeUpdate();
    %>
    		<jsp:forward page="insert.jsp" />
    <%
    		break;
    	case "modify":
    		sql = "UPDATE COURSE_TBL " + 
    				"SET name=?, credit=?, lecturer=?, week=?, start_hour=?, end_hour=? " +
    				"WHERE id=?";
    		pstmt = c.prepareStatement(sql);
    		pstmt.setString(1, name);
    		pstmt.setString(2, credit);
    		pstmt.setString(3, lecturer);
    		pstmt.setString(4, week);
    		pstmt.setString(5, start_hour);
    		pstmt.setString(6, end_hour);
    		pstmt.setString(7, id);
    		pstmt.executeUpdate();
    %>
    		<jsp:forward page="modify.jsp" />
    <%
    		break;
    	case "delete":
    		sql = "DELETE FROM COURSE_TBL " +
    				"WHERE ID = ?";
    		pstmt = c.prepareStatement(sql);
    		pstmt.setString(1, id);
    		pstmt.executeUpdate();
    %>
    		<jsp:forward page="list.jsp" />
    <%
    		break;
    	}
    } catch(Exception e) {
    	e.printStackTrace();
    }
    %>

     

    출저: https://www.youtube.com/@channel_ej/videos

    댓글