728x90
3단계 jsp만 update처리
update 처리 작성과정
1. m_list.jsp에서 수정버튼 작성
m_list.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import = "java.sql.DriverManager" %> <%@ page import = "java.sql.Connection" %> <%@ page import = "java.sql.PreparedStatement" %> <%@ page import = "java.sql.ResultSet" %> <%@ page import = "java.sql.SQLException" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>회원 리스트</title> </head> <body> <%= request.getRequestURI() %> <br> 회원 리스트 <br> <table width="100%" border="1"> <tr> <td>이름</td> <td>아이디</td> <td>권한</td> <td>이름</td> <td>이메일</td> <td>수정</td><!-- **수정버튼작성 --> </tr> <% Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; // ResultSet클래스형 데이터 타입으로 객체참조변수 rs를 선언하고 null로 초기화 //JDBC 프로그램 순서 //01단계 :드라이버 로딩 시작 Class.forName("com.mysql.jdbc.Driver"); //01단계 :드라이버 로딩 끝 try{ //02단계 :DB연결(Connection)시작 String jdbcDriver = "jdbc:mysql://localhost:3306/doublesdb?" + "useUnicode=true&characterEncoding=euckr"; String dbUser = "doublesid"; String dbPass = "doublespw"; conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass); //02단계 :DB연결(Connection)끝 System.out.println(conn + "<-- conn m_list.jsp"); if(conn != null){ out.println("01 DB연결 성공"); }else{ out.println("02 DB연결 실패"); } //03단계 :Query실행을 위한 statement 또는 prepareStatement객체생성 시작 pstmt = conn.prepareStatement("select * from tb_member"); //04단계 :Query실행 시작 rs = pstmt.executeQuery(); System.out.println(rs + "<-- rs m_list.jsp"); //System.out.println(rs.next() + "<-- rs.next() m_list.jsp"); //System.out.println(rs.next() + "<-- rs.next() m_list.jsp"); //04단계 :Query실행 끝 //05단계 :Query실행결과 사용 //System.out.println(rs.next() + "<-- rs.next() m_list.jsp"); //select문장 통해서 모든 회원 목록 가져와서 한줄씩 //(레코드(record) or 로우(row))보여준다 시작 while(rs.next()){ %> <tr> <td><%= rs.getString("m_id")%></td> <td><%= rs.getString("m_pw")%></td> <td><%= rs.getString("m_level")%></td> <td><%= rs.getString("m_name")%></td> <td><%= rs.getString("m_email")%></td> <td> <a href="<%= request.getContextPath() %> /mupdate/m_update_form.jsp?send_id=<%= rs.getString("m_id")%>">수정클릭</a> <!-- **수정버튼작성 --> </td> </tr> <% } //select문장 통해서 모든 회원 목록 가져와서 한줄씩 //(레코드(record) or 로우(row))보여준다 끝 } catch(SQLException ex) { out.println(ex.getMessage()); ex.printStackTrace(); } finally { // 6. 사용한 Statement 종료 if (rs != null) try { rs.close(); } catch(SQLException ex) {} if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {} // 7. 커넥션 종료 if (conn != null) try { conn.close(); } catch(SQLException ex) {} } %> </table> </body> </html> | cs |
*HttpServletRequest의 URL 정보 추출 메서드
http://localhost:8080/test/index.jsp
위와 같은 주소가 있다고 가정한다면 URL정보 추출 메서드가 리턴하는 값은 아래와 같다.
getContextPath()
웹 애플리케이션의 경로
리턴값 : /test
getRequestURL()
요청 URL리턴(단, 매개변수 제외)
리턴값 : http://localhost:8080/test/index.jsp
getRequestURI()
서버 주소를 제외한 URL
리턴값 : /test/index.jsp
getServletPath()
서블릿 경로
리턴값 : /index.jsp
2. 수정버튼 클릭시 수정화면으로 이동(해당 id값을 넘기고 콘솔에 확인), 테스트작업
m_update_form.jsp
회원 리스트로부터 하나의 회원 id값이 넘어오는지 확인
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>회원정보수정 화면</title> </head> <body> <% String send_id = request.getParameter("send_id"); System.out.println(send_id + "<-- send_id m_update_form.jsp"); %> </body> </html> | cs |
m_list.jsp화면에서 수정버튼 클릭
해당 id값 확인
3. 수정화면 작성
m_update_form.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import = "java.sql.DriverManager" %> <%@ page import = "java.sql.Connection" %> <%@ page import = "java.sql.PreparedStatement" %> <%@ page import = "java.sql.ResultSet" %> <%@ page import = "java.sql.SQLException" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>회원 수정 입력 폼</title> </head> <body> <% String send_id = request.getParameter("send_id"); System.out.println(send_id + "<-- send_id m_update_form.jsp"); Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; Class.forName("com.mysql.jdbc.Driver"); String dbid = null; String dbpw = null; String dblevel = null; String dbname = null; String dbemail = null; try{ String jdbcDriver = "jdbc:mysql://localhost:3306/doublesdb?" + "useUnicode=true&characterEncoding=euckr"; String dbUser = "doublesid"; String dbPass = "doublespw"; conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass); pstmt = conn.prepareStatement("select * from tb_member where m_id=?"); pstmt.setString(1, send_id); rs = pstmt.executeQuery(); if(rs.next()){ dbid = rs.getString("m_id"); dbpw = rs.getString("m_pw"); dblevel = rs.getString("m_level"); dbname = rs.getString("m_name"); dbemail = rs.getString("m_email"); //콘솔창에 출력한다 } } catch(SQLException ex) { out.println(ex.getMessage()); ex.printStackTrace(); } finally { // 6. 사용한 Statement 종료 if (rs != null) try { rs.close(); } catch(SQLException ex) {} if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {} // 7. 커넥션 종료 if (conn != null) try { conn.close(); } catch(SQLException ex) {} } %> <form action="<%= request.getContextPath() %>/minsert/m_insert_pro.jsp" method="post"> <table border="1"> <tr> <td>아이디</td> <td> <input type="hidden" name="m_id" size="20" value="<%= dbid %>"> <%= dbid %> <!-- 아이디를 수정할 수 없도록 hidden 태그 사용 --> <%-- <input type="text" name="m_id" size="20" value="<%= dbid %>" readonly> readonly 태그도 사용가능 --%> </td> <tr> <tr> <td>암호</td> <td><input type="text" name="m_pw" size="20" value="<%= dbpw %>"></td> <tr> <tr> <td>권한</td> <td><input type="text" name="m_level" size="20" value="<%= dblevel %>"></td> <tr> <tr> <td>이름</td> <td><input type="text" name="m_name" size="20" value="<%= dbname %>"></td> <tr> <tr> <td>이메일</td> <td><input type="text" name="m_email" size="20" value="<%= dbemail %>"></td> <tr> <tr> <td colspan="4"><input type="submit" value="회원수정버튼"></td> </tr> </table> </form> </body> </html> | cs |
회원정보수정화면
회원정보 수정시 id는 수정이 불가하도록 hidden 태그를 사용하고, id값만 출력해주거나 속성을 readonly로 처리
4. 수정처리 작성
m_update_pro.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import = "java.sql.DriverManager" %> <%@ page import = "java.sql.Connection" %> <%@ page import = "java.sql.PreparedStatement" %> <%@ page import = "java.sql.SQLException" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>회원수정처리</title> </head> <body> <% request.setCharacterEncoding("utf-8"); Connection conn = null; PreparedStatement pstmt = null; //화면에서 입력자료 받아서 콘솔창에 출력 시작 String m_id = request.getParameter("m_id"); String m_pw = request.getParameter("m_pw"); String m_level = request.getParameter("m_level"); String m_name = request.getParameter("m_name"); String m_email = request.getParameter("m_email"); System.out.println(m_id + "<-- m_id /mupdate/m_update_pro.jsp"); System.out.println(m_pw + "<-- m_pw /mupdate/m_update_pro.jsp"); System.out.println(m_level + "<-- m_level /mupdate/m_update_pro.jsp"); System.out.println(m_name + "<-- m_name /mupdate/m_update_pro.jsp"); System.out.println(m_email + "<-- m_email /mupdate/m_update_pro.jsp"); Class.forName("com.mysql.jdbc.Driver"); //01단계 :드라이버 로딩 끝 try{ //02단계 :DB연결(Connection)시작 String jdbcDriver = "jdbc:mysql://localhost:3306/doublesdb?" + "useUnicode=true&characterEncoding=euckr"; String dbUser = "doublesid"; String dbPass = "doublespw"; conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass); pstmt = conn.prepareStatement( "UPDATE tb_member SET m_pw=?,m_level=?,m_name=?,m_email=? WHERE m_id=?"); System.out.println(pstmt + "<-- pstmt 1"); System.out.println(pstmt.getClass() + "<-- pstmt.getClass() 1"); pstmt.setString(1, m_pw); pstmt.setString(2, m_level); pstmt.setString(3, m_name); pstmt.setString(4, m_email); pstmt.setString(5, m_id); System.out.println(pstmt + "<-- pstmt 2"); pstmt.executeUpdate(); }finally{ if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {} if (conn != null) try { conn.close(); } catch(SQLException ex) {} } %> </body> </html> | cs |
회원정보 수정화면
회원정보 수정처리후 콘솔화면
회원정보 수정처리후 리스트화면