Compare commits
7 Commits
o-matsuda
...
s-sakiyama
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a56fa4eefa | ||
|
|
b4b3f35d00 | ||
|
|
a6bd282b1c | ||
|
|
1aade1c6ed | ||
|
|
e4e9c1f3ac | ||
|
|
450270610f | ||
|
|
468b80bc4a |
@@ -60,15 +60,10 @@ public class EmployeeDAO {
|
||||
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = stmt.executeQuery();
|
||||
//DBのカラムをリストに追加
|
||||
while (rs.next()) {
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
dto.setEmpid(rs.getInt(ID_EMPLOYEE));
|
||||
dto.setName(rs.getString(NM_EMPLOYEE));
|
||||
dto.setKana(rs.getString(KN_EMPLOYEE));
|
||||
dto.setMail(rs.getString(MAIL_ADDRESS));
|
||||
dto.setPass(rs.getString(PASSWORD));
|
||||
dto.setDepid(rs.getInt(ID_DEPARTMENT));
|
||||
EmployeeDTO dto = new EmployeeDTO(rs.getInt(ID_EMPLOYEE), rs.getString(NM_EMPLOYEE),
|
||||
rs.getString(KN_EMPLOYEE), rs.getString(MAIL_ADDRESS), rs.getString(PASSWORD),
|
||||
rs.getInt(ID_DEPARTMENT));
|
||||
rtnList.add(dto);
|
||||
}
|
||||
} finally {
|
||||
@@ -77,65 +72,159 @@ public class EmployeeDAO {
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
public List<EmployeeDTO> selectByld(int id) throws SQLException {
|
||||
/**
|
||||
* 社員テーブルから社員IDに合致する情報を取得する
|
||||
* @param id
|
||||
* 検索する社員ID
|
||||
* @return 検索結果
|
||||
* @throws SQLException
|
||||
* @throws ClassNotFoundException
|
||||
* @throws NumberFormatException
|
||||
*/
|
||||
public List<EmployeeDTO> selectById(int id)
|
||||
throws SQLException, ClassNotFoundException, NumberFormatException {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
StringBuilder sql = new StringBuilder();
|
||||
//社員テーブルから指定された社員IDに一致するデータを取得
|
||||
sql.append(" SELECT ");
|
||||
sql.append(" " + "*");
|
||||
sql.append(" FROM ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
sql.append(" WHERE ");
|
||||
sql.append(" " + ID_EMPLOYEE);
|
||||
sql.append(" = ");
|
||||
sql.append(" = ");
|
||||
sql.append(" " + id);
|
||||
|
||||
try {
|
||||
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = stmt.executeQuery();
|
||||
//DBのカラムをリストに追加
|
||||
while (rs.next()) {
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
dto.setEmpid(rs.getInt(ID_EMPLOYEE));
|
||||
dto.setName(rs.getString(NM_EMPLOYEE));
|
||||
dto.setKana(rs.getString(KN_EMPLOYEE));
|
||||
dto.setMail(rs.getString(MAIL_ADDRESS));
|
||||
dto.setPass(rs.getString(PASSWORD));
|
||||
dto.setDepid(rs.getInt(ID_DEPARTMENT));
|
||||
|
||||
EmployeeDTO dto = new EmployeeDTO(rs.getInt(ID_EMPLOYEE), rs.getString(NM_EMPLOYEE),
|
||||
rs.getString(KN_EMPLOYEE), rs.getString(MAIL_ADDRESS), rs.getString(PASSWORD),
|
||||
rs.getInt(ID_DEPARTMENT));
|
||||
rtnList.add(dto);
|
||||
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
if (rtnList.size() == 0) {
|
||||
System.out.println("入力された値で情報が見つかりませんでした");
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
}
|
||||
return rtnList;
|
||||
|
||||
}
|
||||
|
||||
public void empDelete(int id) throws SQLException {
|
||||
/**
|
||||
* 社員テーブルの社員IDに合致する情報を削除する
|
||||
*
|
||||
* @param id
|
||||
* 削除ID
|
||||
* @throws ClassNotFoundException
|
||||
* @throws SQLException
|
||||
* @throws NumberFormatException
|
||||
*/
|
||||
public void empDelete(int id)
|
||||
throws ClassNotFoundException, SQLException, NumberFormatException {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
//社員テーブルから指定された社員IDに一致するデータを取得
|
||||
sql.append(" DELETE ");
|
||||
sql.append(" " + "*");
|
||||
sql.append(" FROM ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
sql.append(" WHERE ");
|
||||
sql.append(" " + ID_EMPLOYEE);
|
||||
sql.append(" = ");
|
||||
sql.append(" = ");
|
||||
sql.append(" " + id);
|
||||
|
||||
try {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
this.stmt.executeUpdate();
|
||||
stmt.executeUpdate();
|
||||
System.out.println("削除が完了しました");
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルに指定された情報を新規登録する
|
||||
*
|
||||
* @param name - 社員名
|
||||
* @param kana - 社員名カナ
|
||||
* @param mail - メールアドレス
|
||||
* @param pass - パスワード
|
||||
* @param depId - 部署ID
|
||||
* @throws NumberFormatException
|
||||
* @throws SQLException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
public void empInsert(String name, String kana, String mail, String pass, int depId)
|
||||
throws NumberFormatException, SQLException, ClassNotFoundException {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" INSERT ");
|
||||
sql.append(" INTO ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
sql.append(" ( " + NM_EMPLOYEE);
|
||||
sql.append(" , " + KN_EMPLOYEE);
|
||||
sql.append(" , " + MAIL_ADDRESS);
|
||||
sql.append(" , " + PASSWORD);
|
||||
sql.append(" , " + ID_DEPARTMENT + " ) ");
|
||||
sql.append(" VALUES ");
|
||||
sql.append(" ( '" + name);
|
||||
sql.append("' , '" + kana);
|
||||
sql.append("' , '" + mail);
|
||||
sql.append("' , '" + pass);
|
||||
sql.append("' , " + depId + " ) ");
|
||||
|
||||
try {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
stmt.executeUpdate();
|
||||
System.out.println("登録が完了しました");
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
}
|
||||
public void emplnsert (String name, String kana, String mail, String pass, int id) {
|
||||
|
||||
/**
|
||||
* 社員テーブルの社員IDに合致する情報を更新する
|
||||
*
|
||||
* @param id - 社員ID
|
||||
* @param name - 社員名
|
||||
* @param kana - 社員名カナ
|
||||
* @param mail - メールアドレス
|
||||
* @param pass - パスワード
|
||||
* @param depId - 部署ID
|
||||
* @throws ClassNotFoundException
|
||||
* @throws SQLException
|
||||
* @throws NumberFormatException
|
||||
|
||||
*/
|
||||
public void empUpdate(int id, String name, String kana, String mail, String pass, int depId)
|
||||
throws ClassNotFoundException, SQLException, NumberFormatException {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
|
||||
sql.append(" UPDATE ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
sql.append(" SET ");
|
||||
sql.append(" " + NM_EMPLOYEE);
|
||||
sql.append(" = '" + name + " ' ");
|
||||
sql.append(" , " + KN_EMPLOYEE);
|
||||
sql.append(" = '" + kana + " ' ");
|
||||
sql.append(" , " + MAIL_ADDRESS);
|
||||
sql.append(" = '" + mail + " ' ");
|
||||
sql.append(" , " + PASSWORD);
|
||||
sql.append(" = '" + pass + " ' ");
|
||||
sql.append(" , " + ID_DEPARTMENT);
|
||||
sql.append(" = " + depId);
|
||||
sql.append(" WHERE ");
|
||||
sql.append(" " + ID_EMPLOYEE);
|
||||
sql.append(" = ");
|
||||
sql.append(" " + id);
|
||||
|
||||
try {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
stmt.executeUpdate();
|
||||
System.out.println("更新が完了しました");
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
79
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
79
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package com.rhizome.example.dto;
|
||||
|
||||
public class EmployeeDTO {
|
||||
private int ID_EMPLOYEE;
|
||||
private String NM_EMPLOYEE;
|
||||
private String KN_EMPLOYEE;
|
||||
private String MAIL_ADDRESS;
|
||||
private String PASSWORD;
|
||||
private int ID_DEPARTMENT;
|
||||
|
||||
public EmployeeDTO() {
|
||||
|
||||
}
|
||||
|
||||
public EmployeeDTO(int ID_EMPLOYEE, String NM_EMPLOYEE, String KN_EMPLOYEE, String MAIL_ADDRESS, String PASSWORD,
|
||||
int ID_DEPARTMENT) {
|
||||
setID_EMPLOYEE(ID_EMPLOYEE);
|
||||
setNM_EMPLOYEE(NM_EMPLOYEE);
|
||||
setKN_EMPLOYEE(KN_EMPLOYEE);
|
||||
setMAIL_ADDRESS(MAIL_ADDRESS);
|
||||
setPASSWORD(PASSWORD);
|
||||
setID_DEPARTMENT(ID_DEPARTMENT);
|
||||
}
|
||||
|
||||
public int getID_EMPLOYEE() {
|
||||
return ID_EMPLOYEE;
|
||||
}
|
||||
|
||||
public void setID_EMPLOYEE(int iD_EMPLOYEE) {
|
||||
ID_EMPLOYEE = iD_EMPLOYEE;
|
||||
}
|
||||
|
||||
public String getNM_EMPLOYEE() {
|
||||
return NM_EMPLOYEE;
|
||||
}
|
||||
|
||||
public void setNM_EMPLOYEE(String nM_EMPLOYEE) {
|
||||
NM_EMPLOYEE = nM_EMPLOYEE;
|
||||
}
|
||||
|
||||
public String getKN_EMPLOYEE() {
|
||||
return KN_EMPLOYEE;
|
||||
}
|
||||
|
||||
public void setKN_EMPLOYEE(String kN_EMPLOYEE) {
|
||||
KN_EMPLOYEE = kN_EMPLOYEE;
|
||||
}
|
||||
|
||||
public String getMAIL_ADDRESS() {
|
||||
return MAIL_ADDRESS;
|
||||
}
|
||||
|
||||
public void setMAIL_ADDRESS(String mAIL_ADDRESS) {
|
||||
MAIL_ADDRESS = mAIL_ADDRESS;
|
||||
}
|
||||
|
||||
public String getPASSWORD() {
|
||||
return PASSWORD;
|
||||
}
|
||||
|
||||
public void setPASSWORD(String pASSWORD) {
|
||||
PASSWORD = pASSWORD;
|
||||
}
|
||||
|
||||
public int getID_DEPARTMENT() {
|
||||
return ID_DEPARTMENT;
|
||||
}
|
||||
|
||||
public void setID_DEPARTMENT(int iD_DEPARTMENT) {
|
||||
ID_DEPARTMENT = iD_DEPARTMENT;
|
||||
}
|
||||
|
||||
public void showInfo() {
|
||||
System.out.println(ID_EMPLOYEE + "\t" + NM_EMPLOYEE +
|
||||
"\t" + KN_EMPLOYEE + "\t" + MAIL_ADDRESS +
|
||||
"\t" + PASSWORD + "\t" + ID_DEPARTMENT);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -45,8 +45,6 @@ public class Main {
|
||||
* @throws SQLException
|
||||
*/
|
||||
public void start() throws ClassNotFoundException, SQLException {
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
System.out.println();
|
||||
System.out.println("現在のメンテナンス対象テーブル:" + targetTableName(tblFlg));
|
||||
System.out.println("1:検索 2:登録 3:更新 4:削除 9:テーブル切替 0:終了");
|
||||
@@ -56,11 +54,13 @@ public class Main {
|
||||
empSelect();
|
||||
break;
|
||||
case "2":
|
||||
empInsert();
|
||||
break;
|
||||
case "3":
|
||||
empUpdate();
|
||||
break;
|
||||
case "4":
|
||||
empDelete();
|
||||
empDelete();
|
||||
break;
|
||||
case "9":
|
||||
return;
|
||||
@@ -73,25 +73,88 @@ public class Main {
|
||||
}
|
||||
}
|
||||
|
||||
private void empDelete() throws SQLException {
|
||||
/**
|
||||
* 社員テーブルの社員IDに合致する情報を更新する
|
||||
*
|
||||
* @throws ClassNotFoundException
|
||||
* @throws SQLException
|
||||
* @throws NumberFormatException
|
||||
*/
|
||||
public static void empUpdate()
|
||||
throws ClassNotFoundException, SQLException, NumberFormatException {
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
int id = Integer.parseInt(inputCheck("ID", true));
|
||||
|
||||
if (empDao.selectById(id).size() == 0) {
|
||||
return;
|
||||
}
|
||||
String name = inputCheck("社員名", false);
|
||||
String kana = inputCheck("フリガナ", false);
|
||||
String mail = inputCheck("メールアドレス", false);
|
||||
String pass = inputCheck("パスワード", false);
|
||||
int depId = Integer.parseInt(inputCheck("部署ID", true));
|
||||
|
||||
try {
|
||||
String strId = inputCheck("ID", true);
|
||||
int id = Integer.parseInt(strId);
|
||||
List<EmployeeDTO> empList = empDao.selectByld(id);
|
||||
if (empList.isEmpty()) {
|
||||
} else {
|
||||
System.out.println("指定されたIDの情報を削除します? Y/y");
|
||||
System.out.println("Y/y以外の入力であればキャンセル");
|
||||
Scanner sc = new Scanner(System.in);
|
||||
String str = sc.nextLine();
|
||||
if (str.equals("Y") || str.equals("y")) {
|
||||
empDao.empDelete(id);
|
||||
} else {
|
||||
System.out.println("キャンセルしました");
|
||||
}
|
||||
}
|
||||
empDao.empUpdate(id, name, kana, mail, pass, depId);
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルに指定された情報を新規登録する
|
||||
* @throws NumberFormatException
|
||||
* @throws SQLException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
public static void empInsert()
|
||||
throws NumberFormatException, SQLException, ClassNotFoundException {
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
|
||||
String name = inputCheck("社員名", false);
|
||||
String kana = inputCheck("フリガナ", false);
|
||||
String mail = inputCheck("メールアドレス", false);
|
||||
String pass = inputCheck("パスワード", false);
|
||||
int depId = Integer.parseInt(inputCheck("部署ID", true));
|
||||
|
||||
try {
|
||||
empDao.empInsert(name, kana, mail, pass, depId);
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルの社員IDに合致する情報を削除する
|
||||
*
|
||||
* @throws ClassNotFoundException
|
||||
* @throws SQLException
|
||||
*/
|
||||
public static void empDelete()
|
||||
throws ClassNotFoundException, SQLException, NumberFormatException {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
int id = Integer.parseInt(inputCheck("ID", true));
|
||||
rtnList = empDao.selectById(id);
|
||||
if (rtnList.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("指定されたIDの情報を削除します? Y/y\r\n"
|
||||
+ "(Y/y以外の入力であればキャンセル)");
|
||||
|
||||
String str = inputCheck(false);
|
||||
|
||||
if (!str.equalsIgnoreCase("y")) {
|
||||
System.out.println("キャンセルしました");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
empDao.empDelete(id);
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
@@ -115,16 +178,13 @@ public class Main {
|
||||
empList = empDao.selectAll();
|
||||
break;
|
||||
case "2":
|
||||
System.out.println("IDを入力してください");
|
||||
int id = sc.nextInt();
|
||||
empList = empDao.selectByld(id);
|
||||
empList = empDao.selectById(Integer.parseInt(inputCheck("ID", true)));
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
for (EmployeeDTO emp : empList) {
|
||||
System.out.println(emp.getEmpid() + "\t" + emp.getName() + "\t" + emp.getKana() + "\t"
|
||||
+ emp.getMail() + "\t" + emp.getPass() + "\t" + emp.getDepid());
|
||||
emp.showInfo();
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
@@ -158,7 +218,7 @@ public class Main {
|
||||
* @return
|
||||
*/
|
||||
public static String inputCheck(String titleName, boolean flg) {
|
||||
String str;
|
||||
String str = null;
|
||||
while (true) {
|
||||
System.out.println(titleName + "を入力してください");
|
||||
str = sc.nextLine();
|
||||
@@ -180,4 +240,26 @@ public class Main {
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
public static String inputCheck(boolean flg) {
|
||||
String str = null;
|
||||
while (true) {
|
||||
str = sc.nextLine();
|
||||
if (str.trim().length() == 0) {
|
||||
|
||||
continue;
|
||||
}
|
||||
if (flg) {
|
||||
if (str.trim().length() > 0) {
|
||||
try {
|
||||
Integer.parseInt(str);
|
||||
} catch (NumberFormatException e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Objects;
|
||||
|
||||
public class DbUtil {
|
||||
// 定数定義(共通)
|
||||
@@ -44,10 +45,13 @@ 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 {
|
||||
if (con != null && con.isValid(1)) {
|
||||
return con;
|
||||
}
|
||||
Class.forName(DB_DRIVER);
|
||||
return con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -59,12 +63,12 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||
*/
|
||||
|
||||
public static void closeStatement(PreparedStatement ps) throws SQLException {
|
||||
//DBステートメントのクローズ
|
||||
if (ps != null) {
|
||||
ps.close(); //DBステートメントをクローズ
|
||||
}
|
||||
if (Objects.nonNull(ps))
|
||||
ps.close();
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
*
|
||||
@@ -75,10 +79,8 @@ public class DbUtil {
|
||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||
*/
|
||||
public static void closeConnection(Connection con) throws SQLException {
|
||||
//コネクションの切断
|
||||
if (con != null) {
|
||||
con.close(); //コネクション切断
|
||||
}
|
||||
}
|
||||
if (Objects.nonNull(con))
|
||||
con.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user