Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6493baccef | |||
| f909550b87 | |||
| df7972c662 |
@@ -37,4 +37,3 @@ INSERT INTO department (nm_department) VALUES ('経理部');
|
||||
INSERT INTO department (nm_department) VALUES ('システム開発事業部');
|
||||
INSERT INTO department (nm_department) VALUES ('教育事業部');
|
||||
INSERT INTO department (nm_department) VALUES ('営業企画部');
|
||||
|
||||
|
||||
@@ -61,129 +61,124 @@ public class EmployeeDAO {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
EmployeeDTO emp = new EmployeeDTO();
|
||||
emp.setId(rs.getInt("id_employee"));
|
||||
emp.setName(rs.getString("nm_employee"));
|
||||
emp.setKana(rs.getString("kn_employee"));
|
||||
emp.setEmail(rs.getString("mail_address"));
|
||||
emp.setPass(rs.getString("password"));
|
||||
emp.setIdDep(rs.getInt("id_department"));
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
dto.setId(rs.getInt("id_employee"));
|
||||
dto.setName(rs.getString("nm_employee"));
|
||||
dto.setKana(rs.getString("kn_employee"));
|
||||
dto.setEmail(rs.getString("mail_address"));
|
||||
dto.setPass(rs.getString("password"));
|
||||
dto.setIdDep(rs.getInt("id_department"));
|
||||
|
||||
rtnList.add(dto);
|
||||
|
||||
rtnList.add(emp);
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
//指定されたIDの社員データを取得する
|
||||
public List<EmployeeDTO> selectById(int id_employee)throws SQLException, ClassNotFoundException{
|
||||
public java.util.List<EmployeeDTO> selectById(int id)
|
||||
throws java.sql.SQLException, java.lang.ClassNotFoundException, java.lang.NumberFormatException {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" SELECT ");
|
||||
sql.append(" " + "*");
|
||||
sql.append(" FROM ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
sql.append(" WHERE ");
|
||||
sql.append(" ID_EMPLOYEE = ?");
|
||||
sql.append(" SELECT * FROM ");
|
||||
sql.append(TBL_NAME);
|
||||
sql.append(" WHERE id_employee = ?");
|
||||
|
||||
try {
|
||||
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
this.stmt.setInt(1,id_employee );
|
||||
stmt.setInt(1, id);
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
EmployeeDTO emp = new EmployeeDTO();
|
||||
emp.setId(rs.getInt("id_employee"));
|
||||
emp.setName(rs.getString("nm_employee"));
|
||||
emp.setKana(rs.getString("kn_employee"));
|
||||
emp.setEmail(rs.getString("mail_address"));
|
||||
emp.setPass(rs.getString("password"));
|
||||
emp.setIdDep(rs.getInt("id_department"));
|
||||
|
||||
rtnList.add(emp);
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
dto.setId(rs.getInt("id_employee"));
|
||||
dto.setName(rs.getString("nm_employee"));
|
||||
dto.setKana(rs.getString("kn_employee"));
|
||||
dto.setEmail(rs.getString("mail_address"));
|
||||
dto.setPass(rs.getString("password"));
|
||||
dto.setIdDep(rs.getInt("id_department"));
|
||||
|
||||
rtnList.add(dto);
|
||||
|
||||
}
|
||||
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
return rtnList;
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
|
||||
//指定された社員のデータを削除する
|
||||
public void empDelete(int id_employee)throws SQLException, ClassNotFoundException{
|
||||
//SQL文の作成
|
||||
return rtnList;
|
||||
|
||||
}
|
||||
|
||||
public void empDelete(int id) throws java.lang.ClassNotFoundException, java.sql.SQLException {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" DELETE ");
|
||||
sql.append(" ");
|
||||
sql.append(" FROM ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
sql.append(" WHERE ");
|
||||
sql.append(" ID_EMPLOYEE = ?");
|
||||
sql.append(" DELETE FROM ");
|
||||
sql.append(TBL_NAME);
|
||||
sql.append(" WHERE id_employee = ?");
|
||||
|
||||
try {
|
||||
//SQL文の組み立て結果を保存
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
//?に代入
|
||||
this.stmt.setInt(1,id_employee );
|
||||
//削除命令
|
||||
stmt.executeUpdate();
|
||||
this.stmt = this.con.prepareStatement(sql.toString());
|
||||
this.stmt.setInt(1, id);
|
||||
this.stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
}
|
||||
|
||||
public void empInsert(String name, String kana, String email,
|
||||
String password, Integer idDep)throws SQLException, ClassNotFoundException{
|
||||
public void empInsert(java.lang.String name, java.lang.String kana, java.lang.String mail, java.lang.String pass,
|
||||
int depId) throws java.lang.NumberFormatException, java.sql.SQLException, java.lang.ClassNotFoundException {
|
||||
//List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" INSERT ");
|
||||
sql.append(" " + " INTO ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
sql.append(" ( ");
|
||||
sql.append(" " + NM_EMPLOYEE + ", " + KN_EMPLOYEE + ", " +
|
||||
MAIL_ADDRESS + ", " + PASSWORD + ", " + ID_DEPARTMENT);
|
||||
sql.append(" ) ");
|
||||
|
||||
sql.append(" INSERT INTO " + TBL_NAME);
|
||||
sql.append(" (nm_employee, kn_employee, mail_address, password, id_department) ");
|
||||
sql.append(" VALUES ");
|
||||
sql.append(" (?, ?, ?, ?, ?) ");
|
||||
|
||||
try {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
|
||||
this.stmt.setString(1, name);
|
||||
this.stmt.setString(2, kana);
|
||||
this.stmt.setString(3,email );
|
||||
this.stmt.setString(4,password );
|
||||
this.stmt.setInt(5,idDep );
|
||||
stmt.executeUpdate();
|
||||
this.stmt.setString(3, mail);
|
||||
this.stmt.setString(4, pass);
|
||||
this.stmt.setInt(5, depId);
|
||||
|
||||
this.stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
}
|
||||
|
||||
public void empUpdate(Integer id, String name, String kana, String email,
|
||||
String password, Integer idDep)throws SQLException, ClassNotFoundException{
|
||||
public void empUpdate(int id, java.lang.String name, java.lang.String kana, java.lang.String mail,
|
||||
java.lang.String pass, int depId)
|
||||
throws java.lang.ClassNotFoundException, java.sql.SQLException, java.lang.NumberFormatException {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" UPDATE ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
sql.append(TBL_NAME);
|
||||
sql.append(" SET ");
|
||||
sql.append(" " + NM_EMPLOYEE + "= ?,");
|
||||
sql.append(" " + KN_EMPLOYEE + "= ?,");
|
||||
sql.append(" " + MAIL_ADDRESS + "= ?,");
|
||||
sql.append(" " + PASSWORD + "= ?,");
|
||||
sql.append(" " + ID_DEPARTMENT + "= ?");
|
||||
sql.append(" WHERE ");
|
||||
sql.append(ID_EMPLOYEE + "= ?");
|
||||
sql.append(" nm_employee = ?, kn_employee = ?, mail_address = ?, password = ?, id_department = ? ");
|
||||
sql.append(" WHERE id_employee = ? ");
|
||||
|
||||
try {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
|
||||
this.stmt.setString(1, name);
|
||||
this.stmt.setString(2, kana);
|
||||
this.stmt.setString(3,email );
|
||||
this.stmt.setString(4,password );
|
||||
this.stmt.setInt(5,idDep );
|
||||
this.stmt.setString(3, mail);
|
||||
this.stmt.setString(4, pass);
|
||||
this.stmt.setInt(5, depId);
|
||||
this.stmt.setInt(6, id);
|
||||
stmt.executeUpdate();
|
||||
|
||||
this.stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.rhizome.example.dto;
|
||||
|
||||
public class EmployeeDTO {
|
||||
|
||||
private Integer id;
|
||||
private String name;
|
||||
private String kana;
|
||||
@@ -11,37 +12,49 @@ public class EmployeeDTO {
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getKana() {
|
||||
return kana;
|
||||
}
|
||||
|
||||
public void setKana(String kana) {
|
||||
this.kana = kana;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getPass() {
|
||||
return pass;
|
||||
}
|
||||
|
||||
public void setPass(String pass) {
|
||||
this.pass = pass;
|
||||
}
|
||||
|
||||
public Integer getIdDep() {
|
||||
return idDep;
|
||||
}
|
||||
|
||||
public void setIdDep(Integer idDep) {
|
||||
this.idDep = idDep;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -91,23 +91,119 @@ public class Main {
|
||||
empList = empDao.selectAll();
|
||||
break;
|
||||
case "2":
|
||||
String strId =inputCheck("ID", true);
|
||||
int ID = Integer.parseInt(strId);
|
||||
empList = empDao.selectById(ID);
|
||||
String idStr = inputCheck("ID", true);
|
||||
int id = Integer.parseInt(idStr);
|
||||
empList = empDao.selectById(id);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
if(empList.isEmpty()) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
}else{
|
||||
for (EmployeeDTO emp : empList) {
|
||||
System.out.println(emp.getId() + "\t" + emp.getName() + "\t" +
|
||||
emp.getKana() + "\t" + emp.getEmail() + "\t" + emp.getPass() + "\t" + emp.getIdDep());
|
||||
}
|
||||
System.out.println(
|
||||
|
||||
emp.getId() + "\t" +
|
||||
emp.getName() + "\t" +
|
||||
emp.getKana() + "\t" +
|
||||
emp.getEmail() + "\t" +
|
||||
emp.getPass() + "\t" +
|
||||
emp.getIdDep()
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
public static void empInsert()
|
||||
throws java.lang.NumberFormatException, java.lang.ClassNotFoundException, java.sql.SQLException {
|
||||
//List<EmployeeDTO> empList = new ArrayList<>();
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
|
||||
try {
|
||||
|
||||
String name = inputCheck("社員名", false);
|
||||
String kana = inputCheck("フリガナ", false);
|
||||
String mail = inputCheck("メールアドレス", false);
|
||||
String pass = inputCheck("パスワード", false);
|
||||
String depIdstr = inputCheck("部署ID", true);
|
||||
|
||||
int depId = Integer.parseInt(depIdstr);
|
||||
|
||||
System.out.println("登録が完了しました");
|
||||
|
||||
empDao.empInsert(name, kana, mail, pass, depId);
|
||||
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void empUpdate()
|
||||
throws java.lang.NumberFormatException, java.lang.ClassNotFoundException, java.sql.SQLException {
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
|
||||
try {
|
||||
|
||||
String idstr = inputCheck("ID", true);
|
||||
int id = Integer.parseInt(idstr);
|
||||
|
||||
List<EmployeeDTO> checkList = empDao.selectById(id);
|
||||
if (checkList.size() == 0) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
return;
|
||||
}
|
||||
|
||||
String name = inputCheck("社員名", false);
|
||||
String kana = inputCheck("フリガナ", false);
|
||||
String mail = inputCheck("メールアドレス", false);
|
||||
String pass = inputCheck("パスワード", false);
|
||||
String depIdstr = inputCheck("部署ID", true);
|
||||
|
||||
int depId = Integer.parseInt(depIdstr);
|
||||
|
||||
empDao.empUpdate(id, name, kana, mail, pass, depId);
|
||||
|
||||
System.out.println("更新が完了しました");
|
||||
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void empDelete() throws ClassNotFoundException, SQLException {
|
||||
//List<EmployeeDTO> empList = new ArrayList<>();
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
|
||||
try {
|
||||
|
||||
String idStr = inputCheck("ID", true);
|
||||
int id = Integer.parseInt(idStr);
|
||||
|
||||
List<EmployeeDTO> checkList = empDao.selectById(id);
|
||||
if (checkList.size() == 0) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("指定されたIDの情報を削除します? Y/y");
|
||||
System.out.println("(Y/y以外の入力であればキャンセル)");
|
||||
String yY = sc.nextLine();
|
||||
if (!"y".equals(yY) && !"Y".equals(yY)) {
|
||||
System.out.println("削除をキャンセルしました");
|
||||
return;
|
||||
}
|
||||
empDao.empDelete(id);
|
||||
System.out.println("削除が完了しました");
|
||||
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,81 +256,4 @@ public class Main {
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
public static void empDelete() throws ClassNotFoundException, SQLException {
|
||||
//リスト作成
|
||||
List<EmployeeDTO> empList = new ArrayList<>();
|
||||
//DB接続
|
||||
Connection con = DbUtil.getConnection();
|
||||
//DAOインスタンス生成
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
try {
|
||||
//入力された文字列をdelIdに保存
|
||||
String delId =inputCheck("ID", true);
|
||||
//入力された文字列を整数に変換して保存
|
||||
int ID = Integer.parseInt(delId);
|
||||
//入力されたIDに一致するデータを保存
|
||||
empList = empDao.selectById(ID);
|
||||
//リストが空=一致なし
|
||||
if(empList.isEmpty()) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
}else {
|
||||
System.out.println("指定されたIDの情報を削除します? Y/y");
|
||||
//削除の同意受付
|
||||
String agr = sc.nextLine();
|
||||
//同意が得られたら
|
||||
if("Y".equals(agr) || "y".equals(agr)) {
|
||||
//削除メソッド実行し削除
|
||||
empDao.empDelete(ID);
|
||||
System.out.println("削除が完了しました");
|
||||
}
|
||||
}
|
||||
}finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
public static void empInsert() throws ClassNotFoundException, SQLException {
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
try {
|
||||
String name = inputCheck("社員名",false);
|
||||
String kana = inputCheck("フリガナ", false);
|
||||
String email = inputCheck("メールアドレス", false);
|
||||
String password = inputCheck("パスワード", false);
|
||||
String depId = inputCheck("部署ID", true);
|
||||
int dID = Integer.parseInt(depId);
|
||||
|
||||
empDao.empInsert(name,kana,email,password,dID);
|
||||
System.out.println("登録が完了しました");
|
||||
}finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
public static void empUpdate() throws ClassNotFoundException, SQLException{
|
||||
List<EmployeeDTO> empList = new ArrayList<>();
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
try {
|
||||
String delId =inputCheck("ID", true);
|
||||
int ID = Integer.parseInt(delId);
|
||||
empList = empDao.selectById(ID);
|
||||
if(empList.isEmpty()) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
return;
|
||||
}
|
||||
|
||||
String name = inputCheck("社員名",false);
|
||||
String kana = inputCheck("フリガナ", false);
|
||||
String email = inputCheck("メールアドレス", false);
|
||||
String password = inputCheck("パスワード", false);
|
||||
String depId = inputCheck("部署ID", true);
|
||||
int dID = Integer.parseInt(depId);
|
||||
|
||||
empDao.empUpdate(ID,name,kana,email,password,dID);
|
||||
System.out.println("更新が完了しました");
|
||||
}finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ package com.rhizome.example.util;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DbUtil {
|
||||
// 定数定義(共通)
|
||||
@@ -44,13 +42,9 @@ public class DbUtil {
|
||||
* 生成されたConnectionを返却する
|
||||
* ※Connection生成には上記の定数を活用してください
|
||||
*/
|
||||
public static Connection getConnection() throws ClassNotFoundException, SQLException{
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
con = DriverManager.getConnection(
|
||||
"jdbc:mysql://127.0.0.1:3306/rezodb",
|
||||
"rezouser",
|
||||
"Rezo_0000"
|
||||
);
|
||||
|
||||
public static java.sql.Connection getConnection() throws java.lang.ClassNotFoundException, java.sql.SQLException {
|
||||
con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||
return con;
|
||||
}
|
||||
|
||||
@@ -63,7 +57,8 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||
*/
|
||||
public static void closeStatement(PreparedStatement stmt) throws SQLException{
|
||||
|
||||
public static void closeStatement(java.sql.PreparedStatement stmt) throws java.sql.SQLException {
|
||||
if (stmt != null) {
|
||||
stmt.close();
|
||||
}
|
||||
@@ -78,10 +73,16 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||
*/
|
||||
public static void closeConnection(Connection con) throws SQLException{
|
||||
if(con != null) {
|
||||
|
||||
//public static void closeConnection(Connection con) throws SQLException {
|
||||
//if (con != null) {
|
||||
//con.close();
|
||||
//}
|
||||
//
|
||||
|
||||
public static void closeConnection(java.sql.Connection con) throws java.sql.SQLException {
|
||||
if (con != null)
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user