[ java ] MVC - 학생관리 프로그램 - jdbc 연동 - 학생등록, 전체조회 , 특정학생조회, 학생정보수정, 학생삭제, 종료
페이지 정보
작성자 웹지기 댓글 2건 조회 6,026회 작성일 20-12-24 17:26본문
MVC - 학생관리 프로그램 - jdbc 연동 - 학생등록, 전체조회 , 특정학생조회, 학생정보수정, 학생삭제, 종료
VO(VTO) java와 VAO는 댓글에 추가
StudentMain.java -----------------
package com.VO;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//switch 에서는 변수가 공유 되므로 switch문 안에서 변수 선언하지 않고 상단에서 선언한다.
int menu = 0;
int student_num = 0;
String name = "";
int age = 0;
String phone = "";
String email = "";
String search = "";
StudentDAO dao = null;
StudentVO vo = null;
boolean result = false;
System.out.println("===============SMHRD학생관리프로그램===============");
while(true) {
System.out.println("1.학생추가 2.전체명단조회 3.특정학생조회 4.학생정보수정 5.학생삭제 6.프로그램종료");
System.out.print("메뉴 선택 >> ");
menu = sc.nextInt();
switch(menu) {
case 1 : //학생등록
System.out.println(menu+"번 선택");
System.out.println("등록할 학생의 정보를 입력하시오.");
System.out.print("이름 : ");
name = sc.next();
System.out.print("나이 : ");
age = sc.nextInt();
System.out.print("전화번호 : ");
phone = sc.next();
System.out.print("이메일 : ");
email = sc.next();
dao = new StudentDAO();
result = dao.insertStd(name, age, phone, email);
if(result ==true) {
System.out.println("학생등록 성공");
} else {
System.out.println("학생등록 실팽");
}
System.out.println();
break;
case 2 : //전체학생 조회
System.out.println(menu+"번 선택");
//전체 학생 조회
System.out.println("===============SMHRD학생목록===============");
dao = new StudentDAO();
ArrayList<StudentVO> al = dao.selectStd();
System.out.print("고유번호\t");
System.out.print("이름\t");
System.out.print("나이\t");
System.out.print("전번\t\t");
System.out.print("이메일\t");
System.out.println();
for(int i=0; i<al.size(); i++) {
vo = al.get(i);
System.out.print(vo.getStudent_num()+"\t");
System.out.print(vo.getName()+"\t");
System.out.print(vo.getAge()+"\t");
System.out.print(vo.getPhone()+"\t\t");
System.out.print(vo.getEmail()+"\t");
System.out.println();
}
System.out.println();
break;
case 3 : //특정학생조회
System.out.println(menu+"번 선택");
System.out.print("학생이름 입력 : ");
search = sc.next();
System.out.println("=============== "+search+" ===============");
dao = new StudentDAO();
vo = dao.oneSelectStd(search);
if(vo.getStudent_num() > 0) {
System.out.print(vo.getStudent_num()+"\t");
System.out.print(vo.getName()+"\t");
System.out.print(vo.getAge()+"\t");
System.out.print(vo.getPhone()+"\t");
System.out.print(vo.getEmail()+"\t");
System.out.println();
} else {
System.out.println("등록된 정보가 없습니다.");
}
System.out.println("=============== ======== ===============");
System.out.println();
break;
case 4 : //학생정보수정
System.out.println(menu+"번 선택");
System.out.print("변경할 학생이름 : ");
search = sc.next();
dao = new StudentDAO();
vo = dao.oneSelectStd(search);
if(vo.getStudent_num() > 0) {
student_num = vo.getStudent_num();
name = vo.getName();
age = vo.getAge();
phone = vo.getPhone();
email = vo.getEmail();
//뭘 변경할건지 물어볼꺼야.
System.out.print("변경하실 내용 : 선택 [1]이름, [2]나이, [3]연락처, [4]이메일, [5]전체변경 :");
int sel = sc.nextInt();
if(sel == 1) {
System.out.print("변경할 이름 : ");
name = sc.next();
} else if(sel == 2) {
System.out.print("변경할 나이 : ");
age = sc.nextInt();
} else if(sel == 3) {
System.out.print("변경할 연락처 : ");
phone = sc.next();
} else if(sel == 4) {
System.out.print("변경할 이메일 : ");
email = sc.next();
} else if(sel == 5) {
System.out.print("변경할 이름 : ");
name = sc.next();
System.out.print("변경할 나이 : ");
age = sc.nextInt();
System.out.print("변경할 연락처 : ");
phone = sc.next();
System.out.print("변경할 이메일 : ");
email = sc.next();
}
dao = new StudentDAO();
result = dao.updateStd(student_num, name, age, phone, email);
if(result ==true) {
System.out.println("학생등록 성공");
} else {
System.out.println("학생등록 실패");
}
} else {
System.out.println("등록된 정보가 없습니다.");
}
System.out.println();
break;
case 5 : //학생정보삭제
System.out.println(menu+"번 선택");
System.out.print("삭제할 학생 이름을 입력하세요 : ");
search = sc.next();
dao = new StudentDAO();
vo = dao.oneSelectStd(search);
if(vo.getStudent_num() > 0) {
student_num = vo.getStudent_num();
name = vo.getName();
age = vo.getAge();
phone = vo.getPhone();
email = vo.getEmail();
dao = new StudentDAO();
result = dao.deleteStd(student_num);
if(result ==true) {
System.out.println("학생삭제 성공");
} else {
System.out.println("학생삭제 실패");
}
} else {
System.out.println("등록된 정보가 없습니다.");
}
System.out.println();
break;
case 6 : //프로그램 종료
System.out.println(menu+"번 선택");
System.out.println();
break;
}
if(menu == 6) {
System.out.println("프로그램종료");
break;
}
}
sc.close();
}
}
댓글목록
웹지기님의 댓글
웹지기 작성일
StudentDAO.java (DAO) ----------------
package com.VO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class StudentDAO {
//DAO (Data Access Object)
//데이터베이스 관련 작업전담 클래스
//등록, 수정, 조회, 삭제 등의 작업을 하는 클래스
//CRUD ( CREATE, READ, UPDATE, DELETE ) 작업
private Connection conn = null;
private PreparedStatement pst = null;
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String user = "hr";
private String password = "hr";
private int student_num = 0;
private String name = "";
private int age = 0;
private String phone = "";
private String email = "";
private String sql = "";
private boolean result = false;
private StudentVO vo = null;
private ResultSet rs = null;
public void connect() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, user, password);
} catch(Exception e) {
e.printStackTrace();
}
}
public void close(ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void close(PreparedStatement pst) {
if(pst != null) {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void close(Connection conn) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//학생등록
public boolean insertStd(String name, int age, String phone, String email) {
try {
connect();
sql = " INSERT INTO student VALUES (studentSeq.nextval, ?, ?, ?, ?) ";
pst = conn.prepareStatement(sql);
pst.setString(1,name);
pst.setInt(2, age);
pst.setString(3, phone);
pst.setString(4, email);
int cnt = pst.executeUpdate();
if(cnt > 0) {
result = true;
} else {
result = false;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close(pst);
close(conn);
}
return result;
}
public ArrayList<StudentVO> selectStd() {
ArrayList<StudentVO> al = new ArrayList<StudentVO>();
try {
connect();
sql = " SELECT ROWNUM, A.* FROM student A ORDER BY student_num DESC ";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next()) {
//int row = rs.getRow();
//student_num = rs.getInt("student_num");
student_num = rs.getInt(2);
//name = rs.getString("name");
name = rs.getString(3);
//age = rs.getInt("age");
age = rs.getInt(4);
//phone = rs.getString("phone");
phone = rs.getString(5);
//email = rs.getString("email");
email = rs.getString(6);
StudentVO vo = new StudentVO(student_num, name, age, phone, email);
al.add(vo);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close(rs);
close(pst);
close(conn);
}
return al;
}
public StudentVO oneSelectStd(String search) {
try {
connect();
sql = " SELECT * FROM student WHERE name = ? ";
pst = conn.prepareStatement(sql);
pst.setString(1, search);
rs = pst.executeQuery();
boolean isList = false;
while(rs.next()) {
student_num = rs.getInt("student_num");
name = rs.getString("name");
age = rs.getInt("age");
phone = rs.getString("phone");
email = rs.getString("email");
vo = new StudentVO(student_num, name, age, phone, email);
isList = true;
}
if(isList == false) {
vo = new StudentVO();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close(rs);
close(pst);
close(conn);
}
return vo;
}
public boolean updateStd(int student_num, String name, int age, String phone, String email) {
try {
connect();
sql = " UPDATE student ";
sql += " SET ";
sql += " name = ?, ";
sql += " age = ?, ";
sql += " phone = ?, ";
sql += " email = ? ";
sql += " WHERE student_num = ? ";
pst = conn.prepareStatement(sql);
pst.setString(1,name);
pst.setInt(2, age);
pst.setString(3, phone);
pst.setString(4, email);
pst.setInt(5, student_num);
int cnt = pst.executeUpdate();
if(cnt > 0) {
result = true;
} else {
result = false;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close(pst);
close(conn);
}
return result;
}
public boolean deleteStd(int student_num) {
try {
connect();
sql = " DELETE FROM student WHERE student_num = ? ";
pst = conn.prepareStatement(sql);
pst.setInt(1, student_num);
int cnt = pst.executeUpdate();
if(cnt > 0) {
result = true;
} else {
result = false;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close(pst);
close(conn);
}
return result;
}
}
웹지기님의 댓글
웹지기 작성일
StudentVO.java(VTO) --------------------
package com.VO;
public class StudentVO {
private int student_num;
private String name;
private int age;
private String phone;
private String email;
public StudentVO(int student_num, String name, int age, String phone, String email) {
super();
this.student_num = student_num;
this.name = name;
this.age = age;
this.phone = phone;
this.email = email;
}
public StudentVO() {
// TODO Auto-generated constructor stub
}
public int getStudent_num() {
return student_num;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getPhone() {
return phone;
}
public String getEmail() {
return email;
}
}