1. DTO 생성
package com.score; public class ScoreDTO {
private String hak; private String name; private int kor; private int eng; private int mat;
private int tot; private int ave; private int rank;
//getter, setter 생략 } |
2. DAO 생성
package com.score; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; public class ScoreDAO { private Connection conn;
//의존성주입 //구성요소간의 종속성을 소스코드에서 설정하지 않고 외부의 설정파일 등을 통해 주입하는 디자인 패턴 public ScoreDAO(Connection conn){ this.conn = conn; }
//입력 public int insertData(ScoreDTO dto){ int result = 0; PreparedStatement pstmt = null; String sql = "";
try {
sql = "insert into score (hak,name,kor,eng,mat) "; sql += "values (?,?,?,?,?) ";
pstmt = conn.prepareStatement(sql); pstmt.setString(1, dto.getHak()); pstmt.setString(2, dto.getName()); pstmt.setInt(3, dto.getKor()); pstmt.setInt(4, dto.getEng()); pstmt.setInt(5, dto.getMat()); result = pstmt.executeUpdate(); pstmt.close();
} catch (Exception e) { System.out.println(e.toString()); } return result; }
//전체조회 public List<ScoreDTO> getList(){
List<ScoreDTO> lists = new ArrayList<ScoreDTO>();
PreparedStatement pstmt = null; ResultSet rs = null; String sql;
try { sql = "select hak, name, kor, eng, mat, "; sql += "(kor+eng+mat) tot, (kor+eng+mat)/3 ave, "; sql += "rank() over(order by (kor+eng+mat) desc) rank "; sql += "from score"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery();
while(rs.next()){ ScoreDTO dto = new ScoreDTO(); dto.setHak(rs.getString("hak")); dto.setName(rs.getString("name")); dto.setKor(rs.getInt("kor")); dto.setEng(rs.getInt("eng")); dto.setMat(rs.getInt("mat")); dto.setTot(rs.getInt("tot")); dto.setAve(rs.getInt("ave")); dto.setRank(rs.getInt("rank")); lists.add(dto); } pstmt.close(); rs.close();
} catch (Exception e) { System.out.println(e.toString()); }
return lists; }
//학번조회 public ScoreDTO getReadData(String hak){
ScoreDTO dto = null; PreparedStatement pstmt = null; ResultSet rs = null; String sql;
try { sql = "select hak, name, kor, eng, mat from score where hak=? "; pstmt = conn.prepareStatement(sql); pstmt.setString(1, hak); rs = pstmt.executeQuery();
if(rs.next()){ dto = new ScoreDTO(); dto.setHak(rs.getString("hak")); dto.setName(rs.getString("name")); dto.setKor(rs.getInt("kor")); dto.setEng(rs.getInt("eng")); dto.setMat(rs.getInt("mat")); }
rs.close(); pstmt.close();
} catch (Exception e) { System.out.println(e.toString()); }
return dto; }
//수정 public int updateData(ScoreDTO dto){
int result = 0; PreparedStatement pstmt=null; String sql; try {
sql = "update score set kor=?, eng=?, mat=? "; sql += "where hak=?"; pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, dto.getKor()); pstmt.setInt(2, dto.getEng()); pstmt.setInt(3, dto.getMat()); pstmt.setString(4, dto.getHak());
result = pstmt.executeUpdate(); pstmt.close();
} catch (Exception e) { System.out.println(e.toString()); } return result;
} //삭제 public int deleteData(String hak){
int result = 0; PreparedStatement pstmt = null; String sql; try { sql = "delete score where hak=? "; pstmt = conn.prepareStatement(sql); pstmt.setString(1, hak); result = pstmt.executeUpdate(); pstmt.close(); } catch (Exception e) { System.out.println(e.toString()); } return result; } } |
3. 성적입력 JSP페이지
write.jsp <%@ page contentType="text/html; charset=UTF-8"%> <% request.setCharacterEncoding("UTF-8"); String cp = request.getContextPath();//특정 폴더를 쉽게 지칭하기 위한 시스템변수 개념으로 사용 //Context가 나오면 프로젝트안에서 전체적으로 모두 사용할 수 있음 //http://192.168.16.16:8080/study %> <!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 sendIt(){
var f = document.myForm; //f.action ="http://ip/study/score/write_ok.jsp"; //여기서의 study D:\java\work\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina \localhost\study\org\apache\jsp f.action ="<%=cp %>/score/write_ok.jsp" f.submit(); } </script> <style type="text/css"> *{ padding:0px; margin:0px; } body { font-size : 10pt; } td { font-size : 10pt; } .txtField{ font-size : 10pt; border: 1px solid; } .btn{ font-size : 10pt; background: #e6e6e6; } </style> </head> <body> <br/><br/> <table width="500" cellpadding="0" cellspacing="3" align="center" bgcolor="#e4e4e4"> <tr height="50"> <td bgcolor="#ffffff" style="padding-left:10px;"> <b>성적처리 입력화면</b> </td> </tr> </table> <br/> <form action="" method="post" name="myForm"> <table width="500" cellpadding="0" cellspacing="0" align="center"> <tr height="2"><td colspan="2" bgcolor="#cccccc"></td></tr> <tr height="30"> <td align="center" width="100" bgcolor="#e6e6e6">학번</td> <td style="padding-left: 5px;"> <input type="text" name="hak" size="10" maxlength="7" class="txtField"/> </td> </tr> <tr height="1"><td colspan="2" bgcolor="#cccccc"></td></tr> <tr height="30"> <td align="center" width="100" bgcolor="#e6e6e6">이름</td> <td style="padding-left: 5px;"> <input type="text" name="name" size="20" maxlength="10" class="txtField"/> </td> </tr> <tr height="1"><td colspan="2" bgcolor="#cccccc"></td></tr> <tr height="30"> <td align="center" width="100" bgcolor="#e6e6e6">국어</td> <td style="padding-left: 5px;"> <input type="text" name="kor" size="20" maxlength="3" class="txtField"/> </td> </tr> <tr height="1"><td colspan="2" bgcolor="#cccccc"></td></tr> <tr height="30"> <td align="center" width="100" bgcolor="#e6e6e6">영어</td> <td style="padding-left: 5px;"> <input type="text" name="eng" size="20" maxlength="3" class="txtField"/> </td> </tr> <tr height="1"><td colspan="2" bgcolor="#cccccc"></td></tr> <tr height="30"> <td align="center" width="100" bgcolor="#e6e6e6">수학</td> <td style="padding-left: 5px;"> <input type="text" name="mat" size="20" maxlength="3" class="txtField"/> </td> </tr> <tr height="2"><td colspan="2" bgcolor="#cccccc"></td></tr> <tr height="35" > <td align="center" colspan="2"> <input type="button" class="btn" value="입력완료" onclick="sendIt()"/> <input type="reset" class="btn" value="다시입력" onclick="document.myForm.hak.focus();"> <input type="button" class="btn" value="입력취소" onclick="javascript:location.href='<%=cp %>/score/list.jsp';"> </td> </tr> </table> </form> </body> </html> |
- 출력 페이지
4. 성적입력처리 JSP페이지
write_ok.jsp <%@page import="com.score.ScoreDAO"%> <%@page import="com.util.DBConn"%> <%@page import="java.sql.Connection"%> <%@ page contentType="text/html; charset=UTF-8"%> <% request.setCharacterEncoding("UTF-8"); %> <jsp:useBean id="dto" class="com.score.ScoreDTO" scope="page"></jsp:useBean> <jsp:setProperty property="*" name="dto"/> <% Connection conn = DBConn.getConnection(); ScoreDAO dao = new ScoreDAO(conn); int result = dao.insertData(dto);//반환값 1 이면 잘 등록됨 if(result!=0){ response.sendRedirect("list.jsp");//DB에 데이터 입력이 정상적으로 이뤄질경우 리스트.jsp로 이동 } %> <!-- 오류가 발생할 경우에 redirect가 실행되지 않아 입력오류가 웹페이지에 조회됨 --> 입력오류!! |
5. 성적수정입력 JSP페이지
update.jsp <%@page import="com.score.ScoreDTO"%> <%@page import="com.score.ScoreDAO"%> <%@page import="com.util.DBConn"%> <%@page import="java.sql.Connection"%> <%@ page contentType="text/html; charset=UTF-8"%> <% request.setCharacterEncoding("UTF-8"); String cp = request.getContextPath(); //http://192.168.16.16:8080/study
//get방식으로 넘어온 hak을 받아서 string으로 저장 String hak = request.getParameter("hak");
//Connection conn = DBConn.getConnection(); //ScoreDAO dao = new ScoreDAO(conn); ScoreDAO dao = new ScoreDAO(DBConn.getConnection()); ScoreDTO dto = dao.getReadData(hak); DBConn.close();
//수정버튼을 누름과 동시에 누군가 먼저 삭제를 진행했을 경우 null. if(dto==null){ response.sendRedirect("list.jsp"); } %> <!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 sendIt(){ var f = document.myForm; f.action ="<%=cp %>/score/update_ok.jsp" f.submit(); } </script> </head> <body> <br/><br/> <table width="500" cellpadding="0" cellspacing="3" align="center" bgcolor="#e4e4e4"> <tr height="50"> <td bgcolor="#ffffff" style="padding-left:10px;"> <b>성적처리 수정화면</b> </td> </tr> </table> <br/> <form action="" method="post" name="myForm"> <table width="500" cellpadding="0" cellspacing="0" align="center"> <tr height="2"><td colspan="2" bgcolor="#cccccc"></td></tr> <tr height="30"> <td align="center" width="100" bgcolor="#e6e6e6">학번</td> <td style="padding-left: 5px;"> <%=dto.getHak() %> <input type="hidden" value="<%=dto.getHak() %>" name="hak"> </td> </tr> <tr height="1"><td colspan="2" bgcolor="#cccccc"></td></tr> <tr height="30"> <td align="center" width="100" bgcolor="#e6e6e6">이름</td> <td style="padding-left: 5px;"> <%=dto.getName() %> <input type="hidden" value="<%=dto.getName() %>" name="name"> </td> </tr> <tr height="1"><td colspan="2" bgcolor="#cccccc"></td></tr> <tr height="30"> <td align="center" width="100" bgcolor="#e6e6e6">국어</td> <td style="padding-left: 5px;"> <input type="text" name="kor" value="<%=dto.getKor() %>"size="20" maxlength="3" class="txtField"/> </td> </tr> <tr height="1"><td colspan="2" bgcolor="#cccccc"></td></tr> <tr height="30"> <td align="center" width="100" bgcolor="#e6e6e6">영어</td> <td style="padding-left: 5px;"> <input type="text" name="eng" value="<%=dto.getEng() %>"size="20" maxlength="3" class="txtField"/> </td> </tr> <tr height="1"><td colspan="2" bgcolor="#cccccc"></td></tr> <tr height="30"> <td align="center" width="100" bgcolor="#e6e6e6">수학</td> <td style="padding-left: 5px;"> <input type="text" name="mat" value="<%=dto.getMat() %>" size="20" maxlength="3" class="txtField"/> </td> </tr> <tr height="2"><td colspan="2" bgcolor="#cccccc"></td></tr> <tr height="35" > <td align="center" colspan="2"> <input type="button" class="btn" value="수정완료" onclick="sendIt()"/> <input type="button" class="btn" value="수정취소" onclick="javascript:location.href='<%=cp %>/score/list.jsp';"> </td> </tr> </table> </form> </body> </html> |
- 출력 페이지
6. 성적수정처리 JSP페이지
update_ok.jsp <%@page import="com.score.ScoreDAO"%> <%@page import="com.util.DBConn"%> <%@page import="java.sql.Connection"%> <%@ page contentType="text/html; charset=UTF-8"%> <% request.setCharacterEncoding("UTF-8"); String cp = request.getContextPath(); %> <jsp:useBean id="dto" class="com.score.ScoreDTO" scope="page"></jsp:useBean> <jsp:setProperty property="*" name="dto"/> <% Connection conn = DBConn.getConnection(); ScoreDAO dao = new ScoreDAO(conn); int result = dao.updateData(dto); DBConn.close(); response.sendRedirect("list.jsp"); %> |
7. 성적삭제 JSP페이지
<%@page import="com.score.ScoreDAO"%> <%@page import="com.util.DBConn"%> <%@page import="java.sql.Connection"%> <%@ page contentType="text/html; charset=UTF-8"%> <% request.setCharacterEncoding("UTF-8"); String cp = request.getContextPath();
String hak = request.getParameter("hak"); Connection conn = DBConn.getConnection(); ScoreDAO dao = new ScoreDAO(conn); int result = dao.deleteData(hak); DBConn.close(); response.sendRedirect("list.jsp");
%> |
8. 성적 리스트 조회 JSP페이지
<%@page import="com.score.ScoreDTO"%> <%@page import="java.util.List"%> <%@page import="com.score.ScoreDAO"%> <%@page import="com.util.DBConn"%> <%@page import="java.sql.Connection"%> <%@ page contentType="text/html; charset=UTF-8"%> <% request.setCharacterEncoding("UTF-8"); String cp = request.getContextPath(); Connection conn = DBConn.getConnection(); ScoreDAO dao = new ScoreDAO(conn); List<ScoreDTO> lists = dao.getList(); DBConn.close(); %> <!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> <style type="text/css"> *{ padding:0px; margin:0px; } body { font-size : 10pt; } td { font-size : 10pt; } .btn{ font-size : 10pt; background: #e6e6e6; } </style> </head> <body> <br><br> <table width="700" cellpadding="0" cellspacing="3" align="center" bgcolor="#e4e4e4"> <tr height="50"> <td bgcolor="#ffffff" style="padding-left: 10px;'"> <b>성적처리 리스트 화면</b> </td> </tr> </table> <br> <table width="650" cellpadding="0" cellspacing="3" align="center"> <tr height="35"> <td align="right"> <input type="button" class="btn" value="글쓰기" onclick="javascript:location.href='<%=cp %>/score/write.jsp';"> </td> </tr> </table> <table width="650" cellpadding="0" cellspacing="1" align="center" bgcolor="#cccccc"> <tr height="30"> <td align="center" bgcolor="#e6e6e6" width="80">학번</td> <td align="center" bgcolor="#e6e6e6" width="80">이름</td> <td align="center" bgcolor="#e6e6e6" width="60">국어</td> <td align="center" bgcolor="#e6e6e6" width="60">영어</td> <td align="center" bgcolor="#e6e6e6" width="60">수학</td> <td align="center" bgcolor="#e6e6e6" width="60">총점</td> <td align="center" bgcolor="#e6e6e6" width="60">평균</td> <td align="center" bgcolor="#e6e6e6" width="60">석차</td> <td align="center" bgcolor="#e6e6e6" width="130">수정</td> </tr> <% for(ScoreDTO dto: lists){%> <tr height="30"> <td align="center" bgcolor="#ffffff"><%=dto.getHak() %></td> <td align="center" bgcolor="#ffffff"><%=dto.getName() %></td> <td align="center" bgcolor="#ffffff"><%=dto.getKor() %></td> <td align="center" bgcolor="#ffffff"><%=dto.getEng() %></td> <td align="center" bgcolor="#ffffff"><%=dto.getMat() %></td> <td align="center" bgcolor="#ffffff"><%=dto.getTot() %></td> <td align="center" bgcolor="#ffffff"><%=dto.getAve() %></td> <td align="center" bgcolor="#ffffff"><%=dto.getRank() %></td> <td align="center" bgcolor="#ffffff"> <a href="update.jsp?hak=<%=dto.getHak() %>">수정</a> <a href="delete_ok.jsp?hak=<%=dto.getHak() %>">삭제</a></td> </tr> <% } %> </table> </body> </html> |
- 출력 페이지
'Dev > JSP & Servlet' 카테고리의 다른 글
JSP 게시판(2) - DTO, DAO 생성 (0) | 2019.02.25 |
---|---|
JSP 게시판 (1) - 페이징, 리스트페이지 (0) | 2019.02.21 |
이클립스 플러그인 설치(퀀텀DB) (0) | 2019.02.19 |
JSP - 액션태그를 활용하여 페이지 만들기 (0) | 2019.02.19 |
JSP - 캐릭터 인코딩, 디코딩 (0) | 2019.02.19 |