반응형
<MemberVo>
package member;
public class MemberVo {
private String id;
private String pwd;
private String name;
private String phone;
public MemberVo () {}
public MemberVo(String id, String pwd, String name, String phone) {
super();
this.id = id;
this.pwd = pwd;
this.name = name;
this.phone = phone;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "[ id: " + id + "\n | pwd: " + pwd + "\n | 이름: " + name + "\n | 전화번호: " + phone + " ] \n";
}
}
<MemberDao>
package member;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import conn.DBConnect;
public class MemberDao {
private DBConnect dbconn;
public MemberDao() {
dbconn = DBConnect.getInstance();
}
// 추가
public void insert(MemberVo vo) {
Connection conn = dbconn.conn();
String sql = "insert into t_member values (?,?,?,?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, vo.getId());
pstmt.setString(2, vo.getPwd());
pstmt.setString(3, vo.getName());
pstmt.setString(4, vo.getPhone());
int num = pstmt.executeUpdate();
System.out.println("회원이 추가되었습니다");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 수정
public void update(MemberVo vo) {
Connection conn = dbconn.conn();
String sql = "update t_member set pwd=?, phone =? where id =?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, vo.getPwd());
pstmt.setString(2, vo.getPhone());
pstmt.setString(3, vo.getId());
int num = pstmt.executeUpdate();
System.out.println("회원정보가 수정되었습니다");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 삭제
public void delete(String id) {
Connection conn = dbconn.conn();
String sql = "delete t_member where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
int num = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public ArrayList<MemberVo> selectAll() { // 전체 검색
Connection conn = dbconn.conn();
String sql = "select * from t_member";
ArrayList<MemberVo> list = new ArrayList<MemberVo>();
// BoardVo 오른쪽 생략 가능
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
list.add(new MemberVo(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
// 검색
public MemberVo select(String id) {
MemberVo vo = null;
Connection conn = dbconn.conn();
String sql = "select * from t_member where id =?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
ResultSet rs = pstmt.executeQuery(); // 실행
if (rs.next()) { // 검색 결과가 있다면
String id2 = rs.getString("id");
String pwd = rs.getString("pwd");
String name = rs.getString("name");
String phone = rs.getString("phone");
vo = new MemberVo(id2, pwd, name, phone);
// System.out.println(id2 + "계정이 검색되었다");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return vo;
}
}
<MemberService>
package member;
import java.util.ArrayList;
import java.util.Scanner;
public class MemberService {
private MemberDao dao;
// 로그인한 사람의 아이디 저장. 이 값이 널이면 로그인 안한 상태
public static String loginId;
// 생성자
public MemberService() {
dao = new MemberDao();
}
// join
public void join(Scanner sc) {
System.out.println("<회원가입>");
MemberVo vo = new MemberVo();
String id = "";
while (true) {
System.out.print("아이디를 입력하세요: ");
id = sc.next();
MemberVo m = dao.select(id);
if (m == null) {
System.out.println("가입이 가능한 아이디입니다.");
break;
} else {
System.out.println("이미 존재하는 아이디입니다.");
}
}
vo.setId(id);
System.out.println("비밀번호를 입력하세요: ");
String pwd = sc.next();
vo.setPwd(pwd);
System.out.println("이름을 입력하세요: ");
vo.setName(sc.next());
System.out.println("전화번호를 입력하세요: ");
vo.setPhone(sc.next());
dao.insert(vo);
}
// login
public int login(Scanner sc) {// 0:로그인실패 / 1:일반로그인 / 2:매니저로그인
System.out.println("<로그인>");
System.out.print("아이디를 입력하세요: ");
String id = sc.next();
System.out.print("비밀번호를 입력하세요: ");
String pwd = sc.next();
MemberVo vo = dao.select(id);
if (vo == null) {
System.out.println("존재하지 않는 아이디입니다.");
} else {
if (pwd.equals(vo.getPwd())) {
loginId = id;
System.out.println(id + "님 로그인 되었습니다.");
if (loginId.equals("director")) {
// System.out.print("관리자 계정 로그인 성공");
return 2;
} else {
return 1;
}
} else {
System.out.print("잘못된 비밀번호입니다. \n");
}
}
return 0;
}
// 로그아웃
public int logout() {
if (loginId == null) {
} else {
System.out.println(loginId + "님 로그아웃 되었습니다.");
loginId = null;
}
return 0;
}
// 내 정보 확인
public boolean printMyInfo() {
System.out.println("<내 정보 확인>");
if (loginId == null) {
System.out.print("로그인 상태가 아닙니다.\n");
return false;
} else {
System.out.print(dao.select(loginId));
return true;
}
}
// 내 정보 수정
public void editMyInfo(Scanner sc) {
System.out.println("<내 정보 수정>");
boolean flag = printMyInfo();
if (flag == false) {
return;
} else {
System.out.print("새 비밀번호를 입력하세요: ");
String pwd = sc.next();
System.out.print("새 전화번호를 입력하세요: ");
String phone = sc.next();
dao.update(new MemberVo(loginId, pwd, null, phone));
}
}
// 회원탈퇴
public void out() {
System.out.println("<탈퇴>");
if (loginId == null) {
System.out.print("로그인 상태가 아닙니다.");
} else {
System.out.println(loginId + "회원님 탈퇴되었습니다. ");
dao.delete(loginId);
loginId = null;
}
}
public void getAll() {
System.out.println("<글 전체목록>");
ArrayList<MemberVo> list = dao.selectAll();
for (MemberVo vo : list) {
System.out.println(vo.getId() + "\t" + vo.getName() + "\t" + vo.getPhone());
}
}
}
반응형