๐โ Java์ Oracle SQL์ JDBC๋ก ์ฐ๋ํ์ฌ TBL_MEMBER ํ ์ด๋ธ์ ์กฐํ(SELECT), ํ ์ด๋ธ์ ๊ฐ์ ์ฝ์ (INSERT), ์์ (UPDATE), ์ญ์ (DELETE)ํ ์ ์๋๋ก MVC๊ตฌ์กฐ๋ก ์ด๋ฃจ์ด์ง ํ์๊ด๋ฆฌ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์ด์ฃผ์ธ์!
public class JDBCTemplate {
/* Connection์ returnํ๋ ๋ฉ์๋ */
public static Connection getConnection() {
Connection conn = null;
Properties prop = new Properties();
try {
prop.load(new FileReader("config/driver.properties"));
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
Class.forName(driver);
conn = DriverManager.getConnection(url, prop);
/* auto commit์ด ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ค์ ๋์ด ์์ผ๋,
* ํ๋ก๊ทธ๋จ ๋ด์์ ์์
๋จ์๋ณ๋ก commit๊ณผ rollback์ ํ๋จํ์ฌ ์ํํ๊ณ ์ํ๋ฏ๋ก
* auto commit์ false๋ก ์ค์ */
conn.setAutoCommit(false);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/* Connection์ closeํ ๋ฉ์๋ */
public static void close(Connection conn) {
try {
if(conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/* Statement๋ฅผ closeํ ๋ฉ์๋ */
public static void close(Statement stmt) {
try {
if(stmt != null && !stmt.isClosed()) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/* ResultSet์ closeํ ๋ฉ์๋ */
public static void close(ResultSet rset) {
try {
if(rset != null && !rset.isClosed()) {
rset.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/* ---------์๋ commit์ ์๋ commit์ผ๋ก ๋ณ๊ฒฝ--------- */
/* ๋ชจ๋ ์ฌ๋ฐ๋ฅด๊ฒ ์คํ์ด ์๋ฃ๋๋ฉด, commitํ ๋ฉ์๋ */
public static void commit(Connection conn) {
try {
if(conn != null && !conn.isClosed()) { // :conn ๊ฐ์ฒด๊ฐ ์กด์ฌํ๋ค๋ฉด, commmit
conn.commit();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/* ํ๋๋ผ๋ ์คํ๋์ง ์๋๋ค๋ฉด, rollbackํ ๋ฉ์๋ */
public static void rollback(Connection conn) {
try {
if(conn != null && !conn.isClosed()) { // :conn ๊ฐ์ฒด๊ฐ ์กด์ฌํ๋ค๋ฉด, rollback
conn.rollback();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public class MemberDTO {
/*
MEMBER_NO NUMBER PRIMARY KEY,
MEMBER_ID VARCHAR2(30) UNIQUE,
MEMBER_PWD VARCHAR2(30) NOT NULL,
MEMBER_NAME VARCHAR2(30),
GENDER VARCHAR2(10) CHECK(GENDER IN ('M', 'F')),
EMAIL VARCHAR2(50),
PHONE VARCHAR2(30),
ADDRESS VARCHAR2(100),
AGE NUMBER CHECK(AGE > 0),
ENROLL_DATE DATE DEFAULT SYSDATE
*/
/* ํ๋ */
private int memberNo;
private String memberId;
private String memberPwd;
private String memberName;
private String gender;
private String email;
private String phone;
private String address;
private int age;
private Date enrollDate;
/* ๊ธฐ๋ณธ ์์ฑ์, ๋ชจ๋ ๋งค๊ฐ๋ณ์๊ฐ ์๋ ์์ฑ์ */
/* getter & setter */
/* toString */
public class MemberMenu {
/* ์์ ์ ๋ฆฌ
MemberMenu(view)์์ ํ์ํ ๋ฉ์๋ ์์ฑ ํ Controller ํธ์ถ
MemberController ๋ฉ์๋ ์์ฑ ํ, Service ํธ์ถํ๋ฉด์ ๊ฐ ๋ฐ๊ธฐ
MemberService ๋ฉ์๋ ์์ฑ ํ, DAO ํธ์ถํ๋ฉด์ ๊ฐ ๋ฐ๊ธฐ
MemberDAO ์ฟผ๋ฆฌ์ ๋งคํํ ํ Service๋ก ๊ฐ ๋ฆฌํด
*/
private Scanner sc = new Scanner(System.in);
public void displayMenu() {
MemberController memberController = new MemberController();
do {
System.out.println("\n *** ํ์ ๊ด๋ฆฌ ํ๋ก๊ทธ๋จ *** \n");
System.out.println("1. ์ ํ์ ๋ฑ๋ก");
System.out.println("2. ๋ชจ๋ ํ์ ์กฐํ");
System.out.println("3. ์์ด๋๋ก ํ์ ์กฐํ");
System.out.println("4. ์ฑ๋ณ๋ก ํ์ ์กฐํ");
System.out.println("5. ์ํธ ์์ ");
System.out.println("6. ์ด๋ฉ์ผ ๋ณ๊ฒฝ");
System.out.println("7. ์ ํ๋ฒํธ ๋ณ๊ฒฝ");
System.out.println("8. ์ฃผ์ ๋ณ๊ฒฝ");
System.out.println("9. ํ์ ํํด");
System.out.print("๋ฒํธ ์ ํ : ");
int no = sc.nextInt();
switch(no) {
case 1 : memberController.registNewMember(inputMember()); break;
case 2 : memberController.selectAllMembers(); break;
case 3 : memberController.searchMemberById(inputMemberId()); break;
case 4 : memberController.searchMemberByGender(inputGender()); break;
case 5 : memberController.modifyPassword(inputMemberId(), inputPassword()); break;
case 6 : memberController.modifyEmail(inputMemberId(), inputEmail()); break;
case 7 : memberController.modifyPhone(inputMemberId(), inputPhone()); break;
case 8 : memberController.modifyAddress(inputMemberId(), inputAddress()); break;
case 9 : memberController.deleteMember(inputMemberId()); break;
case 0 : return;
default : System.out.println("์๋ชป๋ ๋ฒํธ์
๋๋ค. ๋ค์ ์
๋ ฅํด์ฃผ์ธ์. \n");
}
}while(true);
}
public Map<String, String> inputMember() {
/* ํ์์ ๋ํ ๋ชจ๋ ์ ๋ณด๋ฅผ ๋ฌธ์์ด๋ก๋ง ์
๋ ฅํ๊ณ , ์
๋ ฅํ ์ ๋ณด๋ฅผ HashMap์ ๋ฐํํด์ ๋ฆฌํด */
Map<String, String> map = new HashMap<>();
/* ์ฌ์ฉ์์ ์
๋ ฅ์ ๋ฐ์ ๋งต์ ๋ฃ๊ธฐ */
sc.nextLine();
System.out.print("์ฌ์ฉํ์ค ์์ด๋๋ฅผ ์
๋ ฅํด์ฃผ์ธ์ : ");
String id = sc.nextLine();
System.out.print("์์ด๋์ ๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์ : ");
String pwd = sc.nextLine();
System.out.print("์ฑํจ์ ์
๋ ฅํด์ฃผ์ธ์ : ");
String name = sc.nextLine();
System.out.print("์ฑ๋ณ์ ์
๋ ฅํด์ฃผ์ธ์(๋จ:M/์ฌ:F) : ");
String gender = sc.nextLine().toUpperCase();
System.out.print("์ด๋ฉ์ผ์ ์
๋ ฅํด์ฃผ์ธ์ : ");
String email = sc.nextLine();
System.out.print("ํธ๋ํฐ ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์('-'ํฌํจ) : ");
String phone = sc.nextLine();
System.out.print("๊ฐ๋ตํ ์ฃผ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์ : ");
String address = sc.nextLine();
System.out.print("๋์ด๋ฅผ ์
๋ ฅํด์ฃผ์ธ์ : ");
String age = sc.nextLine();
map.put("id", id);
map.put("pwd", pwd);
map.put("name", name);
map.put("gender", gender);
map.put("email", email);
map.put("phone", phone);
map.put("address", address);
map.put("age", age);
return map;
}
public String inputMemberId() {
System.out.print("ํ์ ์์ด๋ : ");
sc.nextLine();
return sc.nextLine();
}
public String inputGender() {
System.out.print("์กฐํํ ์ฑ๋ณ ์
๋ ฅ(๋จ:M/์ฌ:F) : ");
sc.nextLine();
return sc.nextLine().toUpperCase();
}
public String inputPassword() {
System.out.print("์์ ํ ๋น๋ฐ๋ฒํธ ์
๋ ฅ : ");
return sc.nextLine();
}
public String inputEmail() {
System.out.print("์์ ํ ์ด๋ฉ์ผ ์
๋ ฅ : ");
return sc.nextLine();
}
public String inputPhone() {
System.out.print("์์ ํ ์ ํ๋ฒํธ ์
๋ ฅ : ");
return sc.nextLine();
}
public String inputAddress() {
System.out.print("์์ ํ ์ฃผ์ ์
๋ ฅ : ");
return sc.nextLine();
}
}
public class MemberResultView {
public void displayDmlResult(String code) {
switch(code) {
case "insertFailed" : System.out.println("ํ์ ๊ฐ์
์คํจ :("); break;
case "updateFailed" : System.out.println("ํ์ ์ ๋ณด ์์ ์คํจ :("); break;
case "deleteFailed" : System.out.println("ํ์ ํํด ์คํจ :("); break;
case "selectFailed" : System.out.println("ํ์ ์กฐํ ์คํจ :("); break;
case "insertSuccess" : System.out.println("ํ์ ๊ฐ์
์๋ฃ :)"); break;
case "updateSuccess" : System.out.println("ํ์ ์ ๋ณด ์์ ์๋ฃ :)"); break;
case "deleteSuccess" : System.out.println("ํ์ ํํด ์๋ฃ :)"); break;
default : System.out.println("์ ์ ์๋ ์๋ฌ ๋ฐ์!"); break;
}
}
public void display(List<MemberDTO> list) {
for(MemberDTO m : list) {
System.out.println(m);
}
}
public void display(MemberDTO m) {
System.out.println(m);
}
}
public class MemberController { // ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ๊ณต
private MemberResultView memberResultView = new MemberResultView();
private MemberService memberService = new MemberService();
/* [1] ์ ๊ท ํ์ ๋ฑ๋ก์ฉ ๋ฉ์๋ */
public void registNewMember(Map<String, String> requestMap) {
/* Map์ผ๋ก ์ ๋ฌ ๋ ๋ฐ์ดํฐ๋ฅผ ๊บผ๋ด MemberDTO์ ๋ด์ Service๋ก ์ ๋ฌ */
/* 1. ๋ทฐ์์ ์ ๋ฌ ๋ฐ์ ํ๋ผ๋ฏธํฐ ๊บผ๋ด์ ๋ณ์์ ๋ด๊ธฐ */
String id = requestMap.get("id");
String pwd = requestMap.get("pwd");
String name = requestMap.get("name");
String gender = requestMap.get("gender");
String email = requestMap.get("email");
String phone = requestMap.get("phone");
String address = requestMap.get("address");
String age = requestMap.get("age");
/* 2. ์ถ๊ฐ์ ์ผ๋ก ํ์ํ ๊ฐ์ด ์๋ ๊ฒฝ์ฐ ์์ฑํ๊ธฐ */
/* 3. ์๋น์ค์ชฝ์ผ๋ก ์ ๋ฌํ๊ธฐ ์ํด DTO ์ธ์คํด์ค์ ๋ด๊ธฐ */
MemberDTO member = new MemberDTO();
member.setMemberId(id);
member.setMemberPwd(pwd);
member.setMemberName(name);
member.setGender(gender);
member.setEmail(email);
member.setPhone(phone);
member.setAddress(address);
member.setAge(Integer.parseInt(age));
/* 1. Service(๋น์ฆ๋์ค ๋ก์ง)๋ฅผ ํธ์ถํ๋ฉด์ ์ธ์ ์ ๋ฌํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํด ๋ฐ์ */
int result = memberService.registNewMember(member);
/* 5. ์๋น์ค ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ด์ฉํด ์ฑ๊ณต ์คํจ ์ฌ๋ถ๋ฅผ ํ๋จํ์ฌ ์ฌ์ฉ์์๊ฒ ๋ณด์ฌ์ค ๋ทฐ๋ฅผ ๊ฒฐ์ ํจ */
if(result > 0) {
memberResultView.displayDmlResult("insertSuccess");
} else {
memberResultView.displayDmlResult("insertFailed");
}
}
/* [2] ๋ชจ๋ ํ์ ์ ๋ณด ์กฐํ์ฉ ๋ฉ์๋(List๋ก ์กฐํํ ๊ฒ) */
public void selectAllMembers() {
/* 1. Service(๋น์ฆ๋์ค ๋ก์ง)๋ฅผ ํธ์ถํ๋ฉด์ ์ธ์ ์ ๋ฌํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํด ๋ฐ์ */
List<MemberDTO> memberList = memberService.selectAllMembers();
/* 2. ์๋น์ค ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ด์ฉํด ์ฑ๊ณต ์คํจ ์ฌ๋ถ๋ฅผ ํ๋จํ์ฌ ์ฌ์ฉ์์๊ฒ ๋ณด์ฌ์ค ๋ทฐ๋ฅผ ๊ฒฐ์ ํจ */
if(memberList != null) {
memberResultView.display(memberList);
} else {
memberResultView.displayDmlResult("selectFailed");
}
}
/* [3] ์์ด๋๋ฅผ ์ด์ฉํ ํ์ ์ ๋ณด ๊ฒ์(MemberDTO๋ก ํ ๋ช
ํ์ ์ ๋ณด ์กฐํ) */
public void searchMemberById(String id) { // ์
๋ ฅ๋ฐ์ id ๋งค๊ฐ๋ณ์๋ก ๋ค์ด์ด
/* 1. Service(๋น์ฆ๋์ค ๋ก์ง)๋ฅผ ํธ์ถํ๋ฉด์ ์ธ์ ์ ๋ฌํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํด ๋ฐ์ */
MemberDTO member = memberService.searchMemberById(id);
/* 3. ์๋น์ค ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ด์ฉํด ์ฑ๊ณต ์คํจ ์ฌ๋ถ๋ฅผ ํ๋จํ์ฌ ์ฌ์ฉ์์๊ฒ ๋ณด์ฌ์ค ๋ทฐ๋ฅผ ๊ฒฐ์ ํจ */
if(member != null) {
memberResultView.display(member);
} else {
memberResultView.displayDmlResult("selectFailed");
}
}
/* [4] ์ฑ๋ณ์ ์ด์ฉํ ํ์ ์ ๋ณด ๊ฒ์ (List๋ก ์กฐํํ ๊ฒ)*/
public void searchMemberByGender(String gender) { // ์
๋ ฅ๋ฐ์ gender ๋งค๊ฐ๋ณ์๋ก ๋ค์ด์ด
/* 1. Service(๋น์ฆ๋์ค ๋ก์ง)๋ฅผ ํธ์ถํ๋ฉด์ ์ธ์ ์ ๋ฌํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํด ๋ฐ์ */
List<MemberDTO> memberList = memberService.searchMemberByGender(gender);
/* 3. ์๋น์ค ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ด์ฉํด ์ฑ๊ณต ์คํจ ์ฌ๋ถ๋ฅผ ํ๋จํ์ฌ ์ฌ์ฉ์์๊ฒ ๋ณด์ฌ์ค ๋ทฐ๋ฅผ ๊ฒฐ์ ํจ */
if(memberList != null) {
memberResultView.display(memberList);
} else {
memberResultView.displayDmlResult("selectFailed");
}
}
/* [5] ์
๋ ฅ๋ฐ์ ์์ด๋์ ์ผ์นํ๋ ํ์์ ๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ */
public void modifyPassword(String memberId, String password) { // ์
๋ ฅ๋ฐ์ id, pwd ๋งค๊ฐ๋ณ์๋ก ๋ค์ด์ด
/* 1. Service(๋น์ฆ๋์ค ๋ก์ง)๋ฅผ ํธ์ถํ๋ฉด์ ์ธ์ ์ ๋ฌํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํด ๋ฐ์ */
int result = memberService.modifyPassword(memberId, password);
/* 3. ์๋น์ค ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ด์ฉํด ์ฑ๊ณต ์คํจ ์ฌ๋ถ๋ฅผ ํ๋จํ์ฌ ์ฌ์ฉ์์๊ฒ ๋ณด์ฌ์ค ๋ทฐ๋ฅผ ๊ฒฐ์ ํจ */
if(result > 0) {
memberResultView.displayDmlResult("updateSuccess");
} else {
memberResultView.displayDmlResult("updateFailed");
}
}
/* [6] ์
๋ ฅ๋ฐ์ ์์ด๋์ ์ผ์นํ๋ ํ์์ ์ด๋ฉ์ผ ๋ณ๊ฒฝ */
public void modifyEmail(String memberId, String email) {
/* 1. Service(๋น์ฆ๋์ค ๋ก์ง)๋ฅผ ํธ์ถํ๋ฉด์ ์ธ์ ์ ๋ฌํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํด ๋ฐ์ */
int result = memberService.modifyEmail(memberId, email);
/* 3. ์๋น์ค ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ด์ฉํด ์ฑ๊ณต ์คํจ ์ฌ๋ถ๋ฅผ ํ๋จํ์ฌ ์ฌ์ฉ์์๊ฒ ๋ณด์ฌ์ค ๋ทฐ๋ฅผ ๊ฒฐ์ ํจ */
if(result > 0) {
memberResultView.displayDmlResult("updateSuccess");
} else {
memberResultView.displayDmlResult("updateFailed");
}
}
/* [7] ์
๋ ฅ๋ฐ์ ์์ด๋์ ์ผ์นํ๋ ํ์์ ์ ํ๋ฒํธ ๋ณ๊ฒฝ */
public void modifyPhone(String memberId, String phone) {
/* 1. Service(๋น์ฆ๋์ค ๋ก์ง)๋ฅผ ํธ์ถํ๋ฉด์ ์ธ์ ์ ๋ฌํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํด ๋ฐ์ */
int result = memberService.modifyPhone(memberId, phone);
/* 3. ์๋น์ค ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ด์ฉํด ์ฑ๊ณต ์คํจ ์ฌ๋ถ๋ฅผ ํ๋จํ์ฌ ์ฌ์ฉ์์๊ฒ ๋ณด์ฌ์ค ๋ทฐ๋ฅผ ๊ฒฐ์ ํจ */
if(result > 0) {
memberResultView.displayDmlResult("updateSuccess");
} else {
memberResultView.displayDmlResult("updateFailed");
}
}
/* [8] ์
๋ ฅ๋ฐ์ ์์ด๋์ ์ผ์นํ๋ ํ์์ ์ฃผ์ ๋ณ๊ฒฝ */
public void modifyAddress(String memberId, String address) {
/* 1. Service(๋น์ฆ๋์ค ๋ก์ง)๋ฅผ ํธ์ถํ๋ฉด์ ์ธ์ ์ ๋ฌํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํด ๋ฐ์ */
int result = memberService.modifyAddress(memberId, address);
/* 2. ์๋น์ค ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ด์ฉํด ์ฑ๊ณต ์คํจ ์ฌ๋ถ๋ฅผ ํ๋จํ์ฌ ์ฌ์ฉ์์๊ฒ ๋ณด์ฌ์ค ๋ทฐ๋ฅผ ๊ฒฐ์ ํจ */
if(result > 0) {
memberResultView.displayDmlResult("updateSuccess");
} else {
memberResultView.displayDmlResult("updateFailed");
}
}
/* [9] ํ์ ์ ๋ณด ์ญ์ ์ฉ ๋ฉ์๋ */
public void deleteMember(String memberId) {
/* 1. Service(๋น์ฆ๋์ค ๋ก์ง)๋ฅผ ํธ์ถํ๋ฉด์ ์ธ์ ์ ๋ฌํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํด ๋ฐ์ */
int result = memberService.deleteMember(memberId);
/* 2. ์๋น์ค ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ด์ฉํด ์ฑ๊ณต ์คํจ ์ฌ๋ถ๋ฅผ ํ๋จํ์ฌ ์ฌ์ฉ์์๊ฒ ๋ณด์ฌ์ค ๋ทฐ๋ฅผ ๊ฒฐ์ ํจ */
if(result > 0) {
memberResultView.displayDmlResult("deleteSuccess");
} else {
memberResultView.displayDmlResult("deleteFailed");
}
}
}
public class MemberService {
private MemberDAO memberDAO = new MemberDAO();
/* [1] ์ ๊ท ํ์ ๋ฑ๋ก์ฉ ๋ฉ์๋ */
public int registNewMember(MemberDTO member) {
/* 1. Connection ์์ฑ */
Connection conn = getConnection();
/* 2. ์ฑ๊ณต์ฌ๋ถ ํ๋จํ ๊ฐ ์ด๊ธฐํ */
int result = 0;
/* 3. DAO ๋ฉ์๋๋ก ์ ๋ฌ ๋ฐ์ ๊ฐ ๋๊ฒจ์ insert */
/* TBL_MEMBER์ insert */
int memberResult = memberDAO.insertNewMember(conn, member);
/* 4. ์ฑ๊ณต ์ฌ๋ถ ํ๋จ ํ ํธ๋์ญ์
์ฒ๋ฆฌ */
if(memberResult > 0) {
commit(conn);
result = 1;
} else {
rollback(conn);
}
/* 5. Connection ๋ซ๊ธฐ */
close(conn);
/* 6. ๊ฒฐ๊ณผ ๊ฐ ๋ฐํ */
return result;
}
/* [2] ๋ชจ๋ ํ์ ์ ๋ณด ์กฐํ์ฉ ๋ฉ์๋ */
public List<MemberDTO> selectAllMembers() {
/* 1. Connection ์์ฑ */
Connection conn = getConnection();
/* 2. DAO์ ๋ฉ์๋ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ ๋ฆฌํด ๋ฐ๊ธฐ */
List<MemberDTO> memberList = memberDAO.selectAllMembers(conn);
/* 3. ํธ๋์ญ์
๊ด๋ฆฌ (select์ ๊ฒฝ์ฐ commit, rollback์ด ํ์์์ผ๋ฏ๋ก ์๋ต) */
/* 4. Connection ๋ฐ๋ฉ */
close(conn);
return memberList; // ์ต์ข
๋ฐํ
}
/* [3] ์์ด๋๋ฅผ ์ด์ฉํ ํ์ ์ ๋ณด ๊ฒ์์ฉ ๋ฉ์๋ */
public MemberDTO searchMemberById(String id) { // Controller๋ก๋ถํฐ id์ธ์ ์ ๋ฌ ๋ฐ๊ธฐ
/* 1. Connection ์์ฑ */
Connection conn = getConnection();
/* 2. DAO์ ๋ฉ์๋ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ ๋ฆฌํด ๋ฐ๊ธฐ */
MemberDTO member = memberDAO.searchMemberById(conn, id);
/* 3. ํธ๋์ญ์
๊ด๋ฆฌ (select์ ๊ฒฝ์ฐ commit, rollback์ด ํ์์์ผ๋ฏ๋ก ์๋ต) */
/* 4. Connection ๋ฐ๋ฉ */
close(conn);
return member;
}
/* [4] ์ฑ๋ณ์ ์ด์ฉํ ํ์ ์ ๋ณด ๊ฒ์์ฉ ๋ฉ์๋ */
public List<MemberDTO> searchMemberByGender(String gender) {
/* 1. Connection ์์ฑ */
Connection conn = getConnection();
/* 2. DAO์ ๋ฉ์๋ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ ๋ฆฌํด ๋ฐ๊ธฐ */
List<MemberDTO> memberList = memberDAO.searchMemberByGender(conn, gender);
/* 3. ํธ๋์ญ์
๊ด๋ฆฌ (select์ ๊ฒฝ์ฐ commit, rollback์ด ํ์์์ผ๋ฏ๋ก ์๋ต) */
/* 4. Connection ๋ฐ๋ฉ */
close(conn);
return memberList;
}
/* [5] ์
๋ ฅ๋ฐ์ ์์ด๋์ ์ผ์นํ๋ ํ์์ ๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ์ฉ ๋ฉ์๋ */
public int modifyPassword(String memberId, String password) {
/* 1. Connection ์์ฑ */
Connection conn = getConnection();
/* 2. ์ฑ๊ณต์ฌ๋ถ ํ๋จํ ๊ฐ ์ด๊ธฐํ */
int result = 0;
/* 3. DAO์ ๋ฉ์๋ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ ๋ฆฌํด ๋ฐ๊ธฐ */
int member = memberDAO.modifyPassword(conn, memberId, password);
/* 4. ์ฑ๊ณต ์ฌ๋ถ ํ๋จ ํ ํธ๋์ญ์
์ฒ๋ฆฌ */
if(member > 0) {
commit(conn);
result = 1;
} else {
rollback(conn);
}
/* 5. Connection ๋ซ๊ธฐ */
close(conn);
/* 6. ๊ฒฐ๊ณผ ๊ฐ ๋ฐํ */
return result;
}
/* [6] ์
๋ ฅ๋ฐ์ ์์ด๋์ ์ผ์นํ๋ ํ์์ ์ด๋ฉ์ผ ๋ณ๊ฒฝ์ฉ ๋ฉ์๋ */
public int modifyEmail(String memberId, String email) {
/* 1. Connection ์์ฑ */
Connection conn = getConnection();
/* 2. ์ฑ๊ณต์ฌ๋ถ ํ๋จํ ๊ฐ ์ด๊ธฐํ */
int result = 0;
/* 3. DAO์ ๋ฉ์๋ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ ๋ฆฌํด ๋ฐ๊ธฐ */
int member = memberDAO.modifyEmail(conn, memberId, email);
/* 4. ์ฑ๊ณต ์ฌ๋ถ ํ๋จ ํ ํธ๋์ญ์
์ฒ๋ฆฌ */
if(member > 0) {
commit(conn);
result = 1;
} else {
rollback(conn);
}
/* 5. Connection ๋ซ๊ธฐ */
close(conn);
/* 6. ๊ฒฐ๊ณผ ๊ฐ ๋ฐํ */
return result;
}
/* [7] ์
๋ ฅ๋ฐ์ ์์ด๋์ ์ผ์นํ๋ ํ์์ ์ ํ๋ฒํธ ๋ณ๊ฒฝ์ฉ ๋ฉ์๋ */
public int modifyPhone(String memberId, String phone) {
/* 1. Connection ์์ฑ */
Connection conn = getConnection();
/* 2. ์ฑ๊ณต์ฌ๋ถ ํ๋จํ ๊ฐ ์ด๊ธฐํ */
int result = 0;
/* 3. DAO์ ๋ฉ์๋ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ ๋ฆฌํด ๋ฐ๊ธฐ */
int member = memberDAO.modifyPhone(conn, memberId, phone);
/* 4. ์ฑ๊ณต ์ฌ๋ถ ํ๋จ ํ ํธ๋์ญ์
์ฒ๋ฆฌ */
if(member > 0) {
commit(conn);
result = 1;
} else {
rollback(conn);
}
/* 5. Connection ๋ซ๊ธฐ */
close(conn);
/* 6. ๊ฒฐ๊ณผ ๊ฐ ๋ฐํ */
return result;
}
/* [8] ์
๋ ฅ๋ฐ์ ์์ด๋์ ์ผ์นํ๋ ํ์์ ์ฃผ์ ๋ณ๊ฒฝ์ฉ ๋ฉ์๋ */
public int modifyAddress(String memberId, String address) {
/* 1. Connection ์์ฑ */
Connection conn = getConnection();
/* 2. ์ฑ๊ณต์ฌ๋ถ ํ๋จํ ๊ฐ ์ด๊ธฐํ */
int result = 0;
/* 3. DAO์ ๋ฉ์๋ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ ๋ฆฌํด ๋ฐ๊ธฐ */
int member = memberDAO.modifyAddress(conn, memberId, address);
/* 4. ์ฑ๊ณต ์ฌ๋ถ ํ๋จ ํ ํธ๋์ญ์
์ฒ๋ฆฌ */
if(member > 0) {
commit(conn);
result = 1;
} else {
rollback(conn);
}
/* 5. Connection ๋ซ๊ธฐ */
close(conn);
/* 6. ๊ฒฐ๊ณผ ๊ฐ ๋ฐํ */
return result;
}
/* [9] ํ์ ์ ๋ณด ์ญ์ ์ฉ ๋ฉ์๋ */
public int deleteMember(String memberId) {
/* 1. Connection ์์ฑ */
Connection conn = getConnection();
/* 2. ์ฑ๊ณต์ฌ๋ถ ํ๋จํ ๊ฐ ์ด๊ธฐํ */
int result = 0;
/* 3. DAO์ ๋ฉ์๋ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ ๋ฆฌํด ๋ฐ๊ธฐ */
int member = memberDAO.deleteMember(conn, memberId);
/* 4. ์ฑ๊ณต ์ฌ๋ถ ํ๋จ ํ ํธ๋์ญ์
์ฒ๋ฆฌ */
if(member > 0) {
commit(conn);
result = 1;
} else {
rollback(conn);
}
/* 5. Connection ๋ซ๊ธฐ */
close(conn);
/* 6. ๊ฒฐ๊ณผ ๊ฐ ๋ฐํ */
return result;
}
}
public class MemberDAO {
// ํ์ผ์ ์
๋ ฅ ๋ฐ๊ธฐ ์ํด prop ๊ฐ์ฒด ์์ฑ ํ ์์ธ์ฒ๋ฆฌ
private Properties prop = new Properties();
public MemberDAO() {
try {
prop.loadFromXML(new FileInputStream("mapper/member-query.xml"));
} catch (IOException e) {
e.printStackTrace();
}
}
/* [1] ์ ๊ท ํ์ ๋ฑ๋ก์ฉ ๋ฉ์๋ */
public int insertNewMember(Connection con, MemberDTO member) {
PreparedStatement pstmt = null;
int result = 0;
String query = prop.getProperty("insertNewMember");
try {
pstmt = con.prepareStatement(query);
pstmt.setString(1, member.getMemberId());
pstmt.setString(2, member.getMemberPwd());
pstmt.setString(3, member.getMemberName());
pstmt.setString(4, member.getGender());
pstmt.setString(5, member.getEmail());
pstmt.setString(6, member.getPhone());
pstmt.setString(7, member.getAddress());
pstmt.setInt(8, member.getAge());
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(pstmt);
}
return result;
}
/* [2] ๋ชจ๋ ํ์ ์ ๋ณด ์กฐํ์ฉ ๋ฉ์๋ */
public List<MemberDTO> selectAllMembers(Connection conn) {
PreparedStatement pstmt = null;
ResultSet rset = null;
List<MemberDTO> memberList = null; // ์ต์ข
๋ฐํํ ๋ฆฌ์คํธ
String query = prop.getProperty("selectAllMembers");
try {
pstmt = conn.prepareStatement(query);
rset = pstmt.executeQuery();
memberList = new ArrayList<>();
while(rset.next()) { // ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ, ๋งคํํ๋ ๊ฒ!?
MemberDTO member = new MemberDTO();
member.setMemberNo(rset.getInt("MEMBER_NO"));
member.setMemberId(rset.getString("MEMBER_ID"));
member.setMemberPwd(rset.getString("MEMBER_PWD"));
member.setMemberName(rset.getString("MEMBER_NAME"));
member.setGender(rset.getString("GENDER"));
member.setEmail(rset.getString("EMAIL"));
member.setPhone(rset.getString("PHONE"));
member.setAddress(rset.getString("ADDRESS"));
member.setAge(rset.getInt("AGE"));
member.setEnrollDate(rset.getDate("ENROLL_DATE"));
memberList.add(member);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(rset);
close(pstmt); // conn์ ์๋น์ค์์ ๋ซ๊ธฐ
}
return memberList;
}
/* [3] ์์ด๋๋ฅผ ์ด์ฉํ ํ์ ์ ๋ณด ๊ฒ์์ฉ ๋ฉ์๋ */
public MemberDTO searchMemberById(Connection conn, String id) {
PreparedStatement pstmt = null;
ResultSet rset = null;
MemberDTO member = null;
String query = prop.getProperty("selectMemberById");
try {
pstmt = conn.prepareStatement(query);
pstmt.setString(1, id);
rset = pstmt.executeQuery();
while(rset.next()) {
member = new MemberDTO();
member.setMemberNo(rset.getInt("MEMBER_NO"));
member.setMemberId(rset.getString("MEMBER_ID"));
member.setMemberPwd(rset.getString("MEMBER_PWD"));
member.setMemberName(rset.getString("MEMBER_NAME"));
member.setGender(rset.getString("GENDER"));
member.setEmail(rset.getString("EMAIL"));
member.setPhone(rset.getString("PHONE"));
member.setAddress(rset.getString("ADDRESS"));
member.setAge(rset.getInt("AGE"));
member.setEnrollDate(rset.getDate("ENROLL_DATE"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(rset);
close(pstmt);
}
return member;
}
/* [4] ์ฑ๋ณ์ ์ด์ฉํ ํ์ ์ ๋ณด ๊ฒ์์ฉ ๋ฉ์๋ */
public List<MemberDTO> searchMemberByGender(Connection conn, String gender) {
PreparedStatement pstmt = null;
ResultSet rset = null;
List<MemberDTO> memberList = null;
String query = prop.getProperty("selectMemberByGender");
try {
pstmt = conn.prepareStatement(query);
pstmt.setString(1, gender);
rset = pstmt.executeQuery();
memberList = new ArrayList<>();
while(rset.next()) {
MemberDTO member = new MemberDTO();
member.setMemberNo(rset.getInt("MEMBER_NO"));
member.setMemberId(rset.getString("MEMBER_ID"));
member.setMemberPwd(rset.getString("MEMBER_PWD"));
member.setMemberName(rset.getString("MEMBER_NAME"));
member.setGender(rset.getString("GENDER"));
member.setEmail(rset.getString("EMAIL"));
member.setPhone(rset.getString("PHONE"));
member.setAddress(rset.getString("ADDRESS"));
member.setAge(rset.getInt("AGE"));
member.setEnrollDate(rset.getDate("ENROLL_DATE"));
memberList.add(member);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(rset);
close(pstmt);
}
return memberList;
}
/* [5] ์
๋ ฅ๋ฐ์ ์์ด๋์ ์ผ์นํ๋ ํ์์ ๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ์ฉ ๋ฉ์๋ */
public int modifyPassword(Connection conn, String memberId, String password) {
PreparedStatement pstmt = null;
String query = prop.getProperty("updatePassword");
int result = 0;
MemberDTO member = new MemberDTO();
member.setMemberPwd(password);
try {
pstmt = conn.prepareStatement(query);
pstmt.setString(1, member.getMemberPwd());
pstmt.setString(2, memberId);
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(pstmt);
}
return result;
}
/* [6] ์
๋ ฅ๋ฐ์ ์์ด๋์ ์ผ์นํ๋ ํ์์ ์ด๋ฉ์ผ ๋ณ๊ฒฝ์ฉ ๋ฉ์๋ */
public int modifyEmail(Connection conn, String memberId, String email) {
PreparedStatement pstmt = null;
String query = prop.getProperty("updateEmail");
int result = 0;
MemberDTO member = new MemberDTO();
member.setEmail(email);
try {
pstmt = conn.prepareStatement(query);
pstmt.setString(1, member.getEmail());
pstmt.setString(2, memberId);
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(pstmt);
}
return result;
}
/* [7] ์
๋ ฅ๋ฐ์ ์์ด๋์ ์ผ์นํ๋ ํ์์ ์ ํ๋ฒํธ ๋ณ๊ฒฝ์ฉ ๋ฉ์๋ */
public int modifyPhone(Connection conn, String memberId, String phone) {
PreparedStatement pstmt = null;
String query = prop.getProperty("updatePhone");
int result = 0;
MemberDTO member = new MemberDTO();
member.setPhone(phone);
try {
pstmt = conn.prepareStatement(query);
pstmt.setString(1, member.getPhone());
pstmt.setString(2, memberId);
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(pstmt);
}
return result;
}
/* [8] ์
๋ ฅ๋ฐ์ ์์ด๋์ ์ผ์นํ๋ ํ์์ ์ ํ๋ฒํธ ๋ณ๊ฒฝ์ฉ ๋ฉ์๋ */
public int modifyAddress(Connection conn, String memberId, String address) {
PreparedStatement pstmt = null;
String query = prop.getProperty("updateAddress");
int result = 0;
MemberDTO member = new MemberDTO();
member.setAddress(address);
try {
pstmt = conn.prepareStatement(query);
pstmt.setString(1, member.getAddress());
pstmt.setString(2, memberId);
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(pstmt);
}
return result;
}
/* [9] ํ์ ์ ๋ณด ์ญ์ ์ฉ ๋ฉ์๋ */
public int deleteMember(Connection conn, String memberId) {
PreparedStatement pstmt = null;
String query = prop.getProperty("deleteMember");
int result = 0;
try {
pstmt = conn.prepareStatement(query);
pstmt.setString(1, memberId);
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(pstmt);
}
return result;
}
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<!-- [1] ์ ๊ท ํ์ ๋ฑ๋ก์ฉ ์ฟผ๋ฆฌ -->
<entry key="insertNewMember">
INSERT
INTO TBL_MEMBER A
(
A.MEMBER_NO
, A.MEMBER_ID
, A.MEMBER_PWD
, A.MEMBER_NAME
, A.GENDER
, A.EMAIL
, A.PHONE
, A.ADDRESS
, A.AGE
, A.ENROLL_DATE
)
VALUES
(
SEQ_MEMBER_NO.NEXTVAL
, ?
, ?
, ?
, ?
, ?
, ?
, ?
, ?
, SYSDATE
)
</entry>
<!-- [2] ๋ชจ๋ ํ์ ์ ๋ณด ์กฐํ์ฉ ์ฟผ๋ฆฌ -->
<entry key="selectAllMembers">
SELECT
*
FROM TBL_MEMBER
</entry>
<!-- [3] ์์ด๋๋ฅผ ์ด์ฉํ ํ์ ์ ๋ณด ๊ฒ์์ฉ ์ฟผ๋ฆฌ -->
<entry key="selectMemberById">
SELECT
*
FROM TBL_MEMBER
WHERE MEMBER_ID = ?
</entry>
<!-- [4] ์ฑ๋ณ์ ์ด์ฉํ ํ์ ์ ๋ณด ๊ฒ์์ฉ ์ฟผ๋ฆฌ -->
<entry key="selectMemberByGender">
SELECT
*
FROM TBL_MEMBER
WHERE GENDER = ?
</entry>
<!-- [5] ์
๋ ฅ๋ฐ์ ์์ด๋์ ์ผ์นํ๋ ํ์์ ๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ์ฉ ์ฟผ๋ฆฌ -->
<entry key="updatePassword">
UPDATE
TBL_MEMBER
SET MEMBER_PWD = ?
WHERE MEMBER_ID = ?
</entry>
<!-- [6] ์
๋ ฅ๋ฐ์ ์์ด๋์ ์ผ์นํ๋ ํ์์ ์ด๋ฉ์ผ ๋ณ๊ฒฝ์ฉ ์ฟผ๋ฆฌ -->
<entry key="updateEmail">
UPDATE
TBL_MEMBER
SET EMAIL = ?
WHERE MEMBER_ID = ?
</entry>
<!-- [7] ์
๋ ฅ๋ฐ์ ์์ด๋์ ์ผ์นํ๋ ํ์์ ์ ํ๋ฒํธ ๋ณ๊ฒฝ์ฉ ์ฟผ๋ฆฌ -->
<entry key="updatePhone">
UPDATE
TBL_MEMBER
SET PHONE = ?
WHERE MEMBER_ID = ?
</entry>
<!-- [8] ์
๋ ฅ๋ฐ์ ์์ด๋์ ์ผ์นํ๋ ํ์์ ์ฃผ์ ๋ณ๊ฒฝ์ฉ ์ฟผ๋ฆฌ -->
<entry key="updateAddress">
UPDATE
TBL_MEMBER
SET ADDRESS = ?
WHERE MEMBER_ID = ?
</entry>
<!-- [9] ํ์ ์ ๋ณด ์ญ์ ์ฉ ๋ฉ์๋ -->
<entry key="deleteMember">
DELETE
FROM TBL_MEMBER
WHERE MEMBER_ID = ?
</entry>
</properties>
public class Application {
public static void main(String[] args) {
new MemberMenu().displayMenu();
System.out.println("ํ์๊ด๋ฆฌ ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.");
}
}
๐ฌ Overall Comment
* ์ฒ์์ผ๋ก MVC๊ตฌ์กฐ๋ฅผ ์ค์ค๋ก ์์ฑํด๋ณธ ๊ฒฝํ์ด์๋ค. ์์
์์ ๋ฐฐ์ด ๋ด์ฉ์ ํ ๋๋ก ํ์๊ด๋ฆฌ
ํ๋ก๊ทธ๋จ์ ์์ฑํ๋๋ฐ, ์ฒ์์๋ ๊ต์ฅํ ๋ง๋งํ์ง๋ง ํ๋ํ๋ ๋ง๋ค์ด๊ฐ๋ ๊ตฌ์กฐ๊ฐ ์ ์ ํ๋ฆฝ๋์ด๊ฐ๊ณ
ํ๋ฆ์ ์ฝ์ ์ค ์๊ฒ ๋์๋ค. ์ดํ์ MVC ๊ตฌ์กฐ๋ฅผ ํ์ฉํ์ฌ ํ๋ก์ ํธ๋ฅผ ์งํํ ๋ ์ด๋ฒ ๋์ ์ด
๊ต์ฅํ ํฐ ๋์์ด ๋ ๊ฒ ๊ฐ๋ค.