2 Commits

Author SHA1 Message Date
3cc08bdf7a 2日目 2026-10-29 15:47:58 +09:00
5210508a78 1日目 2026-10-28 17:00:28 +09:00
16 changed files with 112 additions and 343 deletions

View File

@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="src" path=""/>
<classpathentry kind="lib" path="mysql-connector-java-8.0.23.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <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"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

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

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

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,2 @@
java -jar DAO_Simple.jar
pause

Binary file not shown.

View File

@@ -1,8 +0,0 @@
package snippet;
public class Snippet {
public static void main(String[] args) {
}
}

View File

@@ -1,4 +1,10 @@
package src.com.rhizome.example.dao; /*
* EmployeeDAO.java
* 作成 IT-College 2020
*------------------------------------------------------------
* Copyright(c) Rhizome Inc. All Rights Reserved.
*/
package com.rhizome.example.dao;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@@ -7,8 +13,8 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import src.com.rhizome.example.dto.EmployeeDTO; import com.rhizome.example.dto.EmployeeDTO;
import src.com.rhizome.example.util.DbUtil; import com.rhizome.example.util.DbUtil;
public class EmployeeDAO { public class EmployeeDAO {
private static final String TBL_NAME = "employee"; private static final String TBL_NAME = "employee";
@@ -21,7 +27,7 @@ public class EmployeeDAO {
/** DBコネクション */ /** DBコネクション */
public Connection con; public Connection con;
/** DBステートメント */ /** DBステートPreparedStatement stmtメント */
PreparedStatement stmt; PreparedStatement stmt;
/** 検索結果 */ /** 検索結果 */
ResultSet rs; ResultSet rs;
@@ -55,129 +61,25 @@ 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();
dto.setIdEmployee( //インスタンス生成
rs.getInt("ID_EMPLOYEE")); EmployeeDTO emp = new EmployeeDTO();
dto.setNmEmployee( emp.setId(rs.getInt("id_employee"));
rs.getString("NM_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"));
dto.setKnEmployee( rtnList.add(emp);
rs.getString("KN_EMPLOYEE"));
dto.setMailEmployee(
rs.getString("MAIL_ADDRESS"));
dto.setPassEmployee(
rs.getString("PASSWORD"));
dto.setIdDepartment(
rs.getInt("ID_DEPARTMENT"));
rtnList.add(dto);
} }
} finally { } finally {
DbUtil.closeStatement(this.stmt); DbUtil.closeStatement(this.stmt);
} }
return rtnList; return rtnList;
} }
public List<EmployeeDTO> selectById(int employeeId) throws SQLException{
List<EmployeeDTO>rtnList = new ArrayList<>();
java.lang.String sql =
"SELECT * FROM employee WHERE id_employee = ?";
try {
stmt = con.prepareStatement(sql);
stmt.setInt(1, employeeId);
rs = stmt.executeQuery();
while (rs.next()) {
EmployeeDTO dto = new EmployeeDTO();
dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
dto.setNmEmployee(rs.getString(NM_EMPLOYEE));
dto.setKnEmployee(rs.getString(KN_EMPLOYEE));
dto.setMailEmployee(rs.getString(MAIL_ADDRESS));
dto.setPassEmployee(rs.getString(PASSWORD));
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
rtnList.add(dto);
}
} finally {
DbUtil.closeStatement(stmt);
}
return rtnList;
}
public int empDelete (int employeeId)throws SQLException{
java.lang.String sql =
"DELETE FROM employee WHERE id_employee = ?";
try {
stmt = con.prepareStatement(sql);
stmt.setInt(1, employeeId);
return stmt.executeUpdate();
} finally {
DbUtil.closeStatement(stmt);
}
}
public int empInsert(String employeeName,String employeeKn,String employeeMail
,String employeePass, int departmentId)throws SQLException{
java.lang.String sql =
"INSERT INTO employee " +
"(nm_employee, kn_employee, mail_address, password, id_department) " +
"VALUES (?, ?, ?, ?, ?)";
try {
stmt = con.prepareStatement(sql);
stmt.setString(1, employeeName);
stmt.setString(2, employeeKn);
stmt.setString(3, employeeMail);
stmt.setString(4, employeePass);
stmt.setInt(5, departmentId);
return stmt.executeUpdate();
} finally {
DbUtil.closeStatement(stmt);
}
}
public int empUpdate(int employeeId,String employeeName,String employeeKn,String employeeMail
,String employeePass, int departmentId)throws SQLException{
java.lang.String sql =
"UPDATE employee " +
"SET nm_employee = ?, " +
"kn_employee = ?, " +
"mail_address = ?, " +
"password = ?, " +
"id_department = ? " +
"WHERE id_employee = ?";
try {
stmt = con.prepareStatement(sql);
stmt.setString(1, employeeName);
stmt.setString(2, employeeKn);
stmt.setString(3, employeeMail);
stmt.setString(4, employeePass);
stmt.setInt(5, departmentId);
stmt.setInt(6, employeeId);
return stmt.executeUpdate();
} finally {
DbUtil.closeStatement(stmt);
}
}
} }

View File

@@ -1,51 +1,50 @@
package src.com.rhizome.example.dto; package com.rhizome.example.dto;
//フィールド
public class EmployeeDTO { public class EmployeeDTO {
private int idEmployee; private Integer id;
private String nmEmployee; private String name;
private String knEmployee; private String kana;
private String mailEmployee; private String email;
private String passEmployee; private String pass;
private int idDepartment; private Integer idDep;
//ゲッター・セッター
public int getIdEmployee() { public Integer getId() {
return idEmployee; return id;
} }
public void setIdEmployee(int idEmployee) { public String getName() {
this.idEmployee = idEmployee; return name;
} }
public String getNmEmployee() { public String getKana() {
return nmEmployee; return kana;
} }
public void setNmEmployee(String nmEmployee) { public String getEmail() {
this.nmEmployee = nmEmployee; return email;
} }
public String getKnEmployee() { public String getPass() {
return knEmployee; return pass;
} }
public void setKnEmployee(String knEmployee) { public Integer getIdDep() {
this.knEmployee = knEmployee; return idDep;
} }
public String getMailEmployee() { public void setId(Integer id) {
return mailEmployee; this.id = id;
} }
public void setMailEmployee(String mailEmployee) { public void setName(String name) {
this.mailEmployee = mailEmployee; this.name = name;
} }
public String getPassEmployee() { public void setKana(String kana) {
return passEmployee; this.kana = kana;
} }
public void setPassEmployee(String passEmployee) { public void setEmail(String email) {
this.passEmployee = passEmployee; this.email = email;
} }
public int getIdDepartment() { public void setPass(String pass) {
return idDepartment; this.pass = pass;
} }
public void setIdDepartment(int idDepartment) { public void setIdDep(Integer idDep) {
this.idDepartment = idDepartment; this.idDep = idDep;
} }
} }

View File

@@ -1,4 +1,4 @@
package src.com.rhizome.example.main; package com.rhizome.example.main;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
@@ -6,9 +6,9 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Scanner; import java.util.Scanner;
import src.com.rhizome.example.dao.EmployeeDAO; import com.rhizome.example.dao.EmployeeDAO;
import src.com.rhizome.example.dto.EmployeeDTO; import com.rhizome.example.dto.EmployeeDTO;
import src.com.rhizome.example.util.DbUtil; import com.rhizome.example.util.DbUtil;
/** /**
* @author IT-College * @author IT-College
@@ -54,13 +54,10 @@ 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;
@@ -90,35 +87,15 @@ 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;
} }
if(empList.isEmpty()){
System.out.println("入力された値で情報がみつかりませんでした");
}else {
for (EmployeeDTO emp : empList) { for (EmployeeDTO emp : empList) {
System.out.println( System.out.println(emp);
emp.getIdEmployee() + "\t"
+ emp.getNmEmployee() + "\t"
+ emp.getKnEmployee() + "\t"
+ emp.getMailEmployee() + "\t"
+ emp.getPassEmployee() + "\t"
+ emp.getIdDepartment() + "\t");
}
} }
} finally { } finally {
DbUtil.closeConnection(empDao.con); DbUtil.closeConnection(empDao.con);
} }
} }
/** /**
@@ -170,100 +147,4 @@ public class Main {
} }
return str; return str;
} }
public static void empDelete() throws ClassNotFoundException, SQLException {
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
try {
String strId = inputCheck("ID", true);
int id = Integer.parseInt(strId);
int count = empDao.empDelete(id);
if (count == 0) {
System.out.println("入力されたIDの情報は見つかりませんでした。");
} else {
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 mail = inputCheck("メールアドレス", false);
String pass = inputCheck("パスワード", false);
int depId = Integer.parseInt(inputCheck("部署ID", true));
int count = empDao.empInsert(
name,
kana,
mail,
pass,
depId);
if (count > 0) {
System.out.println("登録が完了しました");
}
} finally {
DbUtil.closeConnection(empDao.con);
}
}
public static void empUpdate() throws ClassNotFoundException, SQLException {
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
String strId = inputCheck("ID", true);
int id = Integer.parseInt(strId);
try {
String name =
inputCheck("社員名", false);
String kana =
inputCheck("フリガナ", false);
String mail =
inputCheck("メールアドレス", false);
String pass =
inputCheck("パスワード", false);
int depId =
Integer.parseInt(inputCheck("部署ID", true));
int count = empDao.empUpdate(
id,
name,
kana,
mail,
pass,
depId);
if (count > 0) {
System.out.println("更新が完了しました");
} else {
System.out.println("入力されたIDの情報は見つかりませんでした。");
}
} finally {
DbUtil.closeConnection(empDao.con);
}
}
} }

View File

@@ -1,4 +1,4 @@
package src.com.rhizome.example.util; package com.rhizome.example.util;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
@@ -44,23 +44,23 @@ public class DbUtil {
* 生成されたConnectionを返却する * 生成されたConnectionを返却する
* ※Connection生成には上記の定数を活用してください * ※Connection生成には上記の定数を活用してください
*/ */
public static Connection getConnection() public static Connection getConnection() {
throws ClassNotFoundException, SQLException{ try {
if(con != null && !con.isClosed()) {
return con;
}
Class.forName("com.mysql.cj.jdbc.Driver"); Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection( con = DriverManager.getConnection(
DB_URL, "jdbc:mysql://127.0.0.1:3306/rezodb" // データベース
DB_USER, , "rezouser" // ユーザ
DB_PASS); , "Rezo_0000" // パスワード
);
return con; }catch (ClassNotFoundException e){
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
} }
/* /*
@@ -72,12 +72,15 @@ public class DbUtil {
* 例外 :SQLException * 例外 :SQLException
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする * 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
*/ */
public static void closeStatement(
PreparedStatement stmt) throws SQLException{
if(stmt !=null)
stmt.close();
public static void closeStatement(PreparedStatement stmt){
try {
if (stmt != null) stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} }
}
/* /*
* TODO 以下の仕様でメソッドを作成してください。 * TODO 以下の仕様でメソッドを作成してください。
@@ -88,9 +91,12 @@ public class DbUtil {
* 例外 :SQLException * 例外 :SQLException
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する * 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
*/ */
public static void closeConnection(Connection con) public static void closeConnection(Connection con) {
throws SQLException{ try {
if(con != null) if (con != null) con.close();
con.close(); } catch (SQLException e) {
e.printStackTrace();
}
} }
} }