실습(JDBC)_[Web_JDBC] 01 이어서
※ TBL_USER 테이블 참고
UserDAO.java
0. 필요한 변수 선언
// 로그인 성공 시 로그인된 회원의 번호를 저장할 변수
public static int userNumber;
public static final int GOODBYE_ACCOUNT = -1;
public static final int LOGIN_SUCCESS = 1;
public static final int LOGIN_FAILURE = 0;
// 연결 객체
Connection conn;
// SQL 쿼리문을 실행 해주는 객체
PreparedStatement pstm;
// 결과 테이블에 대한 정보를 가져올 수 있도록 도와주는 객체
ResultSet rs;
1. 로그인
public int login(String userId, String userPw) {
String query = "SELECT USER_NUMBER, STATUS FROM TBL_USER WHERE USER_ID = ? AND USER_PW = ?";
userNumber = 0; // 로그아웃 상태(0이 아니면 로그인 상태)
int check = LOGIN_FAILURE;
try {
conn = DBConnection.getConnection();
pstm = conn.prepareStatement(query);
pstm.setString(1, userId);
pstm.setString(2, userPw);
// executeQuery() : SELECT : 결과 테이블(ResultSet) 리턴
// executeUpdate() : INSERT, UPDATE, DELETE : 실행 건수 리턴
rs = pstm.executeQuery();
if(rs.next()) { // 사용자가 입력한 아이디와 비밀번호가 DB에서 조회된다면
userNumber = rs.getInt(1); // 로그인 된 회원의 번호를 userNumber에 담는다.
check = rs.getInt(2) == 0 ? LOGIN_SUCCESS : GOODBYE_ACCOUNT; // rs.getInt(2) : STATUS
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(rs != null) {
rs.close();
}
if(pstm != null) {
pstm.close();
}
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return check;
}
2. 아이디 찾기
(1) 핸드폰 번호로 랜덤한 인증번호 6자리 전송
public String sendSMS(String userPhoneNumber) {
String api_key = "";
String api_secret = "";
String code = "";
int length = 0;
Random r = new Random();
code += r.nextInt(1000000);
length = 6 - code.length();
for (int i = 0; i < length; i++) {
code = "0" + code;
}
Message coolsms = new Message(api_key, api_secret);
// 4 params(to, from, type, text) are mandatory. must be filled
HashMap<String, String> params = new HashMap<String, String>();
params.put("to", userPhoneNumber);
params.put("from", "01000000000");
params.put("type", "SMS");
params.put("text", "인증번호[" + code + "]를 입력해주세요.");
params.put("app_version", "test app 1.2"); // application name and version
try {
JSONObject obj = (JSONObject) coolsms.send(params);
System.out.println(obj.toString());
} catch (CoolsmsException e) {
System.out.println(e.getMessage());
System.out.println(e.getCode());
}
return code;
}
(2) 전송된 인증번호 검사 후 인증 성공 시 해당 아이디 출력
- 한 사람당 한 개
public String findId(String userPhoneNumber) {
String query = "SELECT USER_ID FROM TBL_USER WHERE USER_PHONE_NUMBER = ?";
String userId = null;
try {
conn = DBConnection.getConnection();
pstm = conn.prepareStatement(query);
pstm.setString(1, userPhoneNumber);
rs = pstm.executeQuery();
rs.next();
userId = rs.getString(1);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(rs != null) {
rs.close();
}
if(pstm != null) {
pstm.close();
}
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return userId;
}
- 한 사람당 여러 개(몇 개가 있을 지 알 수 없기 때문에 ArrayList를 받아온다.)
public ArrayList<String> findIdAll(String userPhoneNumber) {
String query = "SELECT USER_ID FROM TBL_USER WHERE USER_PHONE_NUMBER = ?";
ArrayList<String> ids = new ArrayList<>();
try {
conn = DBConnection.getConnection();
pstm = conn.prepareStatement(query);
pstm.setString(1, userPhoneNumber);
rs = pstm.executeQuery();
while(rs.next()) {
ids.add(rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(rs != null) {
rs.close();
}
if(pstm != null) {
pstm.close();
}
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return ids;
}
3. 비밀번호 찾기(새로운 비밀번호로 변경하기)
public void findPw(String userId, String userPw) {
String query = "UPDATE TBL_USER SET USER_PW = ? WHERE USER_ID = ?";
try {
conn = DBConnection.getConnection();
pstm = conn.prepareStatement(query);
pstm.setString(1, userPw);
pstm.setString(2, userId);
pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(pstm != null) {
pstm.close();
}
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
4. 회원 정보 수정(이름, 나이, 핸드폰번호, 비밀번호 수정)
public void updateInfo(UserVO user) {
String query = "UPDATE TBL_USER SET USER_NAME = ?, USER_AGE = ?, USER_PHONE_NUMBER = ?, USER_PW = ?";
query += "WHERE USER_NUMBER = ?";
try {
conn = DBConnection.getConnection();
pstm = conn.prepareStatement(query);
pstm.setString(1, user.getUserName());
pstm.setInt(2, user.getUserAge());
pstm.setString(3, user.getUserPhoneNumber());
pstm.setString(4, user.getUserPw());
pstm.setInt(5, user.getUserNumber());
pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(pstm != null) {
pstm.close();
}
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
5. 회원 탈퇴(회원 탈퇴 시 회원의 상태는 1로 변경된다.)
public void goodbyeUser() {
String query = "UPDATE TBL_USER SET STATUS = 1 WHERE = USER_NUMBER = ?";
try {
conn = DBConnection.getConnection();
pstm = conn.prepareStatement(query);
pstm.setInt(1, userNumber);
pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(pstm != null) {
pstm.close();
}
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
6. 회원 복구(로그인 시 탈퇴된 계정이라면 복구 버튼 출력, 복구 버튼 클릭 시 회원 status를 0으로 수정)
public void restore() {
String query = "UPDATE TBL_USER SET STATUS = 0 WHERE USER_NUMBER = ?";
try {
conn = DBConnection.getConnection();
pstm = conn.prepareStatement(query);
pstm.setInt(1, userNumber);
pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(pstm != null) {
pstm.close();
}
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
7. 핸드폰 번호 중복 검사
public boolean checkPhoneNumber(String userPhoneNumber) {
String query = "SELECT COUNT(USER_PHONE_NUMBER) FROM TBL_USER WHERE userPhoneNumber = ?";
boolean check = false;
try {
conn = DBConnection.getConnection();
pstm = conn.prepareStatement(query);
pstm.setString(1, userPhoneNumber);
rs = pstm.executeQuery();
rs.next();
check = rs.getInt(1) == 1;
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(rs != null) {
rs.close();
}
if(pstm != null) {
pstm.close();
}
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return check;
}
Test.java
1. 로그인
package test;
import dao.DBConnection;
import dao.UserDAO;
public class Test {
public static void main(String[] args) {
UserDAO dao = new UserDAO();
if(dao.login("hong1234", "1234") == 1) {
System.out.println("로그인 성공");
} else {
System.out.println("로그인 실패");
}
}
}
2. 비밀번호 찾기
package test;
import dao.DBConnection;
import dao.UserDAO;
public class Test {
public static void main(String[] args) {
UserDAO dao = new UserDAO();
dao.findPw("hong1234", "1234");
if(dao.login("hong1234", "1234") == 1) {
System.out.println("로그인 성공");
} else {
System.out.println("로그인 실패");
}
}
}
'웹 개발 > JDBC' 카테고리의 다른 글
[Web_JDBC] 01 (0) | 2022.04.11 |
---|