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.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.rhizome.example.dto.EmployeeDTO;
|
||||||
import com.rhizome.example.util.DbUtil;
|
import com.rhizome.example.util.DbUtil;
|
||||||
|
|
||||||
public class EmployeeDAO {
|
public class EmployeeDAO {
|
||||||
@@ -48,8 +49,8 @@ public class EmployeeDAO {
|
|||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
* @throws ClassNotFoundException
|
* @throws ClassNotFoundException
|
||||||
*/
|
*/
|
||||||
public List<String> selectAll() throws SQLException, ClassNotFoundException {
|
public List<EmployeeDTO> selectAll() throws SQLException, ClassNotFoundException {
|
||||||
List<String> rtnList = new ArrayList<>();
|
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
sql.append(" SELECT ");
|
sql.append(" SELECT ");
|
||||||
sql.append(" " + "*");
|
sql.append(" " + "*");
|
||||||
@@ -60,7 +61,14 @@ 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()) {
|
||||||
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 {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closeStatement(this.stmt);
|
||||||
@@ -68,4 +76,42 @@ public class EmployeeDAO {
|
|||||||
return rtnList;
|
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 java.util.Scanner;
|
||||||
|
|
||||||
import com.rhizome.example.dao.EmployeeDAO;
|
import com.rhizome.example.dao.EmployeeDAO;
|
||||||
|
import com.rhizome.example.dto.EmployeeDTO;
|
||||||
import com.rhizome.example.util.DbUtil;
|
import com.rhizome.example.util.DbUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,6 +58,7 @@ public class Main {
|
|||||||
case "3":
|
case "3":
|
||||||
break;
|
break;
|
||||||
case "4":
|
case "4":
|
||||||
|
empDelete();
|
||||||
break;
|
break;
|
||||||
case "9":
|
case "9":
|
||||||
return;
|
return;
|
||||||
@@ -76,7 +78,7 @@ public class Main {
|
|||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
*/
|
*/
|
||||||
public static void empSelect() throws ClassNotFoundException, SQLException {
|
public static void empSelect() throws ClassNotFoundException, SQLException {
|
||||||
List<String> empList = new ArrayList<>();
|
List<EmployeeDTO> empList = new ArrayList<>();
|
||||||
Connection con = DbUtil.getConnection();
|
Connection con = DbUtil.getConnection();
|
||||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
||||||
@@ -86,10 +88,15 @@ public class Main {
|
|||||||
case "1":
|
case "1":
|
||||||
empList = empDao.selectAll();
|
empList = empDao.selectAll();
|
||||||
break;
|
break;
|
||||||
|
case "2":
|
||||||
|
String inputId = inputCheck("検索する社員ID", true);
|
||||||
|
int input = Integer.parseInt(inputId);
|
||||||
|
empList = empDao.selectById(input);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (String emp : empList) {
|
for (EmployeeDTO emp : empList) {
|
||||||
System.out.println(emp);
|
System.out.println(emp);
|
||||||
}
|
}
|
||||||
} finally {
|
} 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* フラグを判定し、テーブル名を取得する
|
* フラグを判定し、テーブル名を取得する
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package com.rhizome.example.util;
|
package com.rhizome.example.util;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class DbUtil {
|
public class DbUtil {
|
||||||
// 定数定義(共通)
|
// 定数定義(共通)
|
||||||
@@ -41,7 +44,11 @@ public class DbUtil {
|
|||||||
* 生成されたConnectionを返却する
|
* 生成されたConnectionを返却する
|
||||||
* ※Connection生成には上記の定数を活用してください
|
* ※Connection生成には上記の定数を活用してください
|
||||||
*/
|
*/
|
||||||
|
public static Connection getConnection() throws ClassNotFoundException, SQLException {
|
||||||
|
Class.forName(DB_DRIVER);
|
||||||
|
|
||||||
|
return con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO 以下の仕様でメソッドを作成してください。
|
* TODO 以下の仕様でメソッドを作成してください。
|
||||||
@@ -52,7 +59,12 @@ public class DbUtil {
|
|||||||
* 例外 :SQLException
|
* 例外 :SQLException
|
||||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||||
*/
|
*/
|
||||||
|
public static void closeStatement(PreparedStatement stmt) throws SQLException {
|
||||||
|
if (stmt != null) {
|
||||||
|
stmt.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO 以下の仕様でメソッドを作成してください。
|
* TODO 以下の仕様でメソッドを作成してください。
|
||||||
@@ -63,6 +75,10 @@ public class DbUtil {
|
|||||||
* 例外 :SQLException
|
* 例外 :SQLException
|
||||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||||
*/
|
*/
|
||||||
|
public static void closeConnection(Connection con) throws SQLException {
|
||||||
|
if (con != null) {
|
||||||
|
con.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user