package day01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
class CostomerInfo {
int cnum;
String cname;
String phone;
String cadd;
CostomerInfo(int cnum, String cname, String phone, String cadd) {
this.cnum = cnum;
this.cname = cname;
this.phone = phone;
this.cadd = cadd;
}
@Override
public String toString() {
return "[고객 번호 : " + cnum + ", 이름 : " + cname + ", 전화번호 : " + phone + ", 주소 : " + cadd + "]\n";
}
}
public class CostomInfo {
public static void main(String[] args) {
// DB정보
String DName = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/leedb";
String user = "root";
String password = "1234";
// DB정의?
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
// 할일
String sqlR = "select * from costomerInfo";
// 필요한것
Scanner sc = new Scanner(System.in);
ArrayList<CostomerInfo> data = new ArrayList();
try {
Class.forName(DName);
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
while (true) {
rs = stmt.executeQuery(sqlR);
printScreen();
int choice = 0;
try {
choice = sc.nextInt();
} catch (InputMismatchException e) {
sc.next();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
if (choice == 1) { // 고객정보추가
System.out.print("고객번호를 입력하세요 : ");
int cnum = 0;
try {
cnum = sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("잘못입력하셧습니다. 처음으로 돌아갑니다.");
sc.next();
continue;
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
System.out.print("고객이름을 입력하세요 : ");
String cname = sc.next();
System.out.print("전화번호를 입력하세요 : ");
String phone = sc.next();
System.out.print("주소를 입력하세요 : ");
sc.nextLine();
String cadd = sc.nextLine();
String sqlC = "insert into costomerInfo values(" + cnum + ", '" + cname + "', '" + phone + "', '"
+ cadd + "')";
stmt.executeUpdate(sqlC);
continue;
// -----------------------------------------------------------------------
} else if (choice == 2) { // 고객 정보 변경
System.out.println("변경하시려는 고객님의 고객번호를 입력해주세요.");
int cnum = sc.nextInt();
System.out.println("1. 고객 이름 변경");
System.out.println("2. 전화번호 변경");
System.out.println("3. 고객 주소 변경");
System.out.print("변경하실 항목을 선택하세요 : ");
choice = sc.nextInt();
if (choice == 1) { // 이름변경
System.out.print("어떤 이름으로 변경하시겠습니까? : ");
String cname = sc.next();
String sqlU = "update costomerInfo set cname = '" + cname + "' where cnum = " + cnum;
stmt.executeUpdate(sqlU);
System.out.println("변경 완료");
} else if (choice == 2) { // 전화번호변경
System.out.print("변경할 전화번호를 입력해주세요 : ");
String phone = sc.next();
String sqlU = "update costomerInfo set phone = '" + phone + "' where cnum = " + cnum;
stmt.executeUpdate(sqlU);
System.out.println("변경 완료");
} else if (choice == 3) { // 주소변경
System.out.print("변경할 주소를 입력해주세요 : ");
String phone = sc.nextLine();
String sqlU = "update costomerInfo set phone = '" + phone + "' where cnum = " + cnum;
stmt.executeUpdate(sqlU);
System.out.println("변경 완료");
} else {
System.out.println("잘못입력하셧습니다.");
continue;
}
// -----------------------------------------------------------------------
} else if (choice == 3) { // 고객 정보 삭제
System.out.print("삭제하려는 고객의 고객번호를 입력하세요. : ");
int cnum = 0;
try {
cnum = sc.nextInt();
} catch (InputMismatchException e) {
sc.next();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
String sqlD = "delete from costomerInfo where cnum = " + cnum;
stmt.executeUpdate(sqlD);
// -----------------------------------------------------------------------
} else if (choice == 4) { // 고객 정보 출력
System.out.println("============= 고객 정보 =============");
try {
while (rs.next()) {
System.out.print("번호 : " + rs.getInt("cnum") + " 이름 : " + rs.getString("cname")
+ " 전화번호 : " + rs.getString("phone") + " 주소 : " + rs.getString("cadd"));
System.out.println();
}
} catch (NullPointerException e) { // 등록 고객이 없을때
System.out.println("등록된 고객이 없습니다.");
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
} finally {
System.out.println("==================================");
}
// -----------------------------------------------------------------------
} else if (choice == 5) { // 고객 정보 검색
System.out.print("검색하려는 고객의 고객번호를 입력하세요. : ");
int cnum = 0;
try {
cnum = sc.nextInt();
sqlR = "select * from costomerInfo where cnum = " + cnum;
rs = stmt.executeQuery(sqlR);
} catch (InputMismatchException e) {
sc.next();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
while (rs.next()) {
System.out.print("번호 : " + rs.getInt("cnum") + " 이름 : " + rs.getString("cname") + " 전화번호 : "
+ rs.getString("phone") + " 주소 : " + rs.getString("cadd"));
System.out.println();
}
// -----------------------------------------------------------------------
} else if (choice == 6) { // 종료
System.out.println("안녕히가세요!");
break;
} else { // 유효성검사
System.out.println("잘못입력하셧습니다.");
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
// rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
static void printScreen() {
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.print("원하시는 번호를 입력해 주세요 : ");
}
}