< 정보 변경하기 >
------------------------------------ View ------------------------------------
1. 사용자에게 값을 아이디를 입력받고, 사용자에게 변경할 값들을 입력받는다.
2. 값들을 Controller에게 넘겨준다.
private void updateMember() {
System.out.println("--- 회원 정보 변경 ---");
System.out.print("변경할 회원의 아이디를 입력해 주세요 > ");
String userId = sc.nextLine();
System.out.print("새 비밀번호를 입력해 주세요 > ");
String newPwd = sc.nextLine();
System.out.print("새 이메일을 입력해 주세요 > ");
String newEmail = sc.nextLine();
System.out.print("새 핸드폰번호를 입력해 주세요 > ");
String newPhone = sc.nextLine();
System.out.print("새 주소를 입력해 주세요 > ");
String newAddress = sc.nextLine();
mc.updateMember(userId, newPwd, newEmail, newPhone, newAddress);
}
------------------------------------ Controller ------------------------------------
3. View에게 매개변수로 받은 데이터를 가공한다.
이 때 View로부터 받은 값이 많으므로, 객체에 넣어 DAO에게 보내준다.
4. DAO를 호출한다
public void updateMember(String userId, String newPwd, String newEmail, String newPhone, String newAddress) {
Member m = new Member();
m.setUserId(userId);
m.setUserPwd(newPwd);
m.setEmail(newEmail);
m.setPhone(newPhone);
m.setAddress(newAddress);
int result = new MemberDao().updateMember(m);
}
------------------------------------ DAO ------------------------------------
5. 필요한 변수를 세팅한다.
public int updateMember(Member m) {
int result = 0;
Connection conn = null;
Statement stmt = null;
String sql = "UPDATE MEMBER "
+ "SET USERPWD = '" + m.getUserPwd() + "', "
+ "EMAIL = '" + m.getEmail() + "', "
+ "PHONE = '" + m.getPhone() + "', "
+ "ADDRESS = '" + m.getAddress() + "' "
+ "WHERE USERID = '" + m.getUserId() + "'";
6. JDBC Driver등록 / Connection 객체 생성, Statement 객체 생성, SQL문(UPDATE) 실행하고 결과 받기 / 트랜잭션 처리 / 자원반납 / 결과반환
이 때, SQL문이 UPDATE이므로 executeUpdate() 메소드 호출. 또한 반환값이 int형이다.(n행이 삭제되었습니다.)
(생략)
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "JDBC", "JDBC");
stmt = conn.createStatement();
result = stmt.executeUpdate(sql);
if(result > 0) {
conn.commit();
} else {
conn.rollback();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
------------------------------------ Controller ------------------------------------
7. 반환받은 int형 값으로 응답화면을 지정해준다.
if(result > 0) {
new MemberView().displaySuccess();
} else {
new MemberView().displayFail();
}
------------------------------------ View ------------------------------------
8. View는 사용자에게 응답화면을 보여준다.
public void displaySuccess() {
System.out.println("\n서비스 요청 성공!!");
}
public void displayFail() {
System.out.println("\n서비스 요청 실패 ㅠㅠ..");
}
'JDBC > JDBC 수업' 카테고리의 다른 글
#31. JDBC(Properties) (0) | 2023.08.14 |
---|---|
#30. JDBC(model에 service클래스 추가), Template (0) | 2023.08.11 |
#29. test... 팀플과제 (0) | 2023.08.10 |
#28. PreparedStatement (0) | 2023.08.09 |
#26. JDBC, Connection, Statement, ResultSet (0) | 2023.08.07 |