5段階目(DAOのデータ登録メソッドを作成→登録処理完成)

This commit is contained in:
s-sakiyama
2026-09-30 14:00:43 +09:00
parent a6bd282b1c
commit b4b3f35d00
2 changed files with 68 additions and 3 deletions

View File

@@ -82,7 +82,7 @@ public class EmployeeDAO {
* @throws NumberFormatException
*/
public List<EmployeeDTO> selectById​(int id)
throws java.sql.SQLException, java.lang.ClassNotFoundException, java.lang.NumberFormatException {
throws SQLException, ClassNotFoundException, NumberFormatException {
List<EmployeeDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder();
sql.append(" SELECT ");
@@ -124,7 +124,7 @@ public class EmployeeDAO {
* @throws NumberFormatException
*/
public void empDelete​(int id)
throws java.lang.ClassNotFoundException, java.sql.SQLException, java.lang.NumberFormatException {
throws ClassNotFoundException, SQLException, NumberFormatException {
StringBuilder sql = new StringBuilder();
sql.append(" DELETE ");
sql.append(" FROM ");
@@ -143,4 +143,43 @@ public class EmployeeDAO {
}
}
/**
* 社員テーブルに指定された情報を新規登録する
*
* @param name - 社員名
* @param kana - 社員名カナ
* @param mail - メールアドレス
* @param pass - パスワード
* @param depId - 部署ID
* @throws NumberFormatException
* @throws SQLException
* @throws ClassNotFoundException
*/
public void empInsert​(String name, String kana, String mail, String pass, int depId)
throws NumberFormatException, SQLException, ClassNotFoundException {
StringBuilder sql = new StringBuilder();
sql.append(" INSERT ");
sql.append(" INTO ");
sql.append(" " + TBL_NAME);
sql.append(" ( " + NM_EMPLOYEE);
sql.append(" , " + KN_EMPLOYEE);
sql.append(" , " + MAIL_ADDRESS);
sql.append(" , " + PASSWORD);
sql.append(" , " + ID_DEPARTMENT + " ) ");
sql.append(" VALUES ");
sql.append(" ( '" + name);
sql.append("' , '" + kana);
sql.append("' , '" + mail);
sql.append("' , '" + pass);
sql.append("' , " + depId + " ) ");
try {
this.stmt = con.prepareStatement(sql.toString());
stmt.executeUpdate();
System.out.println("登録が完了しました");
} finally {
DbUtil.closeStatement(this.stmt);
}
}
}

View File

@@ -54,6 +54,7 @@ public class Main {
empSelect();
break;
case "2":
empInsert​();
break;
case "3":
break;
@@ -71,6 +72,31 @@ public class Main {
}
}
/**
* 社員テーブルに指定された情報を新規登録する
* @throws NumberFormatException
* @throws SQLException
* @throws ClassNotFoundException
*/
public static void empInsert​()
throws NumberFormatException, SQLException, ClassNotFoundException {
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
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.empInsert​(name, kana, mail, pass, depId);
} finally {
DbUtil.closeConnection(empDao.con);
}
}
/**
* 社員テーブルの社員IDに合致する情報を削除する
*
@@ -78,7 +104,7 @@ public class Main {
* @throws SQLException
*/
public static void empDelete​()
throws java.lang.ClassNotFoundException, java.sql.SQLException, java.lang.NumberFormatException {
throws ClassNotFoundException, SQLException, NumberFormatException {
List<EmployeeDTO> rtnList = new ArrayList<>();
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);