Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ae6a693aeb | ||
|
|
00cf2096e2 | ||
|
|
d2ebdbd11c | ||
|
|
ac0ebd51ce |
@@ -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
|
||||
@@ -62,46 +62,65 @@ public class EmployeeDAO {
|
||||
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.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);
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
public List<EmployeeDTO> selectById(int id_employee) throws SQLException, ClassNotFoundException {
|
||||
List<EmployeeDTO> rtnList2 = new ArrayList<>();
|
||||
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(" * ");
|
||||
sql.append(" FROM ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
sql.append(" WHERE " + ID_EMPLOYEE+ "=?");
|
||||
sql.append(TBL_NAME);
|
||||
sql.append(" WHERE ID_EMPLOYEE = ? ");
|
||||
try {
|
||||
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = stmt.executeQuery();
|
||||
this.stmt.setInt(1, id);
|
||||
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"));
|
||||
rtnList2.add(dto);
|
||||
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);
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
return rtnList2;
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +1,43 @@
|
||||
package com.rhizome.example.dto;
|
||||
|
||||
public class EmployeeDTO {
|
||||
|
||||
private Integer id_employee;
|
||||
private String nm_employee;
|
||||
private String kn_employee;
|
||||
private String mail_address;
|
||||
private int idEmployee;
|
||||
private String nmEmployee;
|
||||
private String knEmployee;
|
||||
private String mailAddress;
|
||||
private String password;
|
||||
private Integer id_department;
|
||||
private int idDepartment;
|
||||
|
||||
public Integer getId_employee() {
|
||||
return id_employee;
|
||||
public int getIdEmployee() {
|
||||
return idEmployee;
|
||||
}
|
||||
|
||||
public void setId_employee(Integer id_employee) {
|
||||
this.id_employee = id_employee;
|
||||
public void setIdEmployee(int idEmployee) {
|
||||
this.idEmployee = idEmployee;
|
||||
}
|
||||
|
||||
public String getNm_employee() {
|
||||
return nm_employee;
|
||||
public String getNmEmployee() {
|
||||
return nmEmployee;
|
||||
}
|
||||
|
||||
public void setNm_employee(String nm_employee) {
|
||||
this.nm_employee = nm_employee;
|
||||
public void setNmEmployee(String nmEmployee) {
|
||||
this.nmEmployee = nmEmployee;
|
||||
}
|
||||
|
||||
public String getKn_employee() {
|
||||
return kn_employee;
|
||||
public String getKnEmployee() {
|
||||
return knEmployee;
|
||||
}
|
||||
|
||||
public void setKn_employee(String kn_employee) {
|
||||
this.kn_employee = kn_employee;
|
||||
public void setKnEmployee(String knEmployee) {
|
||||
this.knEmployee = knEmployee;
|
||||
}
|
||||
|
||||
public String getMail_address() {
|
||||
return mail_address;
|
||||
public String getMailAddress() {
|
||||
return mailAddress;
|
||||
}
|
||||
|
||||
public void setMail_address(String mail_address) {
|
||||
this.mail_address = mail_address;
|
||||
public void setMailAddress(String mailAddress) {
|
||||
this.mailAddress = mailAddress;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
@@ -49,21 +48,12 @@ public class EmployeeDTO {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public Integer getId_department() {
|
||||
return id_department;
|
||||
public int getIdDepartment() {
|
||||
return idDepartment;
|
||||
}
|
||||
|
||||
public void setId_department(Integer id_department) {
|
||||
this.id_department = id_department;
|
||||
public void setIdDepartment(int idDepartment) {
|
||||
this.idDepartment = idDepartment;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return id_employee + "\t"
|
||||
+ nm_employee + "\t"
|
||||
+ kn_employee + "\t"
|
||||
+ mail_address + "\t"
|
||||
+ password + "\t"
|
||||
+ id_department;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +58,7 @@ public class Main {
|
||||
case "3":
|
||||
break;
|
||||
case "4":
|
||||
empDelete();
|
||||
break;
|
||||
case "9":
|
||||
return;
|
||||
@@ -88,21 +89,59 @@ public class Main {
|
||||
empList = empDao.selectAll();
|
||||
break;
|
||||
case "2":
|
||||
System.out.println("IDを入力してください");
|
||||
|
||||
empList = empDao.selectById(tblFlg);
|
||||
String id = inputCheck("ID", true);
|
||||
empList = empDao.selectById(Integer.parseInt(id));
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
for (EmployeeDTO emp : empList) {
|
||||
System.out.println(emp);
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* フラグを判定し、テーブル名を取得する
|
||||
*
|
||||
|
||||
@@ -44,9 +44,11 @@ public class DbUtil {
|
||||
* 生成されたConnectionを返却する
|
||||
* ※Connection生成には上記の定数を活用してください
|
||||
*/
|
||||
public static Connection getConnection() throws SQLException {
|
||||
//データベースの接続
|
||||
public static Connection getConnection()
|
||||
throws ClassNotFoundException, SQLException {
|
||||
Class.forName(DB_DRIVER);
|
||||
con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||
|
||||
return con;
|
||||
}
|
||||
|
||||
@@ -59,10 +61,9 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||
*/
|
||||
|
||||
public static void closestatement(PreparedStatement stmt) throws SQLException {
|
||||
if (!stmt.equals(null)) {
|
||||
//DBステートメントを閉じる
|
||||
public static void closeStatement(PreparedStatement stmt)
|
||||
throws SQLException {
|
||||
if (stmt != null) {
|
||||
stmt.close();
|
||||
}
|
||||
}
|
||||
@@ -76,9 +77,11 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||
*/
|
||||
public static void closeConnection(Connection con) throws SQLException {
|
||||
if (!con.equals(null)) {
|
||||
public static void closeConnection(Connection con)
|
||||
throws SQLException {
|
||||
if (con != null) {
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user