Compare commits
7 Commits
main
...
s-sakiyama
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a56fa4eefa | ||
|
|
b4b3f35d00 | ||
|
|
a6bd282b1c | ||
|
|
1aade1c6ed | ||
|
|
e4e9c1f3ac | ||
|
|
450270610f | ||
|
|
468b80bc4a |
@@ -13,6 +13,7 @@ import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.rhizome.example.dto.EmployeeDTO;
|
||||
import com.rhizome.example.util.DbUtil;
|
||||
|
||||
public class EmployeeDAO {
|
||||
@@ -48,8 +49,8 @@ public class EmployeeDAO {
|
||||
* @throws SQLException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
public List<String> selectAll() throws SQLException, ClassNotFoundException {
|
||||
List<String> rtnList = new ArrayList<>();
|
||||
public List<EmployeeDTO> selectAll() throws SQLException, ClassNotFoundException {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" SELECT ");
|
||||
sql.append(" " + "*");
|
||||
@@ -60,7 +61,10 @@ public class EmployeeDAO {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
rtnList.add(rs.getString(NM_EMPLOYEE));
|
||||
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);
|
||||
@@ -68,4 +72,159 @@ public class EmployeeDAO {
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルから社員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();
|
||||
sql.append(" SELECT ");
|
||||
sql.append(" " + "*");
|
||||
sql.append(" FROM ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
sql.append(" WHERE ");
|
||||
sql.append(" " + ID_EMPLOYEE);
|
||||
sql.append(" = ");
|
||||
sql.append(" " + id);
|
||||
try {
|
||||
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
|
||||
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("入力された値で情報がみつかりませんでした");
|
||||
}
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルの社員IDに合致する情報を削除する
|
||||
*
|
||||
* @param id
|
||||
* 削除ID
|
||||
* @throws ClassNotFoundException
|
||||
* @throws SQLException
|
||||
* @throws NumberFormatException
|
||||
*/
|
||||
public void empDelete(int id)
|
||||
throws ClassNotFoundException, SQLException, NumberFormatException {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" DELETE ");
|
||||
sql.append(" FROM ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルに指定された情報を新規登録する
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルの社員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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import com.rhizome.example.dao.EmployeeDAO;
|
||||
import com.rhizome.example.dto.EmployeeDTO;
|
||||
import com.rhizome.example.util.DbUtil;
|
||||
|
||||
/**
|
||||
@@ -53,10 +54,13 @@ public class Main {
|
||||
empSelect();
|
||||
break;
|
||||
case "2":
|
||||
empInsert();
|
||||
break;
|
||||
case "3":
|
||||
empUpdate();
|
||||
break;
|
||||
case "4":
|
||||
empDelete();
|
||||
break;
|
||||
case "9":
|
||||
return;
|
||||
@@ -69,6 +73,93 @@ public class Main {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルの社員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 {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルから情報を検索する
|
||||
*
|
||||
@@ -76,7 +167,7 @@ public class Main {
|
||||
* @throws SQLException
|
||||
*/
|
||||
public static void empSelect() throws ClassNotFoundException, SQLException {
|
||||
List<String> empList = new ArrayList<>();
|
||||
List<EmployeeDTO> empList = new ArrayList<>();
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
||||
@@ -86,11 +177,14 @@ public class Main {
|
||||
case "1":
|
||||
empList = empDao.selectAll();
|
||||
break;
|
||||
case "2":
|
||||
empList = empDao.selectById(Integer.parseInt(inputCheck("ID", true)));
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
for (String emp : empList) {
|
||||
System.out.println(emp);
|
||||
for (EmployeeDTO emp : empList) {
|
||||
emp.showInfo();
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
@@ -146,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 {
|
||||
// 定数定義(共通)
|
||||
@@ -35,7 +36,7 @@ public class DbUtil {
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
*1,2をやる
|
||||
*
|
||||
* 戻り値 :Connection
|
||||
* メソッド名 :getConnection
|
||||
* 引数 :なし
|
||||
@@ -44,41 +45,42 @@ 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);
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
*6をやる
|
||||
*
|
||||
* 戻り値 :なし
|
||||
* メソッド名 :closeStatement
|
||||
* 引数 :DBステートメント(PreparedStatement型)
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||
*/
|
||||
public static void closeStatement(PreparedStatement ps) throws SQLException {
|
||||
if (ps != null) {
|
||||
ps.close();
|
||||
}
|
||||
|
||||
public static void closeStatement(PreparedStatement ps) throws SQLException {
|
||||
if (Objects.nonNull(ps))
|
||||
ps.close();
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
*7
|
||||
*
|
||||
* 戻り値 :なし
|
||||
* メソッド名 :closeConnection()
|
||||
* 引数 :切断するコネクション(Connection型)
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||
*/
|
||||
public static void closeConnection(Connection co) throws SQLException {
|
||||
if (co != null) {
|
||||
co.close();
|
||||
}
|
||||
|
||||
public static void closeConnection(Connection con) throws SQLException {
|
||||
if (Objects.nonNull(con))
|
||||
con.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user