Strpt 4 (DELETE) Done

This commit is contained in:
2026-09-30 13:39:33 +09:00
parent 0e5ccfe379
commit a5533aeb77
2 changed files with 64 additions and 4 deletions

View File

@@ -82,6 +82,7 @@ public class EmployeeDAO {
} }
return rtnList; return rtnList;
} }
//use selectById make the method //use selectById make the method
public List<EmployeeDTO> selectById(int empId) throws SQLException, ClassNotFoundException { public List<EmployeeDTO> selectById(int empId) throws SQLException, ClassNotFoundException {
//calling to DTO and new inout array //calling to DTO and new inout array
@@ -121,4 +122,21 @@ public class EmployeeDAO {
} }
return rtnList; return rtnList;
} }
public void empDelete(int empId) throws ClassNotFoundException, SQLException {
StringBuilder sql = new StringBuilder();
sql.append(" DELETE FROM ");
sql.append(" " + TBL_NAME);
sql.append(" WHERE ");
sql.append(" " + ID_EMPLOYEE + " = ? ");
try {
this.stmt = con.prepareStatement(sql.toString());
this.stmt.setInt(1, empId);
this.stmt.executeUpdate();
} finally {
DbUtil.closeStatement(this.stmt);
}
}
} }

View File

@@ -58,6 +58,7 @@ public class Main {
case "3": case "3":
break; break;
case "4": case "4":
empDelete();
break; break;
case "9": case "9":
return; return;
@@ -92,9 +93,18 @@ public class Main {
case "2": case "2":
// inputCheck 方法ကို သုံးပြီး ID ရိုက်ခိုင်းပါ (数値 check အတွက် true ထည့်ပါ) // inputCheck 方法ကို သုံးပြီး ID ရိုက်ခိုင်းပါ (数値 check အတွက် true ထည့်ပါ)
String strId = inputCheck("ID", true); String strId = inputCheck("ID", true);
int id = Integer.parseInt(strId); int id = Integer.parseInt(strId);
empList = empDao.selectById(id); empList = empDao.selectById(id);
break; break;
case "3":
break;
case "4":
// String empId = inputCheck("ID", true);
// int empID = Integer.parseInt(empId);
empDelete();
break;
default: default:
return; return;
} }
@@ -120,6 +130,35 @@ public class Main {
} }
} }
public static void empDelete() throws ClassNotFoundException, SQLException {
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
try {
// ID ရိုက်ထည့်ခိုင်းခြင်း (inputCheck ကို သုံးပါ)
String strId = inputCheck("ID", true);
int id = Integer.parseInt(strId);
//System.out.println("IDを入力してください");
// ID ရှိမရှိ စစ်ဆေးခြင်း (selectById ကို သုံးပါ)
List<EmployeeDTO> empList = empDao.selectById(id);
if (empList.isEmpty()) {
System.out.println("入力された値で情報がみつかりませんでした");
return;
}
// ဖျက်ရန် အတည်ပြုချက် (Confirmation) တောင်းခြင်း
System.out.println("指定されたIDの情報を削除します? Y/y");
String confirm = sc.nextLine();
if ("Y".equalsIgnoreCase(confirm)) {
empDao.empDelete(id);
System.out.println("削除が完了しました");
}
}finally {
DbUtil.closeConnection(empDao.con);
}
}
/** /**
* フラグを判定し、テーブル名を取得する * フラグを判定し、テーブル名を取得する
* *
@@ -169,4 +208,7 @@ public class Main {
} }
return str; return str;
} }
} }