1. 로그인, 로그아웃 기능 구현
1) MemberDao.login() : 로그인 기능
- MemberDao : login()
public Member login(String id, String pw) {
Member member = null;
try {
//클래스 로드
Class.forName("oracle.jdbc.driver.OracleDriver");
// connection 취득
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/xe",
"odoung", "1234");
// 문장 생성
String sql = "SELECT * FROM TBL_MEMBER\r\n" +
"WHERE ID = ? AND PW = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
int idx = 1;
pstmt.setString(idx++, id);
pstmt.setString(idx++, pw);
// 결과집합 생성
ResultSet rs = pstmt.executeQuery();
// 결과집합 순회 후 데이터 바인딩
while(rs.next()) {
int idx2 = 1;
member = new Member(rs.getString(idx2++), rs.getString(idx2++), rs.getString(idx2++));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return member;
}
- MemberService : login()
Member member; // null(비로그인), 주소가 있는 상태(로그인)
public void login() {
System.out.println("로그인");
Scanner scanner = new Scanner(System.in);
System.out.println("아이디 > ");
String id = scanner.nextLine();
System.out.println("비밀번호 > ");
String pw = scanner.nextLine();
member = new MemberDao().login(id, pw);
}
2) logout() : 로그아웃 기능
- MemberService : logout()
public void logout() {
System.out.println("로그아웃");
member = null;
}
2. 계정 기능 설계(로그인 적용)
public static void main(String[] args) {
MemberService memberService = new MemberService();
Scanner scanner = new Scanner(System.in);
while (true) {
if(memberService.member == null) {
System.out.println("1.회원가입 2.로그인");
int input = Integer.parseInt(scanner.nextLine());
switch (input) {
case 1:
memberService.register();
break;
case 2:
memberService.login();
break;
}
}
else {
System.out.println("1.정보수정 2.탈퇴 3.로그아웃");
int input = Integer.parseInt(scanner.nextLine());
switch (input) {
case 1:
memberService.modify();
break;
case 2:
memberService.remove();
break;
case 3:
memberService.logout();
break;
}
}
}
}