20261029
This commit is contained in:
7
.classpath
Normal file
7
.classpath
Normal file
@@ -0,0 +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="output" path="bin"/>
|
||||||
|
</classpath>
|
||||||
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/bin/
|
||||||
BIN
mysql-connector-java-8.0.23.jar
Normal file
BIN
mysql-connector-java-8.0.23.jar
Normal file
Binary file not shown.
8
snippet/Snippet.java
Normal file
8
snippet/Snippet.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package snippet;
|
||||||
|
|
||||||
|
public class Snippet {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,10 +1,4 @@
|
|||||||
/*
|
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;
|
||||||
@@ -13,8 +7,8 @@ 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 src.com.rhizome.example.dto.EmployeeDTO;
|
||||||
import com.rhizome.example.util.DbUtil;
|
import src.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";
|
||||||
@@ -92,9 +86,8 @@ public class EmployeeDAO {
|
|||||||
public List<EmployeeDTO> selectById(int employeeId) throws SQLException{
|
public List<EmployeeDTO> selectById(int employeeId) throws SQLException{
|
||||||
List<EmployeeDTO>rtnList = new ArrayList<>();
|
List<EmployeeDTO>rtnList = new ArrayList<>();
|
||||||
|
|
||||||
String sql =
|
java.lang.String sql =
|
||||||
"SELECT * FROM employee" +
|
"SELECT * FROM employee WHERE id_employee = ?";
|
||||||
"WHERE id_employee = ?";
|
|
||||||
try {
|
try {
|
||||||
stmt = con.prepareStatement(sql);
|
stmt = con.prepareStatement(sql);
|
||||||
stmt.setInt(1, employeeId);
|
stmt.setInt(1, employeeId);
|
||||||
@@ -108,8 +101,8 @@ public class EmployeeDAO {
|
|||||||
dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
||||||
dto.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
dto.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
||||||
dto.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
dto.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
||||||
dto.setKnEmployee(rs.getString(MAIL_ADDRESS));
|
dto.setMailEmployee(rs.getString(MAIL_ADDRESS));
|
||||||
dto.setKnEmployee(rs.getString(PASSWORD));
|
dto.setPassEmployee(rs.getString(PASSWORD));
|
||||||
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
||||||
|
|
||||||
rtnList.add(dto);
|
rtnList.add(dto);
|
||||||
@@ -121,25 +114,69 @@ public class EmployeeDAO {
|
|||||||
|
|
||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
public int empDelete (int employeeId)throws SQLException{
|
||||||
public void empDelete(int employeeId)throws SQLException{
|
java.lang.String sql =
|
||||||
String sql =
|
"DELETE FROM employee WHERE id_employee = ?";
|
||||||
"DELETE FROM employee" +
|
|
||||||
"WHERE id_employee = ?";
|
|
||||||
try {
|
try {
|
||||||
stmt = con.prepareStatement(sql);
|
stmt = con.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, employeeId);
|
||||||
|
|
||||||
stmt.setInt(1, employeeId);
|
return stmt.executeUpdate();
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(stmt);
|
||||||
|
|
||||||
stmt.executeUpdate();
|
}
|
||||||
|
}
|
||||||
|
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 {
|
} finally {
|
||||||
|
|
||||||
DbUtil.closeStatement(stmt);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
51
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
51
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package src.com.rhizome.example.dto;
|
||||||
|
|
||||||
|
public class EmployeeDTO {
|
||||||
|
private int idEmployee;
|
||||||
|
private String nmEmployee;
|
||||||
|
private String knEmployee;
|
||||||
|
private String mailEmployee;
|
||||||
|
private String passEmployee;
|
||||||
|
private int idDepartment;
|
||||||
|
|
||||||
|
|
||||||
|
public int getIdEmployee() {
|
||||||
|
return idEmployee;
|
||||||
|
}
|
||||||
|
public void setIdEmployee(int idEmployee) {
|
||||||
|
this.idEmployee = idEmployee;
|
||||||
|
}
|
||||||
|
public String getNmEmployee() {
|
||||||
|
return nmEmployee;
|
||||||
|
}
|
||||||
|
public void setNmEmployee(String nmEmployee) {
|
||||||
|
this.nmEmployee = nmEmployee;
|
||||||
|
}
|
||||||
|
public String getKnEmployee() {
|
||||||
|
return knEmployee;
|
||||||
|
}
|
||||||
|
public void setKnEmployee(String knEmployee) {
|
||||||
|
this.knEmployee = knEmployee;
|
||||||
|
}
|
||||||
|
public String getMailEmployee() {
|
||||||
|
return mailEmployee;
|
||||||
|
}
|
||||||
|
public void setMailEmployee(String mailEmployee) {
|
||||||
|
this.mailEmployee = mailEmployee;
|
||||||
|
}
|
||||||
|
public String getPassEmployee() {
|
||||||
|
return passEmployee;
|
||||||
|
}
|
||||||
|
public void setPassEmployee(String passEmployee) {
|
||||||
|
this.passEmployee = passEmployee;
|
||||||
|
}
|
||||||
|
public int getIdDepartment() {
|
||||||
|
return idDepartment;
|
||||||
|
}
|
||||||
|
public void setIdDepartment(int idDepartment) {
|
||||||
|
this.idDepartment = idDepartment;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.rhizome.example.main;
|
package src.com.rhizome.example.main;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
@@ -6,8 +6,9 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
import com.rhizome.example.dao.EmployeeDAO;
|
import src.com.rhizome.example.dao.EmployeeDAO;
|
||||||
import com.rhizome.example.util.DbUtil;
|
import src.com.rhizome.example.dto.EmployeeDTO;
|
||||||
|
import src.com.rhizome.example.util.DbUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author IT-College
|
* @author IT-College
|
||||||
@@ -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,15 +90,35 @@ 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.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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -146,4 +170,100 @@ 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package com.rhizome.example.util;
|
package src.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,6 +44,23 @@ public class DbUtil {
|
|||||||
* 生成されたConnectionを返却する
|
* 生成されたConnectionを返却する
|
||||||
* ※Connection生成には上記の定数を活用してください
|
* ※Connection生成には上記の定数を活用してください
|
||||||
*/
|
*/
|
||||||
|
public static Connection getConnection()
|
||||||
|
throws ClassNotFoundException, SQLException{
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -52,7 +72,12 @@ 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 +88,9 @@ public class DbUtil {
|
|||||||
* 例外 :SQLException
|
* 例外 :SQLException
|
||||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||||
*/
|
*/
|
||||||
|
public static void closeConnection(Connection con)
|
||||||
|
throws SQLException{
|
||||||
|
if(con != null)
|
||||||
|
con.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user