💡 Statment 를 사용한 학생 관리 프로그램
✔️ model.vo
package com.kh.jdbc.day02.student.model.vo;
import java.sql.Date;
public class Student {
private String studentId;
private String studentPwd;
private String studentName;
private char gender;
private int age;
private String email;
private String phone;
private String address;
private String hobby;
private Date enrollDate;
public Student() {}
public Student(String studentId, String studentPwd, String studentName, char gender, int age, String email,
String phone, String address, String hobby) {
super();
this.studentId = studentId;
this.studentPwd = studentPwd;
this.studentName = studentName;
this.gender = gender;
this.age = age;
this.email = email;
this.phone = phone;
this.address = address;
this.hobby = hobby;
}
public Student(String studentId, String studentPwd, String email, String phone, String address, String hobby) {
super();
this.studentId = studentId;
this.studentPwd = studentPwd;
this.email = email;
this.phone = phone;
this.address = address;
this.hobby = hobby;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getStudentPwd() {
return studentPwd;
}
public void setStudentPwd(String studentPwd) {
this.studentPwd = studentPwd;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public Date getEnrollDate() {
return enrollDate;
}
public void setEnrollDate(Date enrollDate) {
this.enrollDate = enrollDate;
}
@Override
public String toString() {
return "학생 [아이디 =" + studentId + ", 비밀번호 =" + studentPwd + ", 이름 =" + studentName
+ ", 성별 =" + gender + ", 나이 =" + age + ", 이메일 =" + email + ", 전화번호=" + phone + ", 주소 ="
+ address + ", 취미 =" + hobby + ", 가입날짜 =" + enrollDate + "]";
}
}
✔️ run
package com.kh.jdbc.day02.student.run;
import com.kh.jdbc.day02.student.view.StudentView;
public class StudentRun {
public static void main(String[] args) {
StudentView view = new StudentView();
view.startProgram();
}
}
✔️ view
package com.kh.jdbc.day02.student.view;
import java.util.*;
import com.kh.jdbc.day02.student.controller.StudentController;
import com.kh.jdbc.day02.student.model.vo.Student;
public class StudentView {
private StudentController controller;
public StudentView() {
controller = new StudentController();
}
public void startProgram() {
Student student = null;
List<Student> sList = null;
finish:
while(true) {
int choice = printMenu();
switch(choice) {
case 1 :
sList = controller.printStudentList();
if(!sList.isEmpty()) {
showAllStudents(sList);
} else {
displayError("학생 정보가 조회되지 않습니다.");
}
break;
case 2 :
String studentId = inputStudentId();
student = controller.printStudentById(studentId);
if(student != null) {
showStudent(student);
}else {
displayError("학생 정보가 존재하지 않습니다.");
}
break;
case 3 :
String studentName = inputStudentName();
sList = controller.printStudentsByName(studentName);
if(!sList.isEmpty()) {
showAllStudents(sList);
} else {
displayError("학생 정보가 조회되지 않습니다.");
}
break;
case 4 :
student = inputStudent();
int result = controller.insertStudent(student);
if(result > 0) {
displaySuccess("학생 정보 등록 성공!");
} else {
displayError("학생 정보 등록 실패");
}
break;
case 5 :
student = mondifyStudent();
result = controller.modifyStudent(student);
if(result > 0) {
displaySuccess("학생 정보 수정 성공!");
} else {
displayError("학생 정보 수정 실패");
}
break;
case 6 :
studentId = inputStudentId();
result = controller.deleteStudent(studentId);
if(result > 0) {
displaySuccess("학생 정보 삭제 성공!");
} else {
displayError("학생 정보 삭제 실패");
}
break;
case 0 :
break finish;
}
}
}
public int printMenu() {
Scanner sc = new Scanner(System.in);
System.out.println("===== 학생관리 프로그램 =====");
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("0. 프로그램 종료");
System.out.print("메뉴 선택 : ");
int input = sc.nextInt();
return input;
}
private String inputStudentId() {
Scanner sc = new Scanner(System.in);
System.out.println("===== 학생 아이디로 조회 =====");
System.out.print("학생 아이디 입력 : ");
String studentId = sc.next();
return studentId;
}
private String inputStudentName() {
Scanner sc = new Scanner(System.in);
System.out.println("===== 학생 이름으로 조회");
System.out.print("학생 이름 입력 : ");
String studentName = sc.next();
return studentName;
}
private Student inputStudent() {
Scanner sc = new Scanner(System.in);
System.out.print("아이디 : ");
String studentId = sc.next();
System.out.print("비밀번호 : ");
String studentPw = sc.next();
System.out.print("이름 : ");
String studentName = sc.next();
System.out.print("성별 : ");
char gender = sc.next().charAt(0);
System.out.print("나이 : ");
int age = sc.nextInt();
System.out.print("이메일 : ");
String email = sc.next();
System.out.print("전화번호 : ");
String phone = sc.next();
System.out.print("주소 : ");
sc.nextLine();
String address = sc.nextLine();
System.out.print("취미(,로 구분) : ");
String hobby = sc.nextLine();
Student student = new Student(studentId, studentPw, studentName, gender, age, email, phone, address, hobby);
return student;
}
private Student mondifyStudent() {
Scanner sc = new Scanner(System.in);
System.out.println("===== 학생 정보 수정 =====");
System.out.print("아이디 : ");
String studentId = sc.next();
System.out.print("비밀번호 : ");
String studentPw = sc.next();
System.out.print("이메일 : ");
String email = sc.next();
System.out.print("전화번호 : ");
String phone = sc.next();
System.out.print("주소 : ");
sc.nextLine();
String address = sc.nextLine();
System.out.print("취미(,로 구분) : ");
String hobby = sc.next();
Student student = new Student(studentId, studentPw, email, phone, address, hobby);
return student;
}
private void displaySuccess(String message) {
System.out.println("[서비스 성공] "+message);
}
private void displayError(String message) {
System.out.println("[서비스 실패] "+message);
}
private void showAllStudents(List<Student> sList) {
System.out.println("===== 학생 전체 정보 출력 =====");
for(Student student : sList) {
System.out.printf("이름 : %s, 나이 : %d, 아이디: %s, 성별 %s, 이메일 : %s, 전화번호 : %s, 주소 : %s, 취미 : %s, 가입날짜 : %s\n"
, student.getStudentName()
, student.getAge()
, student.getStudentId()
, student.getGender()
, student.getEmail()
, student.getPhone()
, student.getAddress()
, student.getHobby()
, student.getEnrollDate());
}
}
private void showStudent(Student student) {
System.out.println("===== 학생 정보 출력(아이디로 조회) ======");
System.out.printf("이름 : %s, 나이 : %d, 아이디: %s, 성별 %s, 이메일 : %s, 전화번호 : %s, 주소 : %s, 가입날짜 : %s\n"
, student.getStudentName()
, student.getAge()
, student.getStudentId()
, student.getGender()
, student.getEmail()
, student.getPhone()
, student.getAddress()
, student.getEnrollDate());
}
}
✔️ controller
package com.kh.jdbc.day02.student.controller;
import java.util.List;
import com.kh.jdbc.day02.student.model.dao.StudentDAO;
import com.kh.jdbc.day02.student.model.vo.Student;
public class StudentController {
StudentDAO studentDao;
public StudentController() {
studentDao = new StudentDAO();
}
public List<Student> printStudentList() {
List<Student> sList = studentDao.selectAll();
return sList;
}
public Student printStudentById(String studentId) {
Student student = studentDao.selectOneById(studentId);
return student;
}
public List<Student> printStudentsByName(String studentName) {
List<Student> sList = studentDao.selectAllByName(studentName);
return sList;
}
public int insertStudent(Student student) {
int result = studentDao.insertStudent(student);
return result;
}
public int modifyStudent(Student student) {
int result = studentDao.updateStudent(student);
return result;
}
public int deleteStudent(String studentId) {
int result = studentDao.deleteStudent(studentId);
return result;
}
}
✔️ model.dao
package com.kh.jdbc.day02.student.model.dao;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import com.kh.jdbc.day02.student.model.vo.Student;
public class StudentDAO {
private final String DRIVER_NAME = "oracle.jdbc.driver.OracleDriver";
private final String URL = "jdbc:oracle:thin:@localhost:1521:xe";
private final String USER = "STUDENT";
private final String PASSWORD = "STUDENT";
public List<Student> selectAll() {
String query = "SELECT * FROM STUDENT_TBL";
List<Student> sList = null;
Student student = null;
try {
Class.forName(DRIVER_NAME);
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery(query);
sList = new ArrayList<Student>();
while(rset.next()) {
student = rsetToStudent(rset);
sList.add(student);
}
rset.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return sList;
}
public List<Student> selectAllByName(String studentName) {
String query = "SELECT * FROM STUDENT_TBL WHERE STUDENT_NAME ='" +studentName+"'";
Student student = null;
List<Student> sList = new ArrayList<Student>();
try {
Class.forName(DRIVER_NAME);
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery(query);
while(rset.next()) {
student = rsetToStudent(rset);
sList.add(student);
}
rset.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return sList;
}
public Student selectOneById(String studentId) {
String query = "SELECT * FROM STUDENT_TBL WHERE STUDENT_ID ='" + studentId + "'";
Student student = null;
try {
Class.forName(DRIVER_NAME);
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery(query);
if(rset.next()) {
student = rsetToStudent(rset);
}
rset.close();
stmt.close();
conn.close();
} catch (Exception e) {
}
return student;
}
public int insertStudent(Student student) {
String query = "INSERT INTO STUDENT_TBL VALUES("
+ "'"+student.getStudentId()+"', "
+ "'"+student.getStudentPwd()+"', "
+ "'"+student.getStudentName()+"', "
+ "'"+student.getGender()+"', "
+ student.getAge()+", "
+ "'"+student.getEmail()+"', "
+ "'"+student.getPhone()+"', "
+ "'"+student.getAddress()+"', "
+ "'"+student.getHobby()+"', "
+ "SYSDATE)";
int result = -1;
try {
Class.forName(DRIVER_NAME);
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
Statement stmt = conn.createStatement();
result = stmt.executeUpdate(query);
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public int updateStudent(Student student) {
String query = "UPDATE STUDENT_TBL SET "
+ "STUDENT_PWD = '"+ student.getStudentPwd()+"', "
+ "EMAIL = '"+ student.getEmail()+"', "
+ "PHONE = '"+ student.getPhone()+"', "
+ "ADDRESS = '"+ student.getAddress()+"', "
+ "HOBBY = '"+ student.getHobby()+"' "
+ "WHERE STUDENT_ID ='" + student.getStudentId()+"'";
int result = -1;
try {
Class.forName(DRIVER_NAME);
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
Statement stmt = conn.createStatement();
result = stmt.executeUpdate(query);
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public int deleteStudent(String studentId) {
String query = "DELETE FROM STUDENT_TBL WHERE STUDENT_ID ='"+ studentId+"'" ;
int result = 0;
try {
Class.forName(DRIVER_NAME);
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
Statement stmt = conn.createStatement();
result = stmt.executeUpdate(query);
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
private Student rsetToStudent(ResultSet rset) throws SQLException {
Student student = new Student();
student.setStudentId(rset.getString("STUDENT_ID"));
student.setStudentPwd(rset.getString("STUDENT_PWD"));
student.setStudentName(rset.getString("STUDENT_NAME"));
student.setGender(rset.getString("GENDER").charAt(0));
student.setAge(rset.getInt("AGE"));
student.setEmail(rset.getString("EMAIL"));
student.setPhone(rset.getString("PHONE"));
student.setAddress(rset.getString("ADDRESS"));
student.setHobby(rset.getString("HOBBY"));
student.setEnrollDate(rset.getDate("ENROLL_DATE"));
return student;
}
}
정보 감사합니다.