member jdbc
JDBC
MemberMain
package memberDB;
import java.util.Scanner;
public class MemberMain {
public static void main(String[] args) {
MemberService ms = new MemberService();
Action action = null;
Scanner sc = new Scanner(System.in);
boolean isStop = false;
do {
System.out.println("메뉴를 선택하세요.");
System.out.println("1. 회원 가입");
System.out.println("2. 회원 목록보기");
System.out.println("3. 회원 정보수정");
System.out.println("4. 회원 정보삭제");
System.out.println("5. 종료");
String command = sc.next();
switch (command) {
case "1":
System.out.println("회원 가입");
action = new AddAction();
ms.process(action, sc);
//action.execute(sc);
break;
case "2":
System.out.println("회원 목록보기");
action = new ListAction();
ms.process(action, sc);
break;
case "3":
System.out.println("회원 정보수정");
action = new UpdateAction();
ms.process(action, sc);
break;
case "4":
System.out.println("회원 정보삭제");
action = new DeleteAction();
ms.process(action, sc);
break;
case "5":
System.out.println("5");
isStop = true;
}
}while(!isStop);
}
}
MemberService
- 요청을 제어하는 서비스
- 실질적인 비즈니스 로직을 정의하는 Service
package memberDB;
import java.util.Scanner;
public class MemberService {
void process(Action action,Scanner sc){
action.execute(sc);
}
}
MemberVo
package memberDB;
public class MemberVo {
private String id;
private String pw;
private String name;
private int age;
public MemberVo() {
}
public MemberVo(String id, String pw, String name, int age) {
super();
this.id = id;
this.pw = pw;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "MemverVo [id=" + id + ", pw=" + pw + ", name=" + name + ", age=" + age + "]";
}
}
Action
- 요청 처리 규격을 정의(인터페이스)
- 다형성 지원을 위해 사용
package memberDB;
import java.util.Scanner;
public interface Action {
void execute(Scanner sc);
}
각 액션들
AddAction
package memberDB;
import java.util.Scanner;
import dbtest.MemberDAO;
import dbtest.MemberVo;
public class AddAction implements Action{
@Override
public void execute(Scanner sc) {
System.out.println("회원 정보 입력");
System.out.print("ID :");
String id = sc.next();
System.out.print("PW :");
String pw = sc.next();
System.out.print("NAME :");
String name = sc.next();
System.out.print("AGE :");
int age = sc.nextInt();
MemberDAO dao = new MemberDAO();
MemberVo m = new MemberVo(id, pw, name, age);
int rs = dao.insertMember(m);
System.out.println("rs = "+ rs);
}
}
ListAction
package memberDB;
import java.util.List;
import java.util.Scanner;
import dbtest.MemberDAO;
import dbtest.MemberVo;
public class ListAction implements Action{
@Override
public void execute(Scanner sc) {
MemberDAO dao = new MemberDAO();
List<MemberVo> members = dao.getMemberList();
System.out.printf("=== Member Count %s명 === \n",members.size());
for(int i=0;i<members.size();i++) {
System.out.print("ID: " + members.get(i).getId() +" / ");
System.out.print("PW: " + members.get(i).getPw() +" / ");
System.out.print("Name: " + members.get(i).getName() +" / ");
System.out.print("Age: " + members.get(i).getAge() );
System.out.println();
}
System.out.printf("=== Member Count %s명 ===\n",members.size());
for(MemberVo m: members) {
System.out.print("ID: " + m.getId() +" / ");
System.out.print("PW: " + m.getPw() +" / ");
System.out.print("Name: " + m.getName() +" / ");
System.out.print("Age: " + m.getAge() );
System.out.println();
}
}
}
UpdateAction
package memberDB;
import java.util.List;
import java.util.Scanner;
import dbtest.MemberDAO;
import dbtest.MemberVo;
public class UpdateAction implements Action{
@Override
public void execute(Scanner sc) {
System.out.println("회원 정보 수정");
// 수정할 id 찾기
System.out.print("수정할 ID :");
String id = sc.next(); // 1111
MemberVo member = null;
MemberDAO dao = new MemberDAO();
member = dao.getMemberId(id);
// id 있으면 PW,NAME,AGE 수정
if(member != null) {
System.out.println("기존 PW : " + member.getPw() );
System.out.print("수정 PW :");
String pw = sc.next();
System.out.printf("기존 NAME : %s%n", member.getName());
System.out.print("수정 NAME :");
String name = sc.next();
System.out.printf("기존 AGE %s%n", member.getAge());
System.out.print("수정 AGE :");
int age = sc.nextInt();
// 2. MememberVo 주소 변경
MemberVo m = new MemberVo(id, pw, name, age);
int rs = dao.updateMember(m);
System.out.println("rs = "+ rs);
}else {
System.out.println("없는 ID입니다.");
}
}
}
DeleteAction
package memberDB;
import java.util.Scanner;
import dbtest.MemberDAO;
import dbtest.MemberVo;
public class DeleteAction implements Action{
@Override
public void execute(Scanner sc) {
// id 찾기
System.out.print("삭제할 ID :");
String id = sc.next(); // 1111
MemberVo member = null;
MemberDAO dao = new MemberDAO();
member = dao.getMemberId(id);
if(member != null) {
int rs = dao.deleteMember(id);
System.out.println("rs = " + rs);
}else {
System.out.println(id + "는 없는 id 입니다.");
}
}
}