Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0062b9fce4 | |||
| c65c3b1db8 | |||
| e5fef82677 | |||
| 4f60de10c4 |
Binary file not shown.
@@ -13,6 +13,7 @@ import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.rhizome.example.dto.EmployeeDTO;
|
||||
import com.rhizome.example.util.DbUtil;
|
||||
|
||||
public class EmployeeDAO {
|
||||
@@ -48,8 +49,8 @@ public class EmployeeDAO {
|
||||
* @throws SQLException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
public List<String> selectAll() throws SQLException, ClassNotFoundException {
|
||||
List<String> rtnList = new ArrayList<>();
|
||||
public List<EmployeeDTO> selectAll() throws SQLException, ClassNotFoundException {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" SELECT ");
|
||||
sql.append(" " + "*");
|
||||
@@ -60,7 +61,14 @@ public class EmployeeDAO {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
rtnList.add(rs.getString(NM_EMPLOYEE));
|
||||
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"));
|
||||
rtnList.add(dto);
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
@@ -68,4 +76,42 @@ 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 {
|
||||
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
this.stmt.setInt(1, id_employee);
|
||||
this.rs = this.stmt.executeQuery();
|
||||
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);
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
// 社員テーブルから指定された社員IDに一致するデータを削除する
|
||||
public void empDelete(int id_employee) throws SQLException {
|
||||
String sql = "DELETE FROM employee WHERE ID_EMPLOYEE = ?";
|
||||
try {
|
||||
this.stmt = con.prepareStatement(sql);
|
||||
this.stmt.setInt(1, id_employee);
|
||||
this.stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import com.rhizome.example.dao.EmployeeDAO;
|
||||
import com.rhizome.example.dto.EmployeeDTO;
|
||||
import com.rhizome.example.util.DbUtil;
|
||||
|
||||
/**
|
||||
@@ -57,6 +58,7 @@ public class Main {
|
||||
case "3":
|
||||
break;
|
||||
case "4":
|
||||
empDelete();
|
||||
break;
|
||||
case "9":
|
||||
return;
|
||||
@@ -76,7 +78,7 @@ public class Main {
|
||||
* @throws SQLException
|
||||
*/
|
||||
public static void empSelect() throws ClassNotFoundException, SQLException {
|
||||
List<String> empList = new ArrayList<>();
|
||||
List<EmployeeDTO> empList = new ArrayList<>();
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
||||
@@ -86,10 +88,15 @@ public class Main {
|
||||
case "1":
|
||||
empList = empDao.selectAll();
|
||||
break;
|
||||
case "2":
|
||||
String inputId = inputCheck("検索する社員ID", true);
|
||||
int input = Integer.parseInt(inputId);
|
||||
empList = empDao.selectById(input);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
for (String emp : empList) {
|
||||
for (EmployeeDTO emp : empList) {
|
||||
System.out.println(emp);
|
||||
}
|
||||
} finally {
|
||||
@@ -97,6 +104,43 @@ public class Main {
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* フラグを判定し、テーブル名を取得する
|
||||
*
|
||||
|
||||
@@ -45,17 +45,9 @@ public class DbUtil {
|
||||
* ※Connection生成には上記の定数を活用してください
|
||||
*/
|
||||
public static Connection getConnection() throws ClassNotFoundException, SQLException {
|
||||
try {
|
||||
Class.forName(DB_DRIVER);
|
||||
return DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||
} catch (ClassNotFoundException e) {
|
||||
System.out.println();
|
||||
throw e;
|
||||
} catch (SQLException e) {
|
||||
System.out.println();
|
||||
throw e;
|
||||
}
|
||||
Class.forName(DB_DRIVER);
|
||||
|
||||
return con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -67,14 +59,11 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||
*/
|
||||
public static void closeStatement(PreparedStatement stmt) {
|
||||
public static void closeStatement(PreparedStatement stmt) throws SQLException {
|
||||
if (stmt != null) {
|
||||
try {
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
System.out.println();
|
||||
}
|
||||
stmt.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -86,15 +75,9 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||
*/
|
||||
public static void closeConnection(Connection con) {
|
||||
public static void closeConnection(Connection con) throws SQLException {
|
||||
if (con != null) {
|
||||
try {
|
||||
if (!con.isClosed()) {
|
||||
con.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user