Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b4f52cbc55 | |||
| f5ef9d86f6 | |||
| 54565e4472 | |||
| da3b79b06b | |||
| fb8d2c0b1c |
Binary file not shown.
@@ -60,14 +60,15 @@ public class EmployeeDAO {
|
||||
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = stmt.executeQuery();
|
||||
//DBのカラムをリストに追加
|
||||
while (rs.next()) {
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
dto.setID_EMPLOYEE(rs.getInt("ID_EMPLOYEE"));
|
||||
dto.setNM_EMPLOYEE(rs.getString("NM_EMPLOYEE"));
|
||||
dto.setKN_EMPLOYEE(rs.getString("KN_EMPLOYEE"));
|
||||
dto.setMAIL_ADDRESS(rs.getString("MAIL_ADDRESS"));
|
||||
dto.setPASSWORD(rs.getString("PASSWORD"));
|
||||
dto.setID_DEPARTMENT(rs.getInt("ID_DEPARTMENT"));
|
||||
dto.setEmpid(rs.getInt(ID_EMPLOYEE));
|
||||
dto.setName(rs.getString(NM_EMPLOYEE));
|
||||
dto.setKana(rs.getString(KN_EMPLOYEE));
|
||||
dto.setMail(rs.getString(MAIL_ADDRESS));
|
||||
dto.setPass(rs.getString(PASSWORD));
|
||||
dto.setDepid(rs.getInt(ID_DEPARTMENT));
|
||||
rtnList.add(dto);
|
||||
}
|
||||
} finally {
|
||||
@@ -76,42 +77,65 @@ public class EmployeeDAO {
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
//社員テーブルから指定された社員IDに一致するデータを取得し、DTOに設定の上、リスト形式で戻
|
||||
public List<EmployeeDTO> selectById(int id_employee) throws SQLException {
|
||||
List<EmployeeDTO> list = new ArrayList<>();
|
||||
String sql = "SELECT * FROM employee WHERE ID_EMPLOYEE = ?";
|
||||
try {
|
||||
public List<EmployeeDTO> selectByld(int id) throws SQLException {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
StringBuilder sql = new StringBuilder();
|
||||
//社員テーブルから指定された社員IDに一致するデータを取得
|
||||
sql.append(" SELECT ");
|
||||
sql.append(" " + "*");
|
||||
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());
|
||||
this.stmt.setInt(1, id_employee);
|
||||
this.rs = this.stmt.executeQuery();
|
||||
rs = stmt.executeQuery();
|
||||
//DBのカラムをリストに追加
|
||||
while (rs.next()) {
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
dto.setID_EMPLOYEE(rs.getInt("ID_EMPLOYEE"));
|
||||
dto.setNM_EMPLOYEE(rs.getString("NM_EMPLOYEE"));
|
||||
dto.setKN_EMPLOYEE(rs.getString("KN_EMPLOYEE"));
|
||||
dto.setMAIL_ADDRESS(rs.getString("MAIL_ADDRESS"));
|
||||
dto.setPASSWORD(rs.getString("PASSWORD"));
|
||||
dto.setID_DEPARTMENT(rs.getInt("ID_DEPARTMENT"));
|
||||
list.add(dto);
|
||||
dto.setEmpid(rs.getInt(ID_EMPLOYEE));
|
||||
dto.setName(rs.getString(NM_EMPLOYEE));
|
||||
dto.setKana(rs.getString(KN_EMPLOYEE));
|
||||
dto.setMail(rs.getString(MAIL_ADDRESS));
|
||||
dto.setPass(rs.getString(PASSWORD));
|
||||
dto.setDepid(rs.getInt(ID_DEPARTMENT));
|
||||
rtnList.add(dto);
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
|
||||
}
|
||||
return list;
|
||||
if (rtnList.size() == 0) {
|
||||
System.out.println("入力された値で情報が見つかりませんでした");
|
||||
}
|
||||
return rtnList;
|
||||
|
||||
}
|
||||
|
||||
// 社員テーブルから指定された社員IDに一致するデータを削除する
|
||||
public void empDelete(int id_employee) throws SQLException {
|
||||
String sql = "DELETE FROM employee WHERE ID_EMPLOYEE = ?";
|
||||
public void empDelete(int id) throws SQLException {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
//社員テーブルから指定された社員IDに一致するデータを取得
|
||||
sql.append(" DELETE ");
|
||||
sql.append(" " + "*");
|
||||
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);
|
||||
this.stmt.setInt(1, id_employee);
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
this.stmt.executeUpdate();
|
||||
|
||||
System.out.println("削除が完了しました");
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
|
||||
}
|
||||
}
|
||||
public void emplnsert (String name, String kana, String mail, String pass, int id) {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,8 @@ public class Main {
|
||||
* @throws SQLException
|
||||
*/
|
||||
public void start() throws ClassNotFoundException, SQLException {
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
System.out.println();
|
||||
System.out.println("現在のメンテナンス対象テーブル:" + targetTableName(tblFlg));
|
||||
System.out.println("1:検索 2:登録 3:更新 4:削除 9:テーブル切替 0:終了");
|
||||
@@ -71,6 +73,30 @@ public class Main {
|
||||
}
|
||||
}
|
||||
|
||||
private void empDelete() throws SQLException {
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
try {
|
||||
String strId = inputCheck("ID", true);
|
||||
int id = Integer.parseInt(strId);
|
||||
List<EmployeeDTO> empList = empDao.selectByld(id);
|
||||
if (empList.isEmpty()) {
|
||||
} else {
|
||||
System.out.println("指定されたIDの情報を削除します? Y/y");
|
||||
System.out.println("Y/y以外の入力であればキャンセル");
|
||||
Scanner sc = new Scanner(System.in);
|
||||
String str = sc.nextLine();
|
||||
if (str.equals("Y") || str.equals("y")) {
|
||||
empDao.empDelete(id);
|
||||
} else {
|
||||
System.out.println("キャンセルしました");
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルから情報を検索する
|
||||
*
|
||||
@@ -89,58 +115,22 @@ public class Main {
|
||||
empList = empDao.selectAll();
|
||||
break;
|
||||
case "2":
|
||||
String inputId = inputCheck("検索する社員ID", true);
|
||||
int input = Integer.parseInt(inputId);
|
||||
empList = empDao.selectById(input);
|
||||
System.out.println("IDを入力してください");
|
||||
int id = sc.nextInt();
|
||||
empList = empDao.selectByld(id);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
for (EmployeeDTO emp : empList) {
|
||||
System.out.println(emp);
|
||||
System.out.println(emp.getEmpid() + "\t" + emp.getName() + "\t" + emp.getKana() + "\t"
|
||||
+ emp.getMail() + "\t" + emp.getPass() + "\t" + emp.getDepid());
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
public static void empDelete() throws ClassNotFoundException, SQLException {
|
||||
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
try {
|
||||
System.out.println("IDを入力してください");
|
||||
String input = sc.nextLine();
|
||||
// 空文字が入力された場合
|
||||
if (input.strip().isEmpty()) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
return;
|
||||
}
|
||||
int inputId = Integer.parseInt(input.strip());
|
||||
List<EmployeeDTO> empList = empDao.selectById(inputId);
|
||||
// 検索結果が存在しないIDの場合
|
||||
if (empList.isEmpty()) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("指定されたIDの情報を削除します? Y/y");
|
||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
||||
String confilm = sc.nextLine();
|
||||
// 検索結果が存在した場合
|
||||
if ("Y".equalsIgnoreCase(confilm)) {
|
||||
empDao.empDelete(inputId);
|
||||
System.out.println("社員ID" + input + "のデータを削除しました");
|
||||
} else {
|
||||
System.out.println("削除をキャンセルしました");
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* フラグを判定し、テーブル名を取得する
|
||||
*
|
||||
@@ -168,7 +158,7 @@ public class Main {
|
||||
* @return
|
||||
*/
|
||||
public static String inputCheck(String titleName, boolean flg) {
|
||||
String str = null;
|
||||
String str;
|
||||
while (true) {
|
||||
System.out.println(titleName + "を入力してください");
|
||||
str = sc.nextLine();
|
||||
|
||||
@@ -44,10 +44,10 @@ public class DbUtil {
|
||||
* 生成されたConnectionを返却する
|
||||
* ※Connection生成には上記の定数を活用してください
|
||||
*/
|
||||
public static Connection getConnection() throws ClassNotFoundException, SQLException {
|
||||
Class.forName(DB_DRIVER);
|
||||
|
||||
return con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||
public static Connection getConnection() throws SQLException {
|
||||
//データベースへの接続
|
||||
con = DriverManager.getConnection(DB_URL, DB_USER ,DB_PASS);
|
||||
return con;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -59,13 +59,12 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||
*/
|
||||
public static void closeStatement(PreparedStatement stmt) throws SQLException {
|
||||
if (stmt != null) {
|
||||
stmt.close();
|
||||
public static void closeStatement(PreparedStatement ps) throws SQLException {
|
||||
//DBステートメントのクローズ
|
||||
if (ps != null) {
|
||||
ps.close(); //DBステートメントをクローズ
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
*
|
||||
@@ -76,9 +75,10 @@ public class DbUtil {
|
||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||
*/
|
||||
public static void closeConnection(Connection con) throws SQLException {
|
||||
//コネクションの切断
|
||||
if (con != null) {
|
||||
con.close();
|
||||
con.close(); //コネクション切断
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user