[23.01.16] 59일차 [프론트엔드]

W·2023년 2월 6일
0

국비

목록 보기
105/119

회원정보 삭제

<a href="deleteForm.jsp">회원정보삭제</a>
  • deleteForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp3/deleteForm.jsp</title>
</head>
<body>
<h1>jsp3/deleteForm.jsp</h1>
<%
String id = (String)session.getAttribute("id");

%>

<form action="deletePro.jsp" method="get">
아이디 : <input type="text" name="id" value="<%=id%>" readonly><br>
비밀번호 : <input type="password" name="pass"><br>

<input type="submit" value="삭제하기">

</form>
</body>
</html>
  • deletePro.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@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>jsp3/deletePro.jsp</title>
</head>
<body>
<h1>jsp3/deletePro.jsp</h1>
<%
String id = request.getParameter("id");
String pass = request.getParameter("pass");


Class.forName("com.mysql.cj.jdbc.Driver");

String dbUrl = "jdbc:mysql://localhost:3306/jspdb1";
String dbId = "root";
String dbPass = "1234";

Connection con = DriverManager.getConnection(dbUrl, dbId, dbPass);

String sql = "select * from members where id = ? and pass = ?";

PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, pass);

ResultSet rs = pstmt.executeQuery();

if(rs.next()){
String sql2 = "delete from members where id=?";
PreparedStatement pstmt2 = con.prepareStatement(sql2);
pstmt2.setString(1, id);
pstmt2.executeUpdate();
session.invalidate();
%>
<script type="text/javascript">
alert("삭제되었습니다.");
location.href="main.jsp";
</script>
<%
// response.sendRedirect("main.jsp");
} else {
%>	<script type="text/javascript">
	alert("아이디 비밀번호 틀림");
	history.back();
	</script>
<% } %>

</body>
</html>

삭제 후 로그인 화면으로 옮겨감

회원목록

<a href="list.jsp">회원목록</a>
  • list.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@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>jsp3/list.jsp</title>
</head>
<body>
<h1>jsp3/list.jsp</h1>

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

Class.forName("com.mysql.cj.jdbc.Driver");

String dbUrl="jdbc:mysql://localhost:3306/jspdb1";
String dbUser="root";
String dbPass="1234";

Connection con = DriverManager.getConnection(dbUrl, dbUser, dbPass);

String sql = "select * from members";

PreparedStatement psmt = con.prepareStatement(sql);

ResultSet rs = psmt.executeQuery();

%>
<table border="1">
<tr><td>아이디</td><td>비밀번호</td><td>이름</td><td>가입날짜</td></tr>
<%while(rs.next()){%>

<tr><td><%=rs.getString("id")%></td>
    <td><%=rs.getString("pass")%></td>
    <td><%=rs.getString("name")%></td>
    <td><%=rs.getTimestamp("date")%></td></tr>
<%}%>
</table>

</body>
</html>

id : admin만 회원정보 조회 가능하게 하기

  • main.jsp
<%
//로그인 한 회원
if(id!=null){
	// admin 인 경우 
	if(id.equals("admin")){
		%>
		<a href="list.jsp">회원목록</a><br>
		<%
	}
}
%>

메서드 이용하여 회원정보 DB연결하여 전달하기

  • member 폴더 생성
  • insertForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>member/insertFrom.jsp</title>
</head>
<body>
<h1>member/insertFrom.jsp</h1>

<form action="insertPro.jsp" method="get">
아이디 : <input type="text" name="id"><br>
비밀번호 : <input type="password" name="pass"><br>
이름 : <input type="text" name="name"><br>
<input type="submit" value="가입하기">
</form>
</body>
</html>
  • insertPro.jsp
<%@page import="member.MemberDAO"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.Timestamp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>member/insertPro.jsp</title>
</head>
<body>
<h1>member/insertPro.jsp</h1>

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

String id = request.getParameter("id");
String pass = request.getParameter("pass");
String name = request.getParameter("name");

// java.sql.Timestamp 자바내장객체 => 날짜
Timestamp date = new Timestamp(System.currentTimeMillis());

// 1~4단계 자바파일(MemberDAO:member 디비접근)에 (Java Resources = src/main/java)
// insertMember() 메서드 정의해서
// 자바파일 객체 생성하고 메서드 호출
MemberDAO dao = new MemberDAO();
System.out.println("MemberDAO 주소 : "+dao);
dao.insertMember(id,pass,name,date);


response.sendRedirect("loginForm.jsp");
%>

</body>
</html>
  • MemberDAO.jsp
package member;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Timestamp;

public class MemberDAO {
	
	// 1, 2 단계 디비연결 메서드
	// 예외처리를 메서드 호출한곳으로 뒤로 미루겠다
	public Connection getConnection () throws Exception {
	
	Class.forName("com.mysql.cj.jdbc.Driver");

	String dbUrl="jdbc:mysql://localhost:3306/jspdb1";
	String dbUser="root";
	String dbPass="1234";

	Connection con = DriverManager.getConnection(dbUrl, dbUser, dbPass);
	return con;
	}

	// insertMember() 메서드 정의해서
	public void insertMember(String id, String pass, String name, Timestamp date) {
		System.out.println("MemberDAO insertMember");
		System.out.println("폼에서 전달받은 아이디 : "+id );
		System.out.println("폼에서 전달받은 비밀번호 : "+pass );
		System.out.println("폼에서 전달받은 이름 : "+name );
		System.out.println("폼에서 전달받은 가입날짜 : "+date );
			
		try {
			// 예외가 발생할 가능성이 높은 명령(1~4단계)
			Connection con = getConnection();
			String sql = "insert into members(id, pass, name, date) values(?,?,?,?)";

			PreparedStatement pstmt=con.prepareStatement(sql);

			pstmt.setString(1,id);
			pstmt.setString(2,pass);
			pstmt.setString(3,name);
			pstmt.setTimestamp(4, date);

			pstmt.executeUpdate();
			
		} catch (Exception e) {
			// 예외가 발생하면 처리하는 곳
			e.printStackTrace();
		}finally {
			// 예외 상관없이 마무리 작업 
		}

	}// insertMember() 메서드

	
	
} // 클래스

0개의 댓글