Intellij JSP - MySQL 회원 관리 예제
MySQL 회원 관리 예제
-
Project - MySQL_ex
-
DatabaseUtil.java
import java.sql.Connection; import java.sql.DriverManager; public class DatabaseUtil { public static Connection getConnection() { try { String dbURL = "jdbc:mysql://localhost:3306/JSP"; String dbID = "root"; String dbPW = "1111"; Class.forName("com.mysql.jdbc.Driver"); return DriverManager.getConnection(dbURL, dbID, dbPW); } catch (Exception e) { e.printStackTrace(); } return null; } }
-
UserDao.java
import beans.UserDto; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class UserDao { public static int USER_EXISTENT = 1; public static int USER_NONEXISTENT = 2; public static int USER_JOIN_SUCCESS = 3; public static int USER_LOGIN_SUCCESS = 4; public static int USER_LOGIN_FAIL = 5; private static UserDao instance = new UserDao(); public static UserDao getInstance() { return instance; } private UserDao() { } public int insertUser(UserDto user) { int rt = 0; Connection conn = null; PreparedStatement pstmt = null; String query = "INSERT INTO USER VALUES (?,?,?,?,?,?)"; try { conn = DatabaseUtil.getConnection(); if (conn == null) return rt; pstmt = conn.prepareStatement(query); pstmt.setString(1, user.getId()); pstmt.setString(2, user.getPw()); pstmt.setString(3, user.getName()); pstmt.setString(4, user.getEmail()); pstmt.setString(5, user.getAddress()); pstmt.setTimestamp(6, user.getDate()); pstmt.executeUpdate(); rt = USER_JOIN_SUCCESS; } catch (SQLException e) { e.printStackTrace(); } finally { try { if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } return rt; } public int updateUser(UserDto user) { int rt = 0; Connection conn = null; PreparedStatement pstmt = null; String query = "UPDATE USER SET PW=?, EMAIL=?, ADDRESS=? WHERE ID=?"; try { conn = DatabaseUtil.getConnection(); if (conn == null) return rt; pstmt = conn.prepareStatement(query); pstmt.setString(1, user.getPw()); pstmt.setString(2, user.getEmail()); pstmt.setString(3, user.getAddress()); pstmt.setString(4, user.getId()); rt = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } return rt; } public int confirmId(String id) { int rt = 0; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; String query = "SELECT ID FROM USER WHERE ID = ?"; try { conn = DatabaseUtil.getConnection(); if (conn == null) return rt; pstmt = conn.prepareStatement(query); pstmt.setString(1, id); rs = pstmt.executeQuery(); if (rs.next()) rt = USER_EXISTENT; else rt = USER_NONEXISTENT; } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } return rt; } public int confirmLogin(String id, String pw) { int rt = 0; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; String query = "SELECT PW FROM USER WHERE ID = ?"; try { conn = DatabaseUtil.getConnection(); if (conn == null) return rt; pstmt = conn.prepareStatement(query); pstmt.setString(1, id); rs = pstmt.executeQuery(); if (rs.next()) { if (pw.equals(rs.getString("pw"))) rt = USER_LOGIN_SUCCESS; else rt = USER_LOGIN_FAIL; } else rt = USER_NONEXISTENT; } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } return rt; } public UserDto getUser(String id) { int rt = 0; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; UserDto user = null; String query = "SELECT * FROM USER WHERE ID = ?"; try { conn = DatabaseUtil.getConnection(); if (conn == null) return user; pstmt = conn.prepareStatement(query); pstmt.setString(1, id); rs = pstmt.executeQuery(); if (rs.next()) { user = new UserDto(rs.getString("id"), rs.getString("pw"), rs.getString("name"), rs.getString("email"), rs.getString("address"), rs.getTimestamp("date")); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } return user; } }
-
UserDto.java
import java.sql.Timestamp; public class UserDto { private String id; private String pw; private String name; private String email; private String address; private Timestamp date; public UserDto(String id, String pw, String name, String email, String address, Timestamp date) { this.id = id; this.pw = pw; this.name = name; this.email = email; this.address = address; this.date = date; } public UserDto() {} // getter, setter 생략 }
-
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <% if (session.getAttribute("isLogin") != null) { pageContext.forward("main.jsp"); } %> <html> <head> <script src="form.js"></script> <title>Login</title> </head> <body> <form action="login_process.jsp" method="post" name="login_form"> <table> <tr> <td> ID : </td> <td> <input type="text" name="id"></label><br/> </td> </tr> <tr> <td> Password </td> <td> <input type="password" name="pw"> </td> </tr> </table> <br/> <button type="button" onclick="confirmLogin()">로그인</button> <button type="button" onclick="location.href='join.html'">회원 가입</button> </form> </body> </html>
-
login_process.jsp
<%@ page import="beans.UserDto" %> <%@ page import="mysql.UserDao" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String id = request.getParameter("id"); String pw = request.getParameter("pw"); UserDao dao = UserDao.getInstance(); int rt = dao.confirmLogin(id, pw); if (rt == UserDao.USER_NONEXISTENT) { %> <script> alert("존재하지 않는 아이디 입니다.") history.back(); </script> <% } else if (rt == UserDao.USER_LOGIN_FAIL) { %> <script> alert("비밀번호가 틀립니다.") history.back(); </script> <% } else { UserDto user = dao.getUser(id); if (user == null) { %> <script> alert("존재하지 않는 회원 입니다.") history.back(); </script> <% } session.setAttribute("name", user.getName()); session.setAttribute("id", user.getId()); session.setAttribute("isLogin", true); response.sendRedirect("main.jsp"); } %>
-
join.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <script src="form.js"></script> <title>Join</title> <style> table { /*width: 100%;*/ border: 1px solid #444444; padding: 5px; margin-left: auto; margin-right: auto; } td { /*width: 100%;*/ /*border: 1px solid #444444;*/ } .c { text-align: center; } </style> </head> <body> <p class="c">회원 가입</p> <form action="join.jsp" method="post" name="join_form"> <table> <tr> <td>ID</td> <td><input type="text" name="id"></td> </tr> <tr> <td>Password</td> <td><input type="password" name="pw"></td> </tr> <tr> <td>이름</td> <td><input type="text" name="name"></td> </tr> <tr> <td>Email</td> <td><input type="email" name="email"></td> </tr> <tr> <td>Address</td> <td><input type="text" name="address"></td> </tr> </table> <br/> <div class="c"> <input type="button" value="가입" onclick="confirmJoin()"> <input type="reset" value="초기화"> </div> </form> </body> </html>
-
join.jsp
<%@ page import="mysql.UserDao" %> <%@ page import="java.sql.Timestamp" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <jsp:useBean id="user" class="beans.UserDto"/> <jsp:setProperty name="user" property="*"/> <% UserDao dao = UserDao.getInstance(); if (dao.confirmId(user.getId()) == UserDao.USER_EXISTENT) { %> <script> alert("이미 존재하는 아이디 입니다.") history.back(); </script> <% } else { user.setDate(new Timestamp(System.currentTimeMillis())); if (dao.insertUser(user) == UserDao.USER_JOIN_SUCCESS) { %> <script> alert("회원가입을 성공 했습니다."); location.href = 'login.jsp'; </script> <% }else{ %> <script> alert("회원가입을 실패 했습니다."); location.href = 'login.jsp'; </script> <% } } %>
-
main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Home</title> </head> <body> <h2><%= session.getAttribute("name")%>님 안녕하세요</h2><br/><br/> <hr> <button onclick="location.href='logout.jsp'">로그아웃</button> <button onclick="location.href='modify.jsp'">회원정보 수정</button> </body> </html>
-
modify.jsp
<%@ page import="beans.UserDto" %> <%@ page import="mysql.UserDao" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% if (session.getAttribute("isLogin") == null) { %> <script> alert("로그인이 필요합니다."); location.href = "login.jsp"; </script> <% } else { UserDto user = UserDao.getInstance().getUser((String) session.getAttribute("id")); %> <html> <head> <script src="form.js"></script> <title>Join</title> <style> table { /*width: 100%;*/ border: 1px solid #444444; padding: 5px; margin-left: auto; margin-right: auto; } td { /*width: 100%;*/ /*border: 1px solid #444444;*/ } .c { text-align: center; } </style> </head> <body> <p class="c">회원 정보 수정</p> <form action="modify_process.jsp" method="post" name="modify_form"> <table> <tr> <td>ID</td> <td><%= user.getId()%> </td> </tr> <tr> <td>Password</td> <td><input type="password" name="pw"></td> </tr> <tr> <td>이름</td> <td><%= user.getName()%> </td> </tr> <tr> <td>Email</td> <td><input type="email" name="email" value="<%= user.getEmail()%>"></td> </tr> <tr> <td>Address</td> <td><input type="text" name="address" value="<%= user.getAddress()%>"></td> </tr> </table> <br/> <div class="c"> <input type="button" value="수정" onclick="confirmModify()"> <input type="button" value="취소" onclick="location.hre='login.jsp'"> </div> </form> </body> </html> <% } %>
-
modify_process.jsp
<%@ page import="mysql.UserDao" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <jsp:useBean id="user" class="beans.UserDto"/> <jsp:setProperty name="user" property="*"/> <% UserDao dao = UserDao.getInstance(); user.setId(((String) session.getAttribute("id"))); if (dao.updateUser(user) == 1) { %> <script> alert("회원정보 수정 완료"); location.href = "login.jsp"; </script> <% } else { %> <script> alert("회원정보 수정 실패"); history.back(); </script> <% } %>
-
form.js
function confirmLogin() { if(document.login_form.id.value === ""){ alert("아이디를 입력하세요."); document.login_form.id.focus(); return; } if(document.login_form.pw.value === ""){ alert("비밀번호를 입력하세요."); document.login_form.pw.focus(); return; } document.login_form.submit(); } function confirmJoin() { if(document.join_form.id.value === ""){ alert("아이디를 입력하세요."); document.join_form.id.focus(); return; } if(document.join_form.pw.value === ""){ alert("비밀번호를 입력하세요."); document.join_form.pw.focus(); return; } if(document.join_form.name.value === ""){ alert("이름을 입력하세요."); document.join_form.name.focus(); return; } if(document.join_form.email.value === ""){ alert("이메일 입력하세요."); document.join_form.email.focus(); return; } if(document.join_form.address.value === ""){ alert("주소를 입력하세요."); document.join_form.address.focus(); return; } document.join_form.submit(); } function confirmModify() { if(document.modify_form.pw.value === ""){ alert("비밀번호를 입력하세요."); document.modify_form.pw.focus(); return; } if(document.modify_form.email.value === ""){ alert("이메일 입력하세요."); document.modify_form.email.focus(); return; } if(document.modify_form.address.value === ""){ alert("주소를 입력하세요."); document.modify_form.address.focus(); return; } document.modify_form.submit(); }
-
logout.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <% session.invalidate(); response.sendRedirect("login.jsp"); %>
-
로그인
-
회원 가입
-
회원 가입 성공
-
홈 화면
-
회원 정보 수정
-
회원 정보 수정 완료
-
회원 정보 수정 결과
-
-
신입SW인력을 위한 실전 JSP Servlet
Leave a comment