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

This commit is contained in:
s-sakiyama
2026-09-30 12:39:56 +09:00
parent 1aade1c6ed
commit a6bd282b1c
2 changed files with 88 additions and 3 deletions

View File

@@ -12,7 +12,6 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Scanner;
import com.rhizome.example.dto.EmployeeDTO; import com.rhizome.example.dto.EmployeeDTO;
import com.rhizome.example.util.DbUtil; import com.rhizome.example.util.DbUtil;
@@ -75,7 +74,8 @@ public class EmployeeDAO {
/** /**
* 社員テーブルから社員IDに合致する情報を取得する * 社員テーブルから社員IDに合致する情報を取得する
* * @param id
* 検索する社員ID
* @return 検索結果 * @return 検索結果
* @throws SQLException * @throws SQLException
* @throws ClassNotFoundException * @throws ClassNotFoundException
@@ -84,7 +84,6 @@ public class EmployeeDAO {
public List<EmployeeDTO> selectById​(int id) public List<EmployeeDTO> selectById​(int id)
throws java.sql.SQLException, java.lang.ClassNotFoundException, java.lang.NumberFormatException { throws java.sql.SQLException, java.lang.ClassNotFoundException, java.lang.NumberFormatException {
List<EmployeeDTO> rtnList = new ArrayList<>(); List<EmployeeDTO> rtnList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
StringBuilder sql = new StringBuilder(); StringBuilder sql = new StringBuilder();
sql.append(" SELECT "); sql.append(" SELECT ");
sql.append(" " + "*"); sql.append(" " + "*");
@@ -114,4 +113,34 @@ public class EmployeeDAO {
} }
return rtnList; return rtnList;
} }
/**
* 社員テーブルの社員IDに合致する情報を削除する
*
* @param id
* 削除ID
* @throws ClassNotFoundException
* @throws SQLException
* @throws NumberFormatException
*/
public void empDelete​(int id)
throws java.lang.ClassNotFoundException, java.sql.SQLException, java.lang.NumberFormatException {
StringBuilder sql = new StringBuilder();
sql.append(" DELETE ");
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());
stmt.executeUpdate();
System.out.println("削除が完了しました");
} 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;
@@ -70,6 +71,39 @@ public class Main {
} }
} }
/**
* 社員テーブルの社員IDに合致する情報を削除する
*
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void empDelete​()
throws java.lang.ClassNotFoundException, java.sql.SQLException, java.lang.NumberFormatException {
List<EmployeeDTO> rtnList = new ArrayList<>();
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
int id = Integer.parseInt(inputCheck("ID", true));
rtnList = empDao.selectById​(id);
if (rtnList.size() == 0) {
return;
}
System.out.println("指定されたIDの情報を削除します? Y/y\r\n"
+ "(Y/y以外の入力であればキャンセル)");
String str = inputCheck(false);
if (!str.equalsIgnoreCase("y")) {
System.out.println("キャンセルしました");
return;
}
try {
empDao.empDelete​(id);
} finally {
DbUtil.closeConnection(empDao.con);
}
}
/** /**
* 社員テーブルから情報を検索する * 社員テーブルから情報を検索する
* *
@@ -150,4 +184,26 @@ public class Main {
} }
return str; return str;
} }
public static String inputCheck(boolean flg) {
String str = null;
while (true) {
str = sc.nextLine();
if (str.trim().length() == 0) {
continue;
}
if (flg) {
if (str.trim().length() > 0) {
try {
Integer.parseInt(str);
} catch (NumberFormatException e) {
continue;
}
}
}
break;
}
return str;
}
} }