본문 바로가기
개발/JSP

[JSP] JSP 페이지에서 테이블 관련 [수정]

by 두리두리안 2021. 4. 6.

JDBC 다운은 이전 포스트를 참고 

duridan-program.tistory.com/25?category=1193590

 

Mysql JDBC

(먼저 Mysql를 설치 한 후에 진행 ) 1. JDBC 설치(1)  dev.mysql.com/downloads/ MySQL :: MySQL Community Downloads The world's most popular open source database dev.mysql.com Connector J 선택 하기 2...

duridan-program.tistory.com

구성도 

updateTestForm

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<title>레코드 수정</title>
</head>
<body>
  <h2>member테이블의 레코드 수정</h2>
    
  <form method="post" action="updateTestPro.jsp">
      아이디: <input type="text" name="id" maxlength="50"><br>
      패스워드: <input type="password" name="passwd" maxlength="16"><br>
      변경할 이름: <input type="text" name="name" maxlength="10"><br>
      <input type="submit" value="입력완료">
  </form>
</body>
</html>

updateTestPro

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ page import="java.sql.*"%>

<%  request.setCharacterEncoding("utf-8"); %>

<%
  String id= request.getParameter("id");
  String passwd= request.getParameter("passwd");
  String name= request.getParameter("name");

  Connection conn=null;
  PreparedStatement pstmt=null;
  ResultSet rs=null;

  try{
	String jdbcUrl="jdbc:mysql://localhost:3306/jspmysql";
    String dbId="root";
    String dbPass="00000000";
	 
	Class.forName("com.mysql.jdbc.Driver");
	conn=DriverManager.getConnection(jdbcUrl,dbId ,dbPass );
	
	String sql= "select id, passwd from member where id= ?";
	pstmt=conn.prepareStatement(sql);
    pstmt.setString(1,id);
	rs=pstmt.executeQuery();
    
	if(rs.next()){ 
		String rId=rs.getString("id");
		String rPasswd=rs.getString("passwd");
      if(id.equals(rId) && passwd.equals(rPasswd)){
	     sql= "update member set name= ?  where id= ? ";
	     pstmt=conn.prepareStatement(sql);
	     pstmt.setString(1,name);
	     pstmt.setString(2,id);
	     pstmt.executeUpdate();	   
%>
<html>
<head>
<title>레코드 수정</title>
</head>
<body>
  member 테이블의 레코드를 수정했습니다.
</body>
</html>
<%
      }else
		out.println("패스워드가 틀렸습니다.");
	}else
		 out.println("아이디가 틀렸습니다.");
  }catch(Exception e){ 
	  e.printStackTrace();
  }finally{
	  if(rs != null) 
		 try{rs.close();}catch(SQLException sqle){}
	  if(pstmt != null) 
		 try{pstmt.close();}catch(SQLException sqle){}
	  if(conn != null) 
		 try{conn.close();}catch(SQLException sqle){}
  }
%>