diff --git a/src/com/rhizome/example/dao/EmployeeDAO.java b/src/com/rhizome/example/dao/EmployeeDAO.java index 36c9fcb..2d0bcd4 100644 --- a/src/com/rhizome/example/dao/EmployeeDAO.java +++ b/src/com/rhizome/example/dao/EmployeeDAO.java @@ -114,4 +114,24 @@ public class EmployeeDAO { } + public void empDelete(int id) throws SQLException { + StringBuilder sql = new StringBuilder(); + //社員テーブルから指定された社員IDに一致するデータを取得 + sql.append(" DELETE "); + sql.append(" " + "*"); + sql.append(" FROM "); + sql.append(" " + TBL_NAME); + sql.append(" WHERE "); + sql.append(" " + ID_EMPLOYEE); + sql.append(" = "); + sql.append(" " + id); + try { + this.stmt = con.prepareStatement(sql.toString()); + this.stmt.executeUpdate(); + System.out.println("削除が完了しました"); + } finally { + DbUtil.closeStatement(this.stmt); + + } + } } diff --git a/src/com/rhizome/example/main/Main.java b/src/com/rhizome/example/main/Main.java index df17f97..a38c8bc 100644 --- a/src/com/rhizome/example/main/Main.java +++ b/src/com/rhizome/example/main/Main.java @@ -45,6 +45,8 @@ public class Main { * @throws SQLException */ public void start() throws ClassNotFoundException, SQLException { + Connection con = DbUtil.getConnection(); + EmployeeDAO empDao = new EmployeeDAO(con); System.out.println(); System.out.println("現在のメンテナンス対象テーブル:" + targetTableName(tblFlg)); System.out.println("1:検索 2:登録 3:更新 4:削除 9:テーブル切替 0:終了"); @@ -58,6 +60,7 @@ public class Main { case "3": break; case "4": + empDelete(); break; case "9": return; @@ -70,6 +73,30 @@ public class Main { } } + private void empDelete() throws SQLException { + Connection con = DbUtil.getConnection(); + EmployeeDAO empDao = new EmployeeDAO(con); + try { + String strId = inputCheck("ID", true); + int id = Integer.parseInt(strId); + List empList = empDao.selectByld(id); + if (empList.isEmpty()) { + } else { + System.out.println("指定されたIDの情報を削除します? Y/y"); + System.out.println("Y/y以外の入力であればキャンセル"); + Scanner sc = new Scanner(System.in); + String str = sc.nextLine(); + if (str.equals("Y") || str.equals("y")) { + empDao.empDelete(id); + } else { + System.out.println("キャンセルしました"); + } + } + } finally { + DbUtil.closeConnection(empDao.con); + } + } + /** * 社員テーブルから情報を検索する * @@ -89,7 +116,7 @@ public class Main { break; case "2": System.out.println("IDを入力してください"); - int id =sc.nextInt(); + int id = sc.nextInt(); empList = empDao.selectByld(id); break; default: @@ -131,7 +158,7 @@ public class Main { * @return */ public static String inputCheck(String titleName, boolean flg) { - String str = null; + String str; while (true) { System.out.println(titleName + "を入力してください"); str = sc.nextLine();