3 Commits

Author SHA1 Message Date
bbc4fbd353 1030自主学習 2026-10-30 17:49:14 +09:00
bae6f62ca2 20261029 5段階目まで完了 2026-10-29 15:56:21 +09:00
9f7fa737ef 20261028 JavaSQL演習 2026-10-28 16:58:01 +09:00
5 changed files with 207 additions and 234 deletions

View File

@@ -37,3 +37,4 @@ INSERT INTO department (nm_department) VALUES ('経理部');
INSERT INTO department (nm_department) VALUES ('システム開発事業部'); INSERT INTO department (nm_department) VALUES ('システム開発事業部');
INSERT INTO department (nm_department) VALUES ('教育事業部'); INSERT INTO department (nm_department) VALUES ('教育事業部');
INSERT INTO department (nm_department) VALUES ('営業企画部'); INSERT INTO department (nm_department) VALUES ('営業企画部');

View File

@@ -61,124 +61,129 @@ public class EmployeeDAO {
this.stmt = con.prepareStatement(sql.toString()); this.stmt = con.prepareStatement(sql.toString());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
EmployeeDTO dto = new EmployeeDTO(); EmployeeDTO emp = new EmployeeDTO();
dto.setId(rs.getInt("id_employee")); emp.setId(rs.getInt("id_employee"));
dto.setName(rs.getString("nm_employee")); emp.setName(rs.getString("nm_employee"));
dto.setKana(rs.getString("kn_employee")); emp.setKana(rs.getString("kn_employee"));
dto.setEmail(rs.getString("mail_address")); emp.setEmail(rs.getString("mail_address"));
dto.setPass(rs.getString("password")); emp.setPass(rs.getString("password"));
dto.setIdDep(rs.getInt("id_department")); emp.setIdDep(rs.getInt("id_department"));
rtnList.add(dto);
rtnList.add(emp);
} }
} finally { } finally {
DbUtil.closeStatement​(this.stmt); DbUtil.closeStatement(this.stmt);
} }
return rtnList; return rtnList;
} }
public java.util.List<EmployeeDTO> selectById​(int id) //指定されたIDの社員データを取得する
throws java.sql.SQLException, java.lang.ClassNotFoundException, java.lang.NumberFormatException { public List<EmployeeDTO> selectById(int id_employee)throws SQLException, ClassNotFoundException{
List<EmployeeDTO> rtnList = new ArrayList<>(); List<EmployeeDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder(); StringBuilder sql = new StringBuilder();
sql.append(" SELECT * FROM "); sql.append(" SELECT ");
sql.append(TBL_NAME); sql.append(" " + "*");
sql.append(" WHERE id_employee = ?"); sql.append(" FROM ");
sql.append(" " + TBL_NAME);
sql.append(" WHERE ");
sql.append(" ID_EMPLOYEE = ?");
try { try {
this.stmt = con.prepareStatement(sql.toString()); this.stmt = con.prepareStatement(sql.toString());
stmt.setInt(1, id); this.stmt.setInt(1,id_employee );
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { 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(); rtnList.add(emp);
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 {
} finally { DbUtil.closeStatement(this.stmt);
DbUtil.closeStatement​(this.stmt);
} }
return rtnList; return rtnList;
} }
public void empDelete(int id) throws java.lang.ClassNotFoundException, java.sql.SQLException { //指定された社員のデータを削除する
public void empDelete(int id_employee)throws SQLException, ClassNotFoundException{
//SQL文の作成
StringBuilder sql = new StringBuilder(); StringBuilder sql = new StringBuilder();
sql.append(" DELETE FROM "); sql.append(" DELETE ");
sql.append(TBL_NAME); sql.append(" ");
sql.append(" WHERE id_employee = ?"); sql.append(" FROM ");
sql.append(" " + TBL_NAME);
sql.append(" WHERE ");
sql.append(" ID_EMPLOYEE = ?");
try { try {
this.stmt = this.con.prepareStatement(sql.toString()); //SQL文の組み立て結果を保存
this.stmt.setInt(1, id); this.stmt = con.prepareStatement(sql.toString());
this.stmt.executeUpdate(); //?に代入
this.stmt.setInt(1,id_employee );
} finally { //削除命令
DbUtil.closeStatement​(this.stmt); stmt.executeUpdate();
} }finally {
DbUtil.closeStatement(this.stmt);
} }
}
public void empInsert​(java.lang.String name, java.lang.String kana, java.lang.String mail, java.lang.String pass, public void empInsert(String name, String kana, String email,
int depId) throws java.lang.NumberFormatException, java.sql.SQLException, java.lang.ClassNotFoundException { String password, Integer idDep)throws SQLException, ClassNotFoundException{
//List<EmployeeDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder(); StringBuilder sql = new StringBuilder();
sql.append(" INSERT INTO " + TBL_NAME); sql.append(" INSERT ");
sql.append(" (nm_employee, kn_employee, mail_address, password, id_department) "); sql.append(" " + " INTO ");
sql.append(" " + TBL_NAME);
sql.append(" ( ");
sql.append(" " + NM_EMPLOYEE + ", " + KN_EMPLOYEE + ", " +
MAIL_ADDRESS + ", " + PASSWORD + ", " + ID_DEPARTMENT);
sql.append(" ) ");
sql.append(" VALUES "); sql.append(" VALUES ");
sql.append(" (?, ?, ?, ?, ?) "); sql.append(" ( ?, ?, ?, ?, ?) ");
try { try {
this.stmt = con.prepareStatement(sql.toString()); this.stmt = con.prepareStatement(sql.toString());
this.stmt.setString(1,name );
this.stmt.setString(1, name); this.stmt.setString(2,kana );
this.stmt.setString(2, kana); this.stmt.setString(3,email );
this.stmt.setString(3, mail); this.stmt.setString(4,password );
this.stmt.setString(4, pass); this.stmt.setInt(5,idDep );
this.stmt.setInt(5, depId); stmt.executeUpdate();
}finally {
this.stmt.executeUpdate(); DbUtil.closeStatement(this.stmt);
} finally {
DbUtil.closeStatement​(this.stmt);
} }
} }
public void empUpdate​(int id, java.lang.String name, java.lang.String kana, java.lang.String mail, public void empUpdate(Integer id, String name, String kana, String email,
java.lang.String pass, int depId) String password, Integer idDep)throws SQLException, ClassNotFoundException{
throws java.lang.ClassNotFoundException, java.sql.SQLException, java.lang.NumberFormatException {
StringBuilder sql = new StringBuilder(); StringBuilder sql = new StringBuilder();
sql.append(" UPDATE "); sql.append(" UPDATE ");
sql.append(TBL_NAME); sql.append(" " + TBL_NAME);
sql.append(" SET "); sql.append(" SET ");
sql.append(" nm_employee = ?, kn_employee = ?, mail_address = ?, password = ?, id_department = ? "); sql.append(" " + NM_EMPLOYEE + "= ?,");
sql.append(" WHERE id_employee = ? "); sql.append(" " + KN_EMPLOYEE + "= ?,");
sql.append(" " + MAIL_ADDRESS + "= ?,");
sql.append(" " + PASSWORD + "= ?,");
sql.append(" " + ID_DEPARTMENT + "= ?");
sql.append(" WHERE ");
sql.append(ID_EMPLOYEE + "= ?");
try { try {
this.stmt = con.prepareStatement(sql.toString()); this.stmt = con.prepareStatement(sql.toString());
this.stmt.setString(1,name );
this.stmt.setString(1, name); this.stmt.setString(2,kana );
this.stmt.setString(2, kana); this.stmt.setString(3,email );
this.stmt.setString(3, mail); this.stmt.setString(4,password );
this.stmt.setString(4, pass); this.stmt.setInt(5,idDep );
this.stmt.setInt(5, depId);
this.stmt.setInt(6, id); this.stmt.setInt(6, id);
stmt.executeUpdate();
this.stmt.executeUpdate(); }finally {
DbUtil.closeStatement(this.stmt);
} finally {
DbUtil.closeStatement​(this.stmt);
} }
} }

View File

@@ -1,60 +1,47 @@
package com.rhizome.example.dto; package com.rhizome.example.dto;
public class EmployeeDTO { public class EmployeeDTO {
private Integer id; private Integer id;
private String name; private String name;
private String kana; private String kana;
private String email; private String email;
private String pass; private String pass;
private Integer idDep; private Integer idDep;
public Integer getId() { public Integer getId() {
return id; return id;
} }
public void setId(Integer id) { public void setId(Integer id) {
this.id = id; this.id = id;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public String getKana() { public String getKana() {
return kana; return kana;
} }
public void setKana(String kana) { public void setKana(String kana) {
this.kana = kana; this.kana = kana;
} }
public String getEmail() { public String getEmail() {
return email; return email;
} }
public void setEmail(String email) { public void setEmail(String email) {
this.email = email; this.email = email;
} }
public String getPass() { public String getPass() {
return pass; return pass;
} }
public void setPass(String pass) { public void setPass(String pass) {
this.pass = pass; this.pass = pass;
} }
public Integer getIdDep() { public Integer getIdDep() {
return idDep; return idDep;
} }
public void setIdDep(Integer idDep) { public void setIdDep(Integer idDep) {
this.idDep = idDep; this.idDep = idDep;
} }
} }

View File

@@ -91,119 +91,23 @@ public class Main {
empList = empDao.selectAll(); empList = empDao.selectAll();
break; break;
case "2": case "2":
String idStr = inputCheck("ID", true); String strId =inputCheck("ID", true);
int id = Integer.parseInt(idStr); int ID = Integer.parseInt(strId);
empList = empDao.selectById​(id); empList = empDao.selectById(ID);
break; break;
default: default:
return; return;
} }
if(empList.isEmpty()) {
System.out.println("入力された値で情報がみつかりませんでした");
}else{
for (EmployeeDTO emp : empList) { for (EmployeeDTO emp : empList) {
System.out.println( System.out.println(emp.getId() + "\t" + emp.getName() + "\t" +
emp.getKana() + "\t" + emp.getEmail() + "\t" + emp.getPass() + "\t" + emp.getIdDep());
emp.getId() + "\t" +
emp.getName() + "\t" +
emp.getKana() + "\t" +
emp.getEmail() + "\t" +
emp.getPass() + "\t" +
emp.getIdDep()
);
} }
} finally {
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 { } finally {
DbUtil.closeConnection​(empDao.con); 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);
} }
} }
@@ -256,4 +160,81 @@ public class Main {
} }
return str; 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);
}
}
} }

View File

@@ -2,6 +2,8 @@ package com.rhizome.example.util;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DbUtil { public class DbUtil {
// 定数定義(共通) // 定数定義(共通)
@@ -42,11 +44,15 @@ public class DbUtil {
* 生成されたConnectionを返却する * 生成されたConnectionを返却する
* ※Connection生成には上記の定数を活用してください * ※Connection生成には上記の定数を活用してください
*/ */
public static Connection getConnection() throws ClassNotFoundException, SQLException{
public static java.sql.Connection getConnection() throws java.lang.ClassNotFoundException, java.sql.SQLException { Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS); con = DriverManager.getConnection(
return con; "jdbc:mysql://127.0.0.1:3306/rezodb",
} "rezouser",
"Rezo_0000"
);
return con;
}
/* /*
* TODO 以下の仕様でメソッドを作成してください。 * TODO 以下の仕様でメソッドを作成してください。
@@ -57,12 +63,11 @@ public class DbUtil {
* 例外 :SQLException * 例外 :SQLException
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする * 処理 :引数の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) {
if (stmt != null) { stmt.close();
stmt.close(); }
} }
}
/* /*
* TODO 以下の仕様でメソッドを作成してください。 * TODO 以下の仕様でメソッドを作成してください。
@@ -73,16 +78,10 @@ public class DbUtil {
* 例外 :SQLException * 例外 :SQLException
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する * 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
*/ */
public static void closeConnection(Connection con) throws SQLException{
//public static void closeConnection(Connection con) throws SQLException { if(con != null) {
//if (con != null) { con.close();
//con.close(); }
//} }
//
public static void closeConnection​(java.sql.Connection con) throws java.sql.SQLException {
if (con != null)
con.close();
}
} }