6段階目(DAOのデータ更新メソッドを作成→更新処理完成)

This commit is contained in:
s-sakiyama
2026-09-30 14:49:35 +09:00
parent b4b3f35d00
commit a56fa4eefa
2 changed files with 75 additions and 0 deletions

View File

@@ -182,4 +182,49 @@ public class EmployeeDAO {
DbUtil.closeStatement(this.stmt);
}
}
/**
* 社員テーブルの社員IDに合致する情報を更新する
*
* @param id - 社員ID
* @param name - 社員名
* @param kana - 社員名カナ
* @param mail - メールアドレス
* @param pass - パスワード
* @param depId - 部署ID
* @throws ClassNotFoundException
* @throws SQLException
* @throws NumberFormatException
*/
public void empUpdate​(int id, String name, String kana, String mail, String pass, int depId)
throws ClassNotFoundException, SQLException, NumberFormatException {
StringBuilder sql = new StringBuilder();
sql.append(" UPDATE ");
sql.append(" " + TBL_NAME);
sql.append(" SET ");
sql.append(" " + NM_EMPLOYEE);
sql.append(" = '" + name + " ' ");
sql.append(" , " + KN_EMPLOYEE);
sql.append(" = '" + kana + " ' ");
sql.append(" , " + MAIL_ADDRESS);
sql.append(" = '" + mail + " ' ");
sql.append(" , " + PASSWORD);
sql.append(" = '" + pass + " ' ");
sql.append(" , " + ID_DEPARTMENT);
sql.append(" = " + depId);
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

@@ -57,6 +57,7 @@ public class Main {
empInsert​();
break;
case "3":
empUpdate​();
break;
case "4":
empDelete​();
@@ -72,6 +73,35 @@ public class Main {
}
}
/**
* 社員テーブルの社員IDに合致する情報を更新する
*
* @throws ClassNotFoundException
* @throws SQLException
* @throws NumberFormatException
*/
public static void empUpdate​()
throws ClassNotFoundException, SQLException, NumberFormatException {
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
int id = Integer.parseInt(inputCheck("ID", true));
if (empDao.selectById​(id).size() == 0) {
return;
}
String name = inputCheck("社員名", false);
String kana = inputCheck("フリガナ", false);
String mail = inputCheck("メールアドレス", false);
String pass = inputCheck("パスワード", false);
int depId = Integer.parseInt(inputCheck("部署ID", true));
try {
empDao.empUpdate​(id, name, kana, mail, pass, depId);
} finally {
DbUtil.closeConnection(empDao.con);
}
}
/**
* 社員テーブルに指定された情報を新規登録する
* @throws NumberFormatException