package member;
import java.sql.*; import java.util.*;
import conn.*;
public class MemberManager { private Scanner s = new Scanner(System.in);
public static void main(String[] args) { new MemberManager().execute(); }
private void execute() { while (true) { StringBuilder menu = new StringBuilder(); menu.append("***************\n"); menu.append("* 1. 회원검색 *\n"); menu.append("* 2. 회원목록 *\n"); menu.append("* 3. 회원추가 *\n"); menu.append("* 4. 회원수정 *\n"); menu.append("* 5. 회원삭제 *\n"); menu.append("* 6. 끝내기 *\n"); menu.append("***************\n"); System.out.print(menu); System.out.print("입력>"); try { int input = s.nextInt(); switch (input) { case 1: LeeSearch(); break; case 2: selectAll(); // 전체 검색 break; case 3: InsertAll(); break; case 4: UpdateAll(); break; case 5: MemberDel(); break; case 6: System.out.println("이용해주셔서 감사!"); System.exit(0); } } catch (Exception e) { s.nextLine(); } } }
private void LeeSearch() { ResultSet rs = null; String sql = "select * from member where name like ?"; try (Connection con = MyConn.getConnection(); PreparedStatement pstmt = con.prepareStatement(sql);) { System.out.print("검색할 이름> "); String name = s.next(); String name1 = "%" + name + "%"; pstmt.setString(1, name1); rs = pstmt.executeQuery();
System.out.println("번호\t이름\t나이\t성별\t가입일"); while (rs.next()) { System.out.printf("%2s\t%4s\t%2s\t%1s\t%s\n", rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getDate(5)); } } catch (SQLException e) { e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
private void MemberDel() { String sql = "delete from member where no = ?"; try (Connection con = MyConn.getConnection(); PreparedStatement pstmt = con.prepareStatement(sql)) { System.out.print("몇번의 데이터를 지우시겠습니까!!"); int no = s.nextInt(); pstmt.setInt(1, no);
int result = pstmt.executeUpdate(); System.out.println(result + "개 의 행이 삭제 되었습니다.");
} catch (SQLException e) { e.printStackTrace(); } }
private void UpdateAll() { String sql = "update member set name = ?,age = ? where no = ?"; try (Connection con = MyConn.getConnection(); PreparedStatement pstmt = con.prepareStatement(sql);) {
System.out.print("수정할 번호> "); int no = s.nextInt();
System.out.print("이름 수정> "); String name = s.next();
System.out.print("나이 수정> "); int age = s.nextInt();
pstmt.setString(1, name); pstmt.setInt(2, age); pstmt.setInt(3, no);
int result = pstmt.executeUpdate(); System.out.println(result + "수정 되었습니다.");
} catch (SQLException e) { e.printStackTrace(); } }
private void InsertAll() { String sql = "insert into member(NO,NAME,AGE,GENDER) values(SEQ_MEMBER.NEXTVAL,?,?,?)"; try (Connection con = MyConn.getConnection(); PreparedStatement pstmt = con.prepareStatement(sql);) { System.out.print("이름> "); String name = s.next();
System.out.print("나이> "); int age = s.nextInt();
System.out.print("성별> ");
int gender = s.nextInt(); pstmt.setString(1, name); pstmt.setInt(2, age); pstmt.setInt(3, gender);
int result = pstmt.executeUpdate(); System.out.println(result + "몇 개의 행이 삽입 되었습니다."); } catch (SQLException e) { e.printStackTrace(); } }
/* * private void LeeSearch() { System.out.print("이름> "); String name = * s.next(); System.out.println("번호\t이름\t나이\t성별\t가입일"); String sql = * "select * from member where name like '%" + name + "%'"; * * try (Connection con = MyConn.getConnection(); Statement stmt = * con.createStatement(); ResultSet rs = stmt.executeQuery(sql);) { * * while (rs.next()) { System.out.printf("%2s\t%4s\t%2s\t%1s\t%s\n", * rs.getString(1), rs.getString(2), rs.getInt(3), rs.getString(4), * rs.getDate(5)); } } catch (SQLException e) { e.printStackTrace(); } } */
private void selectAll() { try (Connection con = MyConn.getConnection(); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from member");) {
System.out.println("번호\t이름\t나이\t성별\t가입일"); while (rs.next()) { System.out.printf("%2s\t%4s\t%2s\t%1s\t%s\n", rs.getString(1), rs.getString(2), rs.getInt(3), rs.getString(4), rs.getString(5)); } } catch (SQLException e) { e.printStackTrace(); } } }
|