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
11 changed files with 327 additions and 9 deletions

8
.classpath Normal file
View File

@@ -0,0 +1,8 @@
<?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 Normal file
View File

@@ -0,0 +1 @@
/bin/

23
.project Normal file
View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>em-jdbc</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

7
.settings/.jsdtscope Normal file
View File

@@ -0,0 +1,7 @@
<?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>

View File

@@ -0,0 +1 @@
org.eclipse.wst.jsdt.launching.JRE_CONTAINER

View File

@@ -0,0 +1 @@
Global

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

@@ -13,6 +13,7 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.rhizome.example.dto.EmployeeDTO;
import com.rhizome.example.util.DbUtil; import com.rhizome.example.util.DbUtil;
public class EmployeeDAO { public class EmployeeDAO {
@@ -48,8 +49,8 @@ public class EmployeeDAO {
* @throws SQLException * @throws SQLException
* @throws ClassNotFoundException * @throws ClassNotFoundException
*/ */
public List<String> selectAll() throws SQLException, ClassNotFoundException { public List<EmployeeDTO> selectAll() throws SQLException, ClassNotFoundException {
List<String> rtnList = new ArrayList<>(); List<EmployeeDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder(); StringBuilder sql = new StringBuilder();
sql.append(" SELECT "); sql.append(" SELECT ");
sql.append(" " + "*"); sql.append(" " + "*");
@@ -60,7 +61,15 @@ 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()) {
rtnList.add(rs.getString(NM_EMPLOYEE)); 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);
} }
} finally { } finally {
DbUtil.closeStatement(this.stmt); DbUtil.closeStatement(this.stmt);
@@ -68,4 +77,114 @@ public class EmployeeDAO {
return rtnList; return rtnList;
} }
//指定されたIDの社員データを取得する
public List<EmployeeDTO> selectById(int id_employee)throws SQLException, ClassNotFoundException{
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 = ?");
try {
this.stmt = con.prepareStatement(sql.toString());
this.stmt.setInt(1,id_employee );
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);
}
}finally {
DbUtil.closeStatement(this.stmt);
}
return rtnList;
}
//指定された社員のデータを削除する
public void empDelete(int id_employee)throws SQLException, ClassNotFoundException{
//SQL文の作成
StringBuilder sql = new StringBuilder();
sql.append(" DELETE ");
sql.append(" ");
sql.append(" FROM ");
sql.append(" " + TBL_NAME);
sql.append(" WHERE ");
sql.append(" ID_EMPLOYEE = ?");
try {
//SQL文の組み立て結果を保存
this.stmt = con.prepareStatement(sql.toString());
//?に代入
this.stmt.setInt(1,id_employee );
//削除命令
stmt.executeUpdate();
}finally {
DbUtil.closeStatement(this.stmt);
}
}
public void empInsert(String name, String kana, String email,
String password, Integer idDep)throws SQLException, ClassNotFoundException{
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(" 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();
}finally {
DbUtil.closeStatement(this.stmt);
}
}
public void empUpdate(Integer id, String name, String kana, String email,
String password, Integer idDep)throws SQLException, ClassNotFoundException{
StringBuilder sql = new StringBuilder();
sql.append(" UPDATE ");
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 + "= ?");
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.setInt(6, id);
stmt.executeUpdate();
}finally {
DbUtil.closeStatement(this.stmt);
}
}
} }

View File

@@ -0,0 +1,47 @@
package com.rhizome.example.dto;
public class EmployeeDTO {
private Integer id;
private String name;
private String kana;
private String email;
private String pass;
private Integer idDep;
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;
}
}

View File

@@ -7,6 +7,7 @@ import java.util.List;
import java.util.Scanner; import java.util.Scanner;
import com.rhizome.example.dao.EmployeeDAO; import com.rhizome.example.dao.EmployeeDAO;
import com.rhizome.example.dto.EmployeeDTO;
import com.rhizome.example.util.DbUtil; import com.rhizome.example.util.DbUtil;
/** /**
@@ -53,10 +54,13 @@ public class Main {
empSelect(); empSelect();
break; break;
case "2": case "2":
empInsert();
break; break;
case "3": case "3":
empUpdate();
break; break;
case "4": case "4":
empDelete();
break; break;
case "9": case "9":
return; return;
@@ -76,7 +80,7 @@ public class Main {
* @throws SQLException * @throws SQLException
*/ */
public static void empSelect() throws ClassNotFoundException, SQLException { public static void empSelect() throws ClassNotFoundException, SQLException {
List<String> empList = new ArrayList<>(); List<EmployeeDTO> empList = new ArrayList<>();
Connection con = DbUtil.getConnection(); Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con); EmployeeDAO empDao = new EmployeeDAO(con);
System.out.println("1:全検索 2:条件検索 それ以外:戻る"); System.out.println("1:全検索 2:条件検索 それ以外:戻る");
@@ -86,11 +90,21 @@ public class Main {
case "1": case "1":
empList = empDao.selectAll(); empList = empDao.selectAll();
break; break;
case "2":
String strId =inputCheck("ID", true);
int ID = Integer.parseInt(strId);
empList = empDao.selectById(ID);
break;
default: default:
return; return;
} }
for (String emp : empList) { if(empList.isEmpty()) {
System.out.println(emp); 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());
}
} }
} finally { } finally {
DbUtil.closeConnection(empDao.con); DbUtil.closeConnection(empDao.con);
@@ -146,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

@@ -1,6 +1,9 @@
package com.rhizome.example.util; package com.rhizome.example.util;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DbUtil { public class DbUtil {
// 定数定義(共通) // 定数定義(共通)
@@ -41,7 +44,15 @@ public class DbUtil {
* 生成されたConnectionを返却する * 生成されたConnectionを返却する
* ※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"
);
return con;
}
/* /*
* TODO 以下の仕様でメソッドを作成してください。 * TODO 以下の仕様でメソッドを作成してください。
@@ -52,7 +63,11 @@ public class DbUtil {
* 例外 :SQLException * 例外 :SQLException
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする * 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
*/ */
public static void closeStatement(PreparedStatement stmt) throws SQLException{
if(stmt != null) {
stmt.close();
}
}
/* /*
* TODO 以下の仕様でメソッドを作成してください。 * TODO 以下の仕様でメソッドを作成してください。
@@ -63,6 +78,10 @@ public class DbUtil {
* 例外 :SQLException * 例外 :SQLException
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する * 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
*/ */
public static void closeConnection(Connection con) throws SQLException{
if(con != null) {
con.close();
}
}
} }