4段階目(DAOのデータ削除メソッドを作成→削除処理完成)

This commit is contained in:
m-shimizu
2026-09-30 16:19:18 +09:00
parent 00cf2096e2
commit ae6a693aeb
2 changed files with 45 additions and 0 deletions

View File

@@ -107,4 +107,20 @@ public class EmployeeDAO {
return rtnList;
}
//社員データ削除
public void empDelete(int id) throws SQLException {
StringBuilder sql = new StringBuilder();
sql.append(" DELETE ");
sql.append(" FROM ");
sql.append(TBL_NAME);
sql.append(" WHERE ID_EMPLOYEE = ? ");
try {
this.stmt = con.prepareStatement(sql.toString());
this.stmt.setInt(1, id);
this.stmt.executeUpdate();
} finally {
DbUtil.closeStatement(this.stmt);
}
}
}

View File

@@ -58,6 +58,7 @@ public class Main {
case "3":
break;
case "4":
empDelete();
break;
case "9":
return;
@@ -113,6 +114,34 @@ public class Main {
}
}
//社員テーブルから情報を削除する
public static void empDelete()
throws ClassNotFoundException, SQLException {
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
String id = inputCheck("ID", true);
try {
List<EmployeeDTO> empList = empDao.selectById(Integer.parseInt(id));
if (empList.isEmpty()) {
System.out.println("入力されて値で情報がみつかりませんでした");
} else {
System.out.println("指定されたIDの情報を削除しますか? Y/y");
System.out.println("Y/y以外の入力であればキャンセル");
String confirm = sc.nextLine();
if (confirm.equalsIgnoreCase("y")) {
empDao.empDelete(Integer.parseInt(id));
System.out.println("削除が完了しました");
}
}
} finally {
DbUtil.closeConnection(con);
}
}
/**
* フラグを判定し、テーブル名を取得する
*