본문 바로가기

프로그래밍/JDBC

JDBC 실습(2)

 

회원관리 프로그램
***************
* 1. 회원검색 *
* 2. 회원목록 *
* 3. 회원추가 *
* 4. 회원수정 *
* 5. 회원삭제 *
* 6. 끝내기   *
***************

 

meber 생성

 

CREATE TABLE MEMBER (
NO NUMBER(10) CONSTRAINT PK_MEMBER_NO PRIMARY KEY,
NAME VARCHAR2(100) CONSTRAINT NN_MEBER_NAME NOT NULL,
AGE NUMBER(3),
GENDER CHAR(1),  --0: 여자 1: 남자
HDATE DATE DEFAULT SYSDATE
); 

 

INSERT INTO MEMBER(NO,NAME,AGE,GENDER)
VALUES (SEQ_MEMBER.NEXTVAL,'최반',28,1);

INSERT INTO MEMBER(NO,NAME,AGE,GENDER)
VALUES (SEQ_MEMBER.NEXTVAL,'최가',21,0);
commit;

 

Myconn.java

 ( 드라이버 연결시켜주는 파일 )

package conn;

import java.sql.*;

public class MyConn {
 /**
  * scott 계정 커넥션 메소드
  * @return java.sql.Connection
  * @throws SQLException
  */
 public static Connection getConnection() throws SQLException {
  try {
   Class.forName("oracle.jdbc.OracleDriver");
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
   System.out.println("ojdbc6.jar 파일이 필요합니다.");
  }
  String url = "jdbc:oracle:thin:@localhost:1521:xe";
  return DriverManager.getConnection(url, "scott", "tiger");
 }

 

 

MemberManager.java

 

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();
  }
 }
}

'프로그래밍 > JDBC' 카테고리의 다른 글

SQL 마이너스 만들기  (0) 2012.07.17
JDBC 초기 설정  (0) 2012.07.16
JDBC 실습(1)  (0) 2012.06.20
JDBC 레코드 검색  (0) 2012.06.20
JDBC 실행 순서 정리  (0) 2012.06.20