3 Commits
hirayama ... fu

Author SHA1 Message Date
c3a91ecca5 20261030 2026-10-30 17:52:38 +09:00
fa98c66334 20261029 2026-10-29 15:04:34 +09:00
3973a0b1e1 20161028 2026-10-28 17:25:36 +09:00
18 changed files with 528 additions and 113 deletions

View File

@@ -1,8 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<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="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>

View File

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

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

View File

@@ -1 +0,0 @@
Global

23
bin/.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.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.

View File

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

Binary file not shown.

8
snippet/Snippet.java Normal file
View File

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

View File

@@ -0,0 +1,158 @@
package src.com.rhizome.example.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import src.com.rhizome.example.dto.DepartmentDTO;
import src.com.rhizome.example.dto.EmployeeDTO;
import src.com.rhizome.example.util.DbUtil;
public class DepartmentDAO {
private static final String TBL_NAME = "department";
private static final String ID_EMPLOYEE = "id_department";
private static final String NM_EMPLOYEE = "nm_department";
/** DBコネクション */
public Connection con;
/** DBステートメント */
PreparedStatement stmt;
/** 検索結果 */
ResultSet rs;
/**
* インスタンス生成時DBコネクション
*
* @throws ClassNotFoundException
* @throws SQLException
*/
public DepartmentDAO(Connection con) {
this.con = con;
}
/**
* 社員テーブルの情報を全件取得する
*
* @return 検索結果
* @throws SQLException
* @throws ClassNotFoundException
*/
public List<DepartmentDTO> selectAll() throws SQLException, ClassNotFoundException {
List<EmployeeDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder();
sql.append(" SELECT ");
sql.append(" " + "*");
sql.append(" FROM ");
sql.append(" " + TBL_NAME);
try {
this.stmt = con.prepareStatement(sql.toString());
rs = stmt.executeQuery();
while (rs.next()) {
EmployeeDTO dto =
new EmployeeDTO();
dto.setIdDepartment(
rs.getInt("ID_DEPARTMENT"));
dto.setNmDepartment(
rs.getString("NM_DEPARTMENT"));
rtnList.add(dto);
}
} finally {
DbUtil.closeStatement(this.stmt);
}
return rtnList;
}
public List<DepartmentDTO> selectById(int departmentId) throws SQLException{
List<DepartmentDTO>rtnList = new ArrayList<>();
java.lang.String sql =
"SELECT * FROM department WHERE id_department = ?";
try {
stmt = con.prepareStatement(sql);
stmt.setInt(1, departmentId);
rs = stmt.executeQuery();
while (rs.next()) {
DepartmentDTO dto = new DepartmentDTO();
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
dto.setNmDepartment(rs.getString(NM_DEPARTMENT));
rtnList.add(dto);
}
} finally {
DbUtil.closeStatement(stmt);
}
return rtnList;
}
public int depDelete (int departmentId)throws SQLException{
java.lang.String sql =
"DELETE FROM department WHERE id_department = ?";
try {
stmt = con.prepareStatement(sql);
stmt.setInt(1, departmentId);
return stmt.executeUpdate();
} finally {
DbUtil.closeStatement(stmt);
}
}
public int depInsert(String departmentName,String departmentKn)throws SQLException{
java.lang.String sql =
"INSERT INTO employee " +
"(nm_department) " +
"VALUES (?, ?, ?, ?, ?)";
try {
stmt = con.prepareStatement(sql);
stmt.setString(1, departmentName);
stmt.setString(2, departmentKn);
return stmt.executeUpdate();
} finally {
DbUtil.closeStatement(stmt);
}
}
public int empUpdate(int departmentId,String departmentName)throws SQLException{
java.lang.String sql =
"UPDATE department " +
"SET nm_department = ?, " +
"WHERE id_employee = ?";
try {
stmt = con.prepareStatement(sql);
stmt.setString(1, departmentName);
stmt.setInt(5, departmentId);
return stmt.executeUpdate();
} finally {
DbUtil.closeStatement(stmt);
}
}
}

View File

@@ -1,10 +1,4 @@
/*
* EmployeeDAO.java
* 作成 IT-College 2020
*------------------------------------------------------------
* Copyright(c) Rhizome Inc. All Rights Reserved.
*/
package com.rhizome.example.dao;
package src.com.rhizome.example.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
@@ -13,8 +7,8 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.rhizome.example.dto.EmployeeDTO;
import com.rhizome.example.util.DbUtil;
import src.com.rhizome.example.dto.EmployeeDTO;
import src.com.rhizome.example.util.DbUtil;
public class EmployeeDAO {
private static final String TBL_NAME = "employee";
@@ -27,7 +21,7 @@ public class EmployeeDAO {
/** DBコネクション */
public Connection con;
/** DBステートPreparedStatement stmtメント */
/** DBステートメント */
PreparedStatement stmt;
/** 検索結果 */
ResultSet rs;
@@ -61,25 +55,129 @@ public class EmployeeDAO {
this.stmt = con.prepareStatement(sql.toString());
rs = stmt.executeQuery();
while (rs.next()) {
EmployeeDTO dto =
new EmployeeDTO();
//インスタンス生成
EmployeeDTO emp = new EmployeeDTO();
dto.setIdEmployee(
rs.getInt("ID_EMPLOYEE"));
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"));
dto.setNmEmployee(
rs.getString("NM_EMPLOYEE"));
rtnList.add(emp);
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(this.stmt);
}
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

@@ -0,0 +1,26 @@
package src.com.rhizome.example.dto;
public class DepartmentDTO {
private int idDepartment;
private String nmDepartment;
public int getIdDepartment() {
return idDepartment;
}
public void setIdDepartment(int idDepartment) {
this.idDepartment = idDepartment;
}
public String getNmDepartment() {
return nmDepartment;
}
public void setNmDepartment(String Department) {
this.nmDepartment= nmDepartment;
}
}

View File

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

View File

@@ -1,4 +1,4 @@
package com.rhizome.example.main;
package src.com.rhizome.example.main;
import java.sql.Connection;
import java.sql.SQLException;
@@ -6,9 +6,9 @@ import java.util.ArrayList;
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;
import src.com.rhizome.example.dao.EmployeeDAO;
import src.com.rhizome.example.dto.EmployeeDTO;
import src.com.rhizome.example.util.DbUtil;
/**
* @author IT-College
@@ -54,10 +54,13 @@ public class Main {
empSelect();
break;
case "2":
empInsert();
break;
case "3":
empUpdate();
break;
case "4":
empDelete();
break;
case "9":
return;
@@ -87,15 +90,35 @@ public class Main {
case "1":
empList = empDao.selectAll();
break;
case "2":
String strId = inputCheck("ID", true);
int id = Integer.parseInt(strId);
empList = empDao.selectById(id);
break;
default:
return;
}
for (EmployeeDTO emp : empList) {
System.out.println(emp);
if(empList.isEmpty()){
System.out.println("入力された値で情報がみつかりませんでした");
}else {
for (EmployeeDTO emp : empList) {
System.out.println(
emp.getIdEmployee() + "\t"
+ emp.getNmEmployee() + "\t"
+ emp.getKnEmployee() + "\t"
+ emp.getMailEmployee() + "\t"
+ emp.getPassEmployee() + "\t"
+ emp.getIdDepartment() + "\t");
}
}
} finally {
DbUtil.closeConnection(empDao.con);
}finally {
DbUtil.closeConnection(empDao.con);
}
}
/**
@@ -147,4 +170,100 @@ public class Main {
}
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 com.rhizome.example.util;
package src.com.rhizome.example.util;
import java.sql.Connection;
import java.sql.DriverManager;
@@ -44,25 +44,25 @@ public class DbUtil {
* 生成されたConnectionを返却する
* ※Connection生成には上記の定数を活用してください
*/
public static Connection getConnection() {
try {
public static Connection getConnection()
throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.cj.jdbc.Driver");
if(con != null && !con.isClosed()) {
return con;
}
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection(
DB_URL,
DB_USER,
DB_PASS);
return con;
con = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/rezodb" // データベース
, "rezouser" // ユーザ
, "Rezo_0000" // パスワード
);
}catch (ClassNotFoundException e){
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
/*
* TODO 以下の仕様でメソッドを作成してください。
*
@@ -72,15 +72,12 @@ public class DbUtil {
* 例外 :SQLException
* 処理 :引数の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 以下の仕様でメソッドを作成してください。
@@ -91,12 +88,9 @@ public class DbUtil {
* 例外 :SQLException
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
*/
public static void closeConnection(Connection con) {
try {
if (con != null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeConnection(Connection con)
throws SQLException{
if(con != null)
con.close();
}
}