1 Commits

Author SHA1 Message Date
m-endo
1c04363bb7 2段階目完了 2026-09-30 13:37:36 +09:00
10 changed files with 85 additions and 134 deletions

8
.classpath Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="lib/mysql-connector-java-8.0.23.jar"/>
<classpathentry kind="lib" path="実行例/DAO_Simple.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/bin/

23
.project Normal file
View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>em-jdbc</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>

7
.settings/.jsdtscope Normal file
View File

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

View File

@@ -0,0 +1 @@
org.eclipse.wst.jsdt.launching.JRE_CONTAINER

View File

@@ -0,0 +1 @@
Global

View File

@@ -61,14 +61,14 @@ public class EmployeeDAO {
this.stmt = con.prepareStatement(sql.toString());
rs = stmt.executeQuery();
while (rs.next()) {
//インスタンス生成
EmployeeDTO dto = new EmployeeDTO();
dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
dto.setNmEmployee(rs.getString(NM_EMPLOYEE));
dto.setKnEmployee(rs.getString(KN_EMPLOYEE));
dto.setMailAddress(rs.getString(MAIL_ADDRESS));
dto.setPassword(rs.getString(PASSWORD));
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
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 {
@@ -77,50 +77,4 @@ public class EmployeeDAO {
return rtnList;
}
public List<EmployeeDTO> selectById(int id) throws SQLException, ClassNotFoundException {
List<EmployeeDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder();
sql.append(" SELECT ");
sql.append(" * ");
sql.append(" FROM ");
sql.append(TBL_NAME);
sql.append(" WHERE ID_EMPLOYEE = ? ");
try {
this.stmt = con.prepareStatement(sql.toString());
this.stmt.setInt(1, id);
rs = this.stmt.executeQuery();
while (rs.next()) {
EmployeeDTO dto = new EmployeeDTO();
dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
dto.setNmEmployee(rs.getString(NM_EMPLOYEE));
dto.setKnEmployee(rs.getString(KN_EMPLOYEE));
dto.setMailAddress(rs.getString(MAIL_ADDRESS));
dto.setPassword(rs.getString(PASSWORD));
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
rtnList.add(dto);
}
} finally {
DbUtil.closeStatement(this.stmt);
}
return rtnList;
}
//社員データ削除
public void empDelete(int id) throws SQLException {
StringBuilder sql = new StringBuilder();
sql.append(" DELETE ");
sql.append(" FROM ");
sql.append(TBL_NAME);
sql.append(" WHERE ID_EMPLOYEE = ? ");
try {
this.stmt = con.prepareStatement(sql.toString());
this.stmt.setInt(1, id);
this.stmt.executeUpdate();
} finally {
DbUtil.closeStatement(this.stmt);
}
}
}

View File

@@ -1,43 +1,45 @@
package com.rhizome.example.dto;
public class EmployeeDTO {
private int idEmployee;
private String nmEmployee;
private String knEmployee;
private String mailAddress;
private String password;
private int idDepartment;
//フィールド宣言
private Integer id_employee;//社員ID
private String nm_employee;//社員名
private String kn_employee;//社員名カナ
private String mail_address;//メールアドレス
private String password;//パスワード
private Integer id_department;//部署ID
public int getIdEmployee() {
return idEmployee;
//getter/setter
public Integer getId_employee() {
return id_employee;
}
public void setIdEmployee(int idEmployee) {
this.idEmployee = idEmployee;
public void setId_employee(Integer id_employee) {
this.id_employee = id_employee;
}
public String getNmEmployee() {
return nmEmployee;
public String getNm_employee() {
return nm_employee;
}
public void setNmEmployee(String nmEmployee) {
this.nmEmployee = nmEmployee;
public void setNm_employee(String nm_employee) {
this.nm_employee = nm_employee;
}
public String getKnEmployee() {
return knEmployee;
public String getKn_employee() {
return kn_employee;
}
public void setKnEmployee(String knEmployee) {
this.knEmployee = knEmployee;
public void setKn_employee(String kn_employee) {
this.kn_employee = kn_employee;
}
public String getMailAddress() {
return mailAddress;
public String getMail_address() {
return mail_address;
}
public void setMailAddress(String mailAddress) {
this.mailAddress = mailAddress;
public void setMail_address(String mail_address) {
this.mail_address = mail_address;
}
public String getPassword() {
@@ -48,12 +50,12 @@ public class EmployeeDTO {
this.password = password;
}
public int getIdDepartment() {
return idDepartment;
public Integer getId_department() {
return id_department;
}
public void setIdDepartment(int idDepartment) {
this.idDepartment = idDepartment;
public void setId_department(Integer id_department) {
this.id_department = id_department;
}
}

View File

@@ -58,7 +58,6 @@ public class Main {
case "3":
break;
case "4":
empDelete();
break;
case "9":
return;
@@ -88,60 +87,17 @@ public class Main {
case "1":
empList = empDao.selectAll();
break;
case "2":
String id = inputCheck("ID", true);
empList = empDao.selectById(Integer.parseInt(id));
break;
default:
return;
}
if (empList.isEmpty()) {
System.out.println("入力された値で情報がみつかりませんでした");
} else {
for (EmployeeDTO emp : empList) {
System.out.println(
emp.getIdEmployee() + " "
+ emp.getNmEmployee() + " "
+ emp.getKnEmployee() + " "
+ emp.getMailAddress() + " "
+ emp.getPassword() + " "
+ emp.getIdDepartment());
}
for (EmployeeDTO emp : empList) {
System.out.println(emp);
}
} finally {
DbUtil.closeConnection(empDao.con);
}
}
//社員テーブルから情報を削除する
public static void empDelete()
throws ClassNotFoundException, SQLException {
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
String id = inputCheck("ID", true);
try {
List<EmployeeDTO> empList = empDao.selectById(Integer.parseInt(id));
if (empList.isEmpty()) {
System.out.println("入力されて値で情報がみつかりませんでした");
} else {
System.out.println("指定されたIDの情報を削除しますか? Y/y");
System.out.println("Y/y以外の入力であればキャンセル");
String confirm = sc.nextLine();
if (confirm.equalsIgnoreCase("y")) {
empDao.empDelete(Integer.parseInt(id));
System.out.println("削除が完了しました");
}
}
} finally {
DbUtil.closeConnection(con);
}
}
/**
* フラグを判定し、テーブル名を取得する
*

View File

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