From a5533aeb775324330816e473ee20031daeac31d9 Mon Sep 17 00:00:00 2001 From: "h.hein" <26-h-kyaw-sakuracommunication@it-college.co.jp> Date: Wed, 30 Sep 2026 13:39:33 +0900 Subject: [PATCH] Strpt 4 (DELETE) Done --- src/com/rhizome/example/dao/EmployeeDAO.java | 18 +++++++ src/com/rhizome/example/main/Main.java | 50 ++++++++++++++++++-- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/com/rhizome/example/dao/EmployeeDAO.java b/src/com/rhizome/example/dao/EmployeeDAO.java index 4e2664d..29e0080 100644 --- a/src/com/rhizome/example/dao/EmployeeDAO.java +++ b/src/com/rhizome/example/dao/EmployeeDAO.java @@ -82,6 +82,7 @@ public class EmployeeDAO { } return rtnList; } + //use selectById make the method public List selectById(int empId) throws SQLException, ClassNotFoundException { //calling to DTO and new inout array @@ -121,4 +122,21 @@ public class EmployeeDAO { } 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); + } + + } } \ No newline at end of file diff --git a/src/com/rhizome/example/main/Main.java b/src/com/rhizome/example/main/Main.java index 4e28ce9..fd7ba46 100644 --- a/src/com/rhizome/example/main/Main.java +++ b/src/com/rhizome/example/main/Main.java @@ -58,6 +58,7 @@ public class Main { case "3": break; case "4": + empDelete(); break; case "9": return; @@ -92,9 +93,18 @@ public class Main { case "2": // inputCheck 方法ကို သုံးပြီး ID ရိုက်ခိုင်းပါ (数値 check အတွက် true ထည့်ပါ) String strId = inputCheck("ID", true); - int id = Integer.parseInt(strId); - empList = empDao.selectById(id); - break; + int id = Integer.parseInt(strId); + empList = empDao.selectById(id); + break; + case "3": + break; + + case "4": +// String empId = inputCheck("ID", true); +// int empID = Integer.parseInt(empId); + empDelete(); + break; + default: return; } @@ -104,7 +114,7 @@ public class Main { System.out.println("入力された値で情報がみつかりませんでした"); return; } - + for (EmployeeDTO emp : empList) { //for (String emp : empList) { //System.out.println(emp); @@ -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 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; } + + + }