Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0062b9fce4 | |||
| c65c3b1db8 | |||
| e5fef82677 | |||
| 4f60de10c4 |
@@ -1,8 +0,0 @@
|
||||
<?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
1
.gitignore
vendored
@@ -1 +0,0 @@
|
||||
/bin/
|
||||
23
.project
23
.project
@@ -1,23 +0,0 @@
|
||||
<?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>
|
||||
@@ -1,7 +0,0 @@
|
||||
<?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>
|
||||
@@ -1 +0,0 @@
|
||||
org.eclipse.wst.jsdt.launching.JRE_CONTAINER
|
||||
@@ -1 +0,0 @@
|
||||
Global
|
||||
Binary file not shown.
@@ -61,14 +61,13 @@ public class EmployeeDAO {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = 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"));
|
||||
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,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
package com.rhizome.example.dto;
|
||||
|
||||
public class EmployeeDTO {
|
||||
//フィールド宣言
|
||||
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
|
||||
|
||||
//getter/setter
|
||||
public Integer getId_employee() {
|
||||
return id_employee;
|
||||
}
|
||||
|
||||
public void setId_employee(Integer id_employee) {
|
||||
this.id_employee = id_employee;
|
||||
}
|
||||
|
||||
public String getNm_employee() {
|
||||
return nm_employee;
|
||||
}
|
||||
|
||||
public void setNm_employee(String nm_employee) {
|
||||
this.nm_employee = nm_employee;
|
||||
}
|
||||
|
||||
public String getKn_employee() {
|
||||
return kn_employee;
|
||||
}
|
||||
|
||||
public void setKn_employee(String kn_employee) {
|
||||
this.kn_employee = kn_employee;
|
||||
}
|
||||
|
||||
public String getMail_address() {
|
||||
return mail_address;
|
||||
}
|
||||
|
||||
public void setMail_address(String mail_address) {
|
||||
this.mail_address = mail_address;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public Integer getId_department() {
|
||||
return id_department;
|
||||
}
|
||||
|
||||
public void setId_department(Integer id_department) {
|
||||
this.id_department = id_department;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -58,6 +58,7 @@ public class Main {
|
||||
case "3":
|
||||
break;
|
||||
case "4":
|
||||
empDelete();
|
||||
break;
|
||||
case "9":
|
||||
return;
|
||||
@@ -87,6 +88,11 @@ 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;
|
||||
}
|
||||
@@ -98,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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* フラグを判定し、テーブル名を取得する
|
||||
*
|
||||
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user