오늘은 작성하다가 리팩토링 부분이 보여서 리팩토링도 미약하게나마 했습니다.
package web.unstop.login;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import web.unstop.util.connutil;
public class loginDAO {
private static loginDAO instance = null;
private loginDAO() {
}
public static loginDAO getinstance() {
if (instance == null) {
synchronized (loginDAO.class) {
instance = new loginDAO();
}
}
return instance;
}
public int login(loginVO info) {
ResultSet rs = null;
int result = -1;
try {
loginVO che= selectpass(info.getId());
if (che!=null) {
if (che.getPass().equals(info.getPass())) {
result = 2;
} else {
result = 1;
}
} else {
result = 0;
}
} finally {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
}
}
return result;
}
public int creuser(loginVO info) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int result = 0;
try {
conn = connutil.getConnection();
String sql = "insert into bloglogin values(?,?)";
int check = login(info);
if (check == 0) {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, info.getId());
pstmt.setString(2, info.getPass());
pstmt.executeUpdate();
result = 1;
} else {
result = 2;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (SQLException e) {
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
}
}
return result;
}
public loginVO selectpass(String id) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = connutil.getConnection();
pstmt = conn.prepareStatement("select pass from bloglogin where id=?");
pstmt.setString(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
loginVO info=new loginVO();
info.setPass(rs.getString(1));
return info;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (SQLException e) {
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
}
}
return null;
}
public int deleteUser(String id, String pass) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int result = -1;
try {
loginVO che = selectpass(id);
if (che!=null) {
if (che.getPass().equals(pass)) {
conn = connutil.getConnection();
pstmt = conn.prepareStatement("delete from bloglogin where id=?");
pstmt.setString(1, id);
pstmt.executeUpdate();
result = 1;
}else{
result=0;
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (SQLException e) {
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
}
}
return result;
}
public int updateUser(loginVO info,String newpass) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int result = 0;
try {
loginVO che=selectpass(info.getId());
if (che!=null) {
if (che.getPass().equals(info.getPass())) {
conn = connutil.getConnection();
pstmt = conn.prepareStatement("update bloglogin set pass=? where id=?");
pstmt.setString(1, newpass);
pstmt.setString(2, info.getId());
pstmt.executeUpdate();
result = 1;
}else {
result = 2;
// 비번 틀림
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (SQLException e) {
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
}
}
return result;
}
}
loginDAO부분을 리팩토링 과 수정 삭제부분을 구현하였습니다. 우선 비밀 번호 검색하는 부분이 많아서 따로 함수로 만들어주고 호출해서 쓰게 했습니다. 처음에는 ResultSet을 return했는데 이거는 매번 close해줘야되고 jsp에서 호출시 문제가 될거 같아서 loginVO로 바꺼주었습니다. 만약 정보가 다른 다양한것들이 있다면 여기서 VO에 데이터를 더 넣어주거나 아니면 따로 한개 더만드는 다양한 방법이 있습니다. 코드 작성할때 조금씩 생각해보면 좋습니다.
public loginVO selectpass(String id) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = connutil.getConnection();
pstmt = conn.prepareStatement("select pass from bloglogin where id=?");
pstmt.setString(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
loginVO info=new loginVO();
info.setPass(rs.getString(1));
return info;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (SQLException e) {
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
}
}
return null;
}
이부분을 만들어서 close안해도 되는 함수도 만들었고 생각보다 줄어들었고 오히려 늘었다는 느낌이 들수는 있지만 길수록 효과가 좋을 것 입니다.
package web.unstop.login;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import web.unstop.util.connutil;
public class loginDAO {
private static loginDAO instance = null;
private loginDAO() {
}
public static loginDAO getinstance() {
if (instance == null) {
synchronized (loginDAO.class) {
instance = new loginDAO();
}
}
return instance;
}
public int login(loginVO info) {
ResultSet rs = null;
int result = -1;
try {
loginVO che= selectpass(info.getId());
if (che!=null) {
if (che.getPass().equals(info.getPass())) {
//로그인성공
result = 2;
} else {
//비밀번호 틀림
result = 1;
}
} else {
//아이디가 없음
result = 0;
}
} finally {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
}
}
return result;
}
public int creuser(loginVO info) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int result = 0;
try {
conn = connutil.getConnection();
String sql = "insert into bloglogin values(?,?)";
int check = login(info);
if (check == 0) {
//생성성공
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, info.getId());
pstmt.setString(2, info.getPass());
pstmt.executeUpdate();
result = 1;
} else {
//아이디 중복
result = 2;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (SQLException e) {
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
}
}
return result;
}
public loginVO selectpass(String id) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = connutil.getConnection();
pstmt = conn.prepareStatement("select pass from bloglogin where id=?");
pstmt.setString(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
//존재함
loginVO info=new loginVO();
info.setPass(rs.getString(1));
return info;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (SQLException e) {
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
}
}
return null;
}
public int deleteUser(String id, String pass) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int result = -1;
try {
loginVO che = selectpass(id);
if (che!=null) {
if (che.getPass().equals(pass)) {
//비번 일치
conn = connutil.getConnection();
pstmt = conn.prepareStatement("delete from bloglogin where id=?");
pstmt.setString(1, id);
pstmt.executeUpdate();
result = 1;
}else{
//비번틀림
result=0;
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (SQLException e) {
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
}
}
return result;
}
public int updateUser(loginVO info,String newpass) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int result = 0;
try {
loginVO che=selectpass(info.getId());
if (che!=null) {
if (che.getPass().equals(info.getPass())) {
conn = connutil.getConnection();
pstmt = conn.prepareStatement("update bloglogin set pass=? where id=?");
pstmt.setString(1, newpass);
pstmt.setString(2, info.getId());
pstmt.executeUpdate();
result = 1;
}else {
result = 2;
// 비번 틀림
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (SQLException e) {
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
}
}
return result;
}
}
이렇게 주석으로 비번 틀린 결과 값이라고 적어주면 정말 좋습니다.
로그인 페이지에 윗부분에 로그인되있으면 마이페이지로 이동하도록 설정 해놨습니다.
그리고 마이페이지 만들고 삭제 수정 로그아웃 이동 하게 버튼 만들고 세션에서 id 얻어오고 출력하게 해놨습니다.
그리고 모든페이지에 로그인 되어있지 않으면 로그인 페이지 가도록 설정했습니다.
<%
if(session.getAttribute("id")==null){%>
<script type="text/javascript">
alert("로그인 되어있지않습니다.");
window.location="login.jsp"
</script>
<%
}else{
이걸 활요해서 로그인 체크를 해서 그에 맞는 처리를 해줍니다.
로그아웃 페이지도 로그인 체크를 해주고 세션을 invalidate하여 로그아웃하는 기능을 구현했습니다.
비밀번호만 확인하고 삭제하는 페이지입니다.
삭제성공하면 로그아웃 되게 하고 안됬을시 이전페이지로 가게 하고 각 각 알려주도록 했습니다.
원래는 비밀번호 말고 다른 정보를 받아와서 보여줘야되지만 지금은 비밀번호만 잇어서 데이터베이스에서 데이터 받아와
서 페이지에 미리 보여주는 것을 구현해줬습니다. 현재 사실상 비밀번호 바꾸는 기능 입니다. 다른거 더있어도 큰차이는 없으니 구현에 문제는 없을 것입니다.
수정을 하고 안됬으면 안되는 이유를 알려주고 수정했으면 로그인 페이지로 보내고 로그인을 취소시켰습니다.
'JSP' 카테고리의 다른 글
JSP 게시판 수정 삭제 상세 (0) | 2022.07.13 |
---|---|
JSP게시판 작성 및 생성 (0) | 2022.07.01 |
JSP로그인과 회원가입 (0) | 2022.06.28 |
jsp 핵심 요약 (0) | 2022.06.27 |