<a href="deleteForm.jsp">회원정보삭제</a>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>members/deleteForm.jsp</title>
</head>
<body>
<h1>members/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>
<%@page import="member.MemberDTO"%>
<%@page import="member.MemberDAO"%>
<%@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>members/deletePro.jsp</title>
</head>
<body>
<h1>members/deletePro.jsp</h1>
<%
String id = request.getParameter("id");
String pass = request.getParameter("pass");
MemberDAO dao = new MemberDAO();
MemberDTO dto = dao.userCheck(id, pass);
if(dto!=null){
dao.deleteMember(id);
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>
public ArrayList<MemberDTO> getMemberList(){
ArrayList<MemberDTO> memberList = new ArrayList<>();
try {
Connection con = getConnection();
String sql = "select * from members";
PreparedStatement psmt = con.prepareStatement(sql);
ResultSet rs = psmt.executeQuery();
while(rs.next()) {
// 한명 정보 MemberDTO 저장 => 배열한칸 저장
// MemberDTO 객체생성
// set메서드 호출 <= 열접근
// 배열 한칸에 저장
MemberDTO dto = new MemberDTO();
System.out.println("회원 주소 : " +dto);
dto.setId(rs.getString("id"));
dto.setPass(rs.getString("pass"));
dto.setName(rs.getString("name"));
dto.setDate(rs.getTimestamp("date"));
memberList.add(dto);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
}
return memberList;
}
<%@page import="member.MemberDTO"%>
<%@page import="java.util.ArrayList"%>
<%@page import="member.MemberDAO"%>
<%@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>member/list.jsp</title>
</head>
<body>
<h1>member/list.jsp</h1>
<%
MemberDAO dao = new MemberDAO();
// 리턴할형 ArrayList<MemberDTO> getMemberList() 메서드
// ArrayList<MemberDTO> memberList = dao.getMemberList() 메서드 호출
ArrayList<MemberDTO> memberList = dao.getMemberList();
%>
<table border="1">
<tr><td>아이디</td><td>비밀번호</td><td>이름</td><td>가입날짜</td></tr>
<%
// 배열 접근 => for
for(int i=0;i<memberList.size();i++){
// 배열 하나의 내용 가져오기
MemberDTO dto=memberList.get(i);
//
%>
<tr><td><%=dto.getId()%></td>
<td><%=dto.getPass()%></td>
<td><%=dto.getName()%></td>
<td><%=dto.getDate()%></td></tr>
<%}%>
</table>
</body>
</html>
예)
public MemberDTO userCheck(String id, String pass) {
// 바구니 주소가 저장되는 변수 null 초기화
MemberDTO dto = null;
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null; // 변수를 공통으로 사용하기 위해 바깥으로 뺀다
try {
con = getConnection();
String sql = "select * from members where id = ? and pass = ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, pass);
rs = pstmt.executeQuery();
if(rs.next()){
// 바구니 객체 생성
dto = new MemberDTO();
dto.setId(rs.getString("id"));
dto.setPass(rs.getString("pass"));
dto.setName(rs.getString("name"));
dto.setDate(rs.getTimestamp("date"));
}
} catch (Exception e) {
e.printStackTrace();
} finally { // 예외 상관없이 마무리 작업 => 객체생성한 기억장소 해제
if(pstmt!=null)try {pstmt.close();} catch (Exception e2) {}
if(con!=null)try {con.close();} catch (Exception e2) {}
if(rs!=null)try {rs.close();} catch (Exception e2) {}
}
return dto;
}
모든 메서드에 같은 작업하면 됨
속도 향상, 디비연결 정보 수정 최소화
public Connection getConnection () throws Exception {
Context init = new InitialContext();
DataSource ds = (DataSource)init.lookup("java:comp/env/jdbc/MysqlDB");
Connection con = ds.getConnection();
return con;
}