4 Commits

Author SHA1 Message Date
0062b9fce4 4段階目終了 2026-09-30 14:32:50 +09:00
c65c3b1db8 3段階目終了 2026-09-30 13:02:23 +09:00
e5fef82677 2段階目終了 2026-09-29 16:53:16 +09:00
4f60de10c4 1段階目終了 2026-09-29 16:15:11 +09:00
11 changed files with 101 additions and 50 deletions

1
.gitignore vendored
View File

@@ -1 +0,0 @@
/.metadata/

View File

@@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>em-jdbc</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>

View File

@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path=""/>
<classpathentry kind="output" path=""/>
</classpath>

View File

@@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>src</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -1 +0,0 @@
/EmployeeDAO.class

View File

@@ -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);
}
}
}

View File

@@ -1 +0,0 @@
/Main.class

View File

@@ -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);
}
}
/**
* フラグを判定し、テーブル名を取得する
*

View File

@@ -1 +0,0 @@
/DbUtil.class

View File

@@ -44,10 +44,10 @@ public class DbUtil {
* 生成されたConnectionを返却する
* ※Connection生成には上記の定数を活用してください
*/
public static Connection getConnection() throws SQLException {
//データベースへの接続
con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
return con;
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName(DB_DRIVER);
return con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
}
/*
@@ -60,10 +60,10 @@ public class DbUtil {
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
*/
public static void closeStatement(PreparedStatement stmt) throws SQLException {
//nullチェック
if (stmt != null) {
stmt.close();
}
}
/*
@@ -76,10 +76,9 @@ public class DbUtil {
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
*/
public static void closeConnection(Connection con) throws SQLException {
//nullチェック
if (con != null) {
con.close();
}
}
}