3 Commits

Author SHA1 Message Date
6493baccef 必須段階実装完了 2026-10-30 16:48:29 +09:00
f909550b87 5段階目まで完了 2026-10-29 15:49:27 +09:00
df7972c662 1日目 2段階目まで 2026-10-28 16:58:58 +09:00
5 changed files with 247 additions and 220 deletions

View File

@@ -37,4 +37,3 @@ INSERT INTO department (nm_department) VALUES ('経理部');
INSERT INTO department (nm_department) VALUES ('システム開発事業部'); INSERT INTO department (nm_department) VALUES ('システム開発事業部');
INSERT INTO department (nm_department) VALUES ('教育事業部'); INSERT INTO department (nm_department) VALUES ('教育事業部');
INSERT INTO department (nm_department) VALUES ('営業企画部'); INSERT INTO department (nm_department) VALUES ('営業企画部');

View File

@@ -61,129 +61,124 @@ public class EmployeeDAO {
this.stmt = con.prepareStatement(sql.toString()); this.stmt = con.prepareStatement(sql.toString());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
EmployeeDTO emp = new EmployeeDTO(); EmployeeDTO dto = new EmployeeDTO();
emp.setId(rs.getInt("id_employee")); dto.setId(rs.getInt("id_employee"));
emp.setName(rs.getString("nm_employee")); dto.setName(rs.getString("nm_employee"));
emp.setKana(rs.getString("kn_employee")); dto.setKana(rs.getString("kn_employee"));
emp.setEmail(rs.getString("mail_address")); dto.setEmail(rs.getString("mail_address"));
emp.setPass(rs.getString("password")); dto.setPass(rs.getString("password"));
emp.setIdDep(rs.getInt("id_department")); dto.setIdDep(rs.getInt("id_department"));
rtnList.add(dto);
rtnList.add(emp);
} }
} finally { } finally {
DbUtil.closeStatement(this.stmt); DbUtil.closeStatement​(this.stmt);
} }
return rtnList; return rtnList;
} }
//指定されたIDの社員データを取得する public java.util.List<EmployeeDTO> selectById​(int id)
public List<EmployeeDTO> selectById(int id_employee)throws SQLException, ClassNotFoundException{ throws java.sql.SQLException, java.lang.ClassNotFoundException, java.lang.NumberFormatException {
List<EmployeeDTO> rtnList = new ArrayList<>(); List<EmployeeDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder(); StringBuilder sql = new StringBuilder();
sql.append(" SELECT "); sql.append(" SELECT * FROM ");
sql.append(" " + "*"); sql.append(TBL_NAME);
sql.append(" FROM "); sql.append(" WHERE id_employee = ?");
sql.append(" " + TBL_NAME);
sql.append(" WHERE ");
sql.append(" ID_EMPLOYEE = ?");
try { try {
this.stmt = con.prepareStatement(sql.toString()); this.stmt = con.prepareStatement(sql.toString());
this.stmt.setInt(1,id_employee ); stmt.setInt(1, id);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
EmployeeDTO emp = new EmployeeDTO();
emp.setId(rs.getInt("id_employee"));
emp.setName(rs.getString("nm_employee"));
emp.setKana(rs.getString("kn_employee"));
emp.setEmail(rs.getString("mail_address"));
emp.setPass(rs.getString("password"));
emp.setIdDep(rs.getInt("id_department"));
rtnList.add(emp); EmployeeDTO dto = new EmployeeDTO();
dto.setId(rs.getInt("id_employee"));
dto.setName(rs.getString("nm_employee"));
dto.setKana(rs.getString("kn_employee"));
dto.setEmail(rs.getString("mail_address"));
dto.setPass(rs.getString("password"));
dto.setIdDep(rs.getInt("id_department"));
rtnList.add(dto);
} }
}finally {
DbUtil.closeStatement(this.stmt); } finally {
DbUtil.closeStatement​(this.stmt);
} }
return rtnList; return rtnList;
} }
//指定された社員のデータを削除する public void empDelete(int id) throws java.lang.ClassNotFoundException, java.sql.SQLException {
public void empDelete(int id_employee)throws SQLException, ClassNotFoundException{
//SQL文の作成
StringBuilder sql = new StringBuilder(); StringBuilder sql = new StringBuilder();
sql.append(" DELETE "); sql.append(" DELETE FROM ");
sql.append(" "); sql.append(TBL_NAME);
sql.append(" FROM "); sql.append(" WHERE id_employee = ?");
sql.append(" " + TBL_NAME);
sql.append(" WHERE ");
sql.append(" ID_EMPLOYEE = ?");
try { try {
//SQL文の組み立て結果を保存 this.stmt = this.con.prepareStatement(sql.toString());
this.stmt = con.prepareStatement(sql.toString()); this.stmt.setInt(1, id);
//?に代入 this.stmt.executeUpdate();
this.stmt.setInt(1,id_employee );
//削除命令 } finally {
stmt.executeUpdate(); DbUtil.closeStatement​(this.stmt);
}finally { }
DbUtil.closeStatement(this.stmt);
} }
}
public void empInsert(String name, String kana, String email, public void empInsert​(java.lang.String name, java.lang.String kana, java.lang.String mail, java.lang.String pass,
String password, Integer idDep)throws SQLException, ClassNotFoundException{ int depId) throws java.lang.NumberFormatException, java.sql.SQLException, java.lang.ClassNotFoundException {
//List<EmployeeDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder(); StringBuilder sql = new StringBuilder();
sql.append(" INSERT "); sql.append(" INSERT INTO " + TBL_NAME);
sql.append(" " + " INTO "); sql.append(" (nm_employee, kn_employee, mail_address, password, id_department) ");
sql.append(" " + TBL_NAME);
sql.append(" ( ");
sql.append(" " + NM_EMPLOYEE + ", " + KN_EMPLOYEE + ", " +
MAIL_ADDRESS + ", " + PASSWORD + ", " + ID_DEPARTMENT);
sql.append(" ) ");
sql.append(" VALUES "); sql.append(" VALUES ");
sql.append(" ( ?, ?, ?, ?, ?) "); sql.append(" (?, ?, ?, ?, ?) ");
try { try {
this.stmt = con.prepareStatement(sql.toString()); this.stmt = con.prepareStatement(sql.toString());
this.stmt.setString(1,name );
this.stmt.setString(2,kana ); this.stmt.setString(1, name);
this.stmt.setString(3,email ); this.stmt.setString(2, kana);
this.stmt.setString(4,password ); this.stmt.setString(3, mail);
this.stmt.setInt(5,idDep ); this.stmt.setString(4, pass);
stmt.executeUpdate(); this.stmt.setInt(5, depId);
}finally {
DbUtil.closeStatement(this.stmt); this.stmt.executeUpdate();
} finally {
DbUtil.closeStatement​(this.stmt);
} }
} }
public void empUpdate(Integer id, String name, String kana, String email, public void empUpdate​(int id, java.lang.String name, java.lang.String kana, java.lang.String mail,
String password, Integer idDep)throws SQLException, ClassNotFoundException{ java.lang.String pass, int depId)
throws java.lang.ClassNotFoundException, java.sql.SQLException, java.lang.NumberFormatException {
StringBuilder sql = new StringBuilder(); StringBuilder sql = new StringBuilder();
sql.append(" UPDATE "); sql.append(" UPDATE ");
sql.append(" " + TBL_NAME); sql.append(TBL_NAME);
sql.append(" SET "); sql.append(" SET ");
sql.append(" " + NM_EMPLOYEE + "= ?,"); sql.append(" nm_employee = ?, kn_employee = ?, mail_address = ?, password = ?, id_department = ? ");
sql.append(" " + KN_EMPLOYEE + "= ?,"); sql.append(" WHERE id_employee = ? ");
sql.append(" " + MAIL_ADDRESS + "= ?,");
sql.append(" " + PASSWORD + "= ?,");
sql.append(" " + ID_DEPARTMENT + "= ?");
sql.append(" WHERE ");
sql.append(ID_EMPLOYEE + "= ?");
try { try {
this.stmt = con.prepareStatement(sql.toString()); this.stmt = con.prepareStatement(sql.toString());
this.stmt.setString(1,name );
this.stmt.setString(2,kana ); this.stmt.setString(1, name);
this.stmt.setString(3,email ); this.stmt.setString(2, kana);
this.stmt.setString(4,password ); this.stmt.setString(3, mail);
this.stmt.setInt(5,idDep ); this.stmt.setString(4, pass);
this.stmt.setInt(5, depId);
this.stmt.setInt(6, id); this.stmt.setInt(6, id);
stmt.executeUpdate();
}finally { this.stmt.executeUpdate();
DbUtil.closeStatement(this.stmt);
} finally {
DbUtil.closeStatement​(this.stmt);
} }
} }

View File

@@ -1,6 +1,7 @@
package com.rhizome.example.dto; package com.rhizome.example.dto;
public class EmployeeDTO { public class EmployeeDTO {
private Integer id; private Integer id;
private String name; private String name;
private String kana; private String kana;
@@ -11,37 +12,49 @@ public class EmployeeDTO {
public Integer getId() { public Integer getId() {
return id; return id;
} }
public void setId(Integer id) { public void setId(Integer id) {
this.id = id; this.id = id;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public String getKana() { public String getKana() {
return kana; return kana;
} }
public void setKana(String kana) { public void setKana(String kana) {
this.kana = kana; this.kana = kana;
} }
public String getEmail() { public String getEmail() {
return email; return email;
} }
public void setEmail(String email) { public void setEmail(String email) {
this.email = email; this.email = email;
} }
public String getPass() { public String getPass() {
return pass; return pass;
} }
public void setPass(String pass) { public void setPass(String pass) {
this.pass = pass; this.pass = pass;
} }
public Integer getIdDep() { public Integer getIdDep() {
return idDep; return idDep;
} }
public void setIdDep(Integer idDep) { public void setIdDep(Integer idDep) {
this.idDep = idDep; this.idDep = idDep;
} }
} }

View File

@@ -91,23 +91,119 @@ public class Main {
empList = empDao.selectAll(); empList = empDao.selectAll();
break; break;
case "2": case "2":
String strId =inputCheck("ID", true); String idStr = inputCheck("ID", true);
int ID = Integer.parseInt(strId); int id = Integer.parseInt(idStr);
empList = empDao.selectById(ID); empList = empDao.selectById​(id);
break; break;
default: default:
return; return;
} }
if(empList.isEmpty()) {
System.out.println("入力された値で情報がみつかりませんでした");
}else{
for (EmployeeDTO emp : empList) { for (EmployeeDTO emp : empList) {
System.out.println(emp.getId() + "\t" + emp.getName() + "\t" + System.out.println(
emp.getKana() + "\t" + emp.getEmail() + "\t" + emp.getPass() + "\t" + emp.getIdDep());
} emp.getId() + "\t" +
emp.getName() + "\t" +
emp.getKana() + "\t" +
emp.getEmail() + "\t" +
emp.getPass() + "\t" +
emp.getIdDep()
);
} }
} finally { } finally {
DbUtil.closeConnection(empDao.con); DbUtil.closeConnection​(empDao.con);
}
}
public static void empInsert()
throws java.lang.NumberFormatException, java.lang.ClassNotFoundException, java.sql.SQLException {
//List<EmployeeDTO> empList = new ArrayList<>();
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
try {
String name = inputCheck("社員名", false);
String kana = inputCheck("フリガナ", false);
String mail = inputCheck("メールアドレス", false);
String pass = inputCheck("パスワード", false);
String depIdstr = inputCheck("部署ID", true);
int depId = Integer.parseInt(depIdstr);
System.out.println("登録が完了しました");
empDao.empInsert​(name, kana, mail, pass, depId);
} finally {
DbUtil.closeConnection​(empDao.con);
}
}
public static void empUpdate()
throws java.lang.NumberFormatException, java.lang.ClassNotFoundException, java.sql.SQLException {
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
try {
String idstr = inputCheck("ID", true);
int id = Integer.parseInt(idstr);
List<EmployeeDTO> checkList = empDao.selectById​(id);
if (checkList.size() == 0) {
System.out.println("入力された値で情報がみつかりませんでした");
return;
}
String name = inputCheck("社員名", false);
String kana = inputCheck("フリガナ", false);
String mail = inputCheck("メールアドレス", false);
String pass = inputCheck("パスワード", false);
String depIdstr = inputCheck("部署ID", true);
int depId = Integer.parseInt(depIdstr);
empDao.empUpdate​(id, name, kana, mail, pass, depId);
System.out.println("更新が完了しました");
} finally {
DbUtil.closeConnection​(empDao.con);
}
}
public static void empDelete() throws ClassNotFoundException, SQLException {
//List<EmployeeDTO> empList = new ArrayList<>();
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
try {
String idStr = inputCheck("ID", true);
int id = Integer.parseInt(idStr);
List<EmployeeDTO> checkList = empDao.selectById​(id);
if (checkList.size() == 0) {
System.out.println("入力された値で情報がみつかりませんでした");
return;
}
System.out.println("指定されたIDの情報を削除します? Y/y");
System.out.println("(Y/y以外の入力であればキャンセル)");
String yY = sc.nextLine();
if (!"y".equals(yY) && !"Y".equals(yY)) {
System.out.println("削除をキャンセルしました");
return;
}
empDao.empDelete(id);
System.out.println("削除が完了しました");
} finally {
DbUtil.closeConnection​(empDao.con);
} }
} }
@@ -160,81 +256,4 @@ public class Main {
} }
return str; 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);
}
}
public static void empUpdate() throws ClassNotFoundException, SQLException{
List<EmployeeDTO> empList = new ArrayList<>();
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
try {
String delId =inputCheck("ID", true);
int ID = Integer.parseInt(delId);
empList = empDao.selectById(ID);
if(empList.isEmpty()) {
System.out.println("入力された値で情報がみつかりませんでした");
return;
}
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.empUpdate(ID,name,kana,email,password,dID);
System.out.println("更新が完了しました");
}finally {
DbUtil.closeConnection(empDao.con);
}
}
} }

View File

@@ -2,8 +2,6 @@ package com.rhizome.example.util;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DbUtil { public class DbUtil {
// 定数定義(共通) // 定数定義(共通)
@@ -44,13 +42,9 @@ public class DbUtil {
* 生成されたConnectionを返却する * 生成されたConnectionを返却する
* ※Connection生成には上記の定数を活用してください * ※Connection生成には上記の定数を活用してください
*/ */
public static Connection getConnection() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.cj.jdbc.Driver"); public static java.sql.Connection getConnection() throws java.lang.ClassNotFoundException, java.sql.SQLException {
con = DriverManager.getConnection( con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
"jdbc:mysql://127.0.0.1:3306/rezodb",
"rezouser",
"Rezo_0000"
);
return con; return con;
} }
@@ -63,8 +57,9 @@ public class DbUtil {
* 例外 :SQLException * 例外 :SQLException
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする * 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
*/ */
public static void closeStatement(PreparedStatement stmt) throws SQLException{
if(stmt != null) { public static void closeStatement​(java.sql.PreparedStatement stmt) throws java.sql.SQLException {
if (stmt != null) {
stmt.close(); stmt.close();
} }
} }
@@ -78,10 +73,16 @@ public class DbUtil {
* 例外 :SQLException * 例外 :SQLException
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する * 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
*/ */
public static void closeConnection(Connection con) throws SQLException{
if(con != null) { //public static void closeConnection(Connection con) throws SQLException {
//if (con != null) {
//con.close();
//}
//
public static void closeConnection​(java.sql.Connection con) throws java.sql.SQLException {
if (con != null)
con.close(); con.close();
} }
}
} }