20261029 5段階目まで完了

This commit is contained in:
2026-10-29 15:56:21 +09:00
parent 9f7fa737ef
commit bae6f62ca2
2 changed files with 118 additions and 2 deletions

View File

@@ -77,6 +77,7 @@ public class EmployeeDAO {
return rtnList;
}
//指定されたIDの社員データを取得する
public List<EmployeeDTO> selectById(int id_employee)throws SQLException, ClassNotFoundException{
List<EmployeeDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder();
@@ -85,7 +86,7 @@ public class EmployeeDAO {
sql.append(" FROM ");
sql.append(" " + TBL_NAME);
sql.append(" WHERE ");
sql.append(" ID_DEPARTMENT = ?");
sql.append(" ID_EMPLOYEE = ?");
try {
this.stmt = con.prepareStatement(sql.toString());
@@ -107,4 +108,58 @@ public class EmployeeDAO {
}
return rtnList;
}
//指定された社員のデータを削除する
public void empDelete(int id_employee)throws SQLException, ClassNotFoundException{
//リスト作成
List<EmployeeDTO> rtnList = new ArrayList<>();
//SQL文の作成
StringBuilder sql = new StringBuilder();
sql.append(" DELETE ");
sql.append(" ");
sql.append(" FROM ");
sql.append(" " + TBL_NAME);
sql.append(" WHERE ");
sql.append(" ID_EMPLOYEE = ?");
try {
//SQL文の組み立て結果を保存
this.stmt = con.prepareStatement(sql.toString());
//?に代入
this.stmt.setInt(1,id_employee );
//削除命令
stmt.executeUpdate();
}finally {
DbUtil.closeStatement(this.stmt);
}
}
public void empInsert(String name, String kana, String email,
String password, Integer idDep)throws SQLException, ClassNotFoundException{
List<EmployeeDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder();
sql.append(" INSERT ");
sql.append(" " + " INTO ");
sql.append(" " + TBL_NAME);
sql.append(" ( ");
sql.append(" " + NM_EMPLOYEE + ", " + KN_EMPLOYEE + ", " +
MAIL_ADDRESS + ", " + PASSWORD + ", " + ID_DEPARTMENT);
sql.append(" ) ");
sql.append(" VALUES ");
sql.append(" ( ?, ?, ?, ?, ?) ");
try {
this.stmt = con.prepareStatement(sql.toString());
this.stmt.setString(1,name );
this.stmt.setString(2,kana );
this.stmt.setString(3,email );
this.stmt.setString(4,password );
this.stmt.setInt(5,idDep );
stmt.executeUpdate();
}finally {
DbUtil.closeStatement(this.stmt);
}
}
}

View File

@@ -54,11 +54,12 @@ public class Main {
empSelect();
break;
case "2":
inputCheck("id_employee");
empInsert();
break;
case "3":
break;
case "4":
empDelete();
break;
case "9":
return;
@@ -88,13 +89,22 @@ public class Main {
case "1":
empList = empDao.selectAll();
break;
case "2":
String strId =inputCheck("ID", true);
int ID = Integer.parseInt(strId);
empList = empDao.selectById(ID);
break;
default:
return;
}
if(empList.isEmpty()) {
System.out.println("入力された値で情報がみつかりませんでした");
}else{
for (EmployeeDTO emp : empList) {
System.out.println(emp.getId() + "\t" + emp.getName() + "\t" +
emp.getKana() + "\t" + emp.getEmail() + "\t" + emp.getPass() + "\t" + emp.getIdDep());
}
}
} finally {
DbUtil.closeConnection(empDao.con);
}
@@ -149,4 +159,55 @@ public class Main {
}
return str;
}
public static void empDelete() throws ClassNotFoundException, SQLException {
//リスト作成
List<EmployeeDTO> empList = new ArrayList<>();
//DB接続
Connection con = DbUtil.getConnection();
//DAOインスタンス生成
EmployeeDAO empDao = new EmployeeDAO(con);
try {
//入力された文字列をdelIdに保存
String delId =inputCheck("ID", true);
//入力された文字列を整数に変換して保存
int ID = Integer.parseInt(delId);
//入力されたIDに一致するデータを保存
empList = empDao.selectById(ID);
//リストが空=一致なし
if(empList.isEmpty()) {
System.out.println("入力された値で情報がみつかりませんでした");
}else {
System.out.println("指定されたIDの情報を削除します? Y/y");
//削除の同意受付
String agr = sc.nextLine();
//同意が得られたら
if("Y".equals(agr) || "y".equals(agr)) {
//削除メソッド実行し削除
empDao.empDelete(ID);
System.out.println("削除が完了しました");
}
}
}finally {
DbUtil.closeConnection(empDao.con);
}
}
public static void empInsert() throws ClassNotFoundException, SQLException {
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
try {
String name = inputCheck("社員名",false);
String kana = inputCheck("フリガナ", false);
String email = inputCheck("メールアドレス", false);
String password = inputCheck("パスワード", false);
String depId = inputCheck("部署ID", true);
int dID = Integer.parseInt(depId);
empDao.empInsert(name,kana,email,password,dID);
System.out.println("登録が完了しました");
}finally {
DbUtil.closeConnection(empDao.con);
}
}
}