6段階目まで完了済
This commit is contained in:
9
.classpath
Normal file
9
.classpath
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?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="mysql-connector-j-8.0.32.jar"/>
|
||||||
|
<classpathentry kind="lib" path="実行例/DAO_Simple.jar"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
||||||
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/bin/
|
||||||
23
.project
Normal file
23
.project
Normal 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>
|
||||||
7
.settings/.jsdtscope
Normal file
7
.settings/.jsdtscope
Normal 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>
|
||||||
1
.settings/org.eclipse.wst.jsdt.ui.superType.container
Normal file
1
.settings/org.eclipse.wst.jsdt.ui.superType.container
Normal file
@@ -0,0 +1 @@
|
|||||||
|
org.eclipse.wst.jsdt.launching.JRE_CONTAINER
|
||||||
1
.settings/org.eclipse.wst.jsdt.ui.superType.name
Normal file
1
.settings/org.eclipse.wst.jsdt.ui.superType.name
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Global
|
||||||
BIN
mysql-connector-j-8.0.32.jar
Normal file
BIN
mysql-connector-j-8.0.32.jar
Normal file
Binary file not shown.
@@ -12,7 +12,9 @@ import java.sql.ResultSet;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
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,19 +50,25 @@ 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(" " + "*");
|
||||||
sql.append(" FROM ");
|
sql.append(" FROM ");
|
||||||
sql.append(" " + TBL_NAME);
|
sql.append(" " + TBL_NAME);
|
||||||
try {
|
try {
|
||||||
|
|
||||||
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 employee = new EmployeeDTO();
|
||||||
|
employee.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
||||||
|
employee.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
||||||
|
employee.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
||||||
|
employee.setMailaddress(rs.getString(MAIL_ADDRESS));
|
||||||
|
employee.setPassword(rs.getString(PASSWORD));
|
||||||
|
employee.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
||||||
|
rtnList.add(employee);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closeStatement(this.stmt);
|
||||||
@@ -68,4 +76,137 @@ public class EmployeeDAO {
|
|||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 社員テーブルから指定された社員IDに一致するデータを全件取得する
|
||||||
|
*
|
||||||
|
* @return 検索結果
|
||||||
|
* @throws SQLException
|
||||||
|
* @throws ClassNotFoundException
|
||||||
|
*/
|
||||||
|
public List<EmployeeDTO> selectById(int idEmployee) 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());
|
||||||
|
stmt.setInt(1,idEmployee);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
int nRecords = 0;
|
||||||
|
while (rs.next()) {
|
||||||
|
EmployeeDTO employee = new EmployeeDTO();
|
||||||
|
employee.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
||||||
|
employee.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
||||||
|
employee.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
||||||
|
employee.setMailaddress(rs.getString(MAIL_ADDRESS));
|
||||||
|
employee.setPassword(rs.getString(PASSWORD));
|
||||||
|
employee.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
||||||
|
rtnList.add(employee);
|
||||||
|
nRecords++;
|
||||||
|
}
|
||||||
|
if (nRecords==0) {
|
||||||
|
System.out.println("入力された値で情報がみつかりませんでした");
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
}
|
||||||
|
return rtnList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 社員テーブルから指定された社員IDに一致するデータを削除する
|
||||||
|
*
|
||||||
|
* @throws SQLException
|
||||||
|
* @throws ClassNotFoundException
|
||||||
|
*/
|
||||||
|
public void empDelete(int idEmployee) throws SQLException, ClassNotFoundException {
|
||||||
|
if (selectById(idEmployee).size()!=0) {
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" DELETE FROM ");
|
||||||
|
sql.append(" " + TBL_NAME);
|
||||||
|
sql.append(" WHERE ");
|
||||||
|
sql.append(" " + ID_EMPLOYEE + " = " + "?");
|
||||||
|
System.out.println("指定されたIDの情報を削除しますか? Y/y");
|
||||||
|
System.out.println("(Y/y以外の入力であればキャンセル)");
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
String flag = sc.nextLine();
|
||||||
|
if (flag.equals("Y")||flag.equals("y")) {
|
||||||
|
try {
|
||||||
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
|
stmt.setInt(1,idEmployee);
|
||||||
|
stmt.executeUpdate();
|
||||||
|
System.out.println("削除が完了しました");
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 社員テーブルに指定された情報を新規登録する
|
||||||
|
*
|
||||||
|
* @throws SQLException
|
||||||
|
* @throws ClassNotFoundException
|
||||||
|
*/
|
||||||
|
public void empInsert(String name, String kn, String mailaddress, String password, int idDepartment) throws SQLException, ClassNotFoundException {
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" INSERT INTO ");
|
||||||
|
sql.append(" " + TBL_NAME);
|
||||||
|
sql.append(" ("+NM_EMPLOYEE+", "+KN_EMPLOYEE+","+MAIL_ADDRESS+","+PASSWORD+","+ID_DEPARTMENT+") ");
|
||||||
|
sql.append(" VALUES ");
|
||||||
|
sql.append(" (?, ?, ?, ?, ?) ");
|
||||||
|
try {
|
||||||
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
|
stmt.setString(1,name);
|
||||||
|
stmt.setString(2,kn);
|
||||||
|
stmt.setString(3,mailaddress);
|
||||||
|
stmt.setString(4,password);
|
||||||
|
stmt.setInt(5,idDepartment);
|
||||||
|
stmt.executeUpdate();
|
||||||
|
System.out.println("登録が完了しました");
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 社員テーブルの指定された社員IDに一致するデータを引数の内容に更新する
|
||||||
|
*
|
||||||
|
* @throws SQLException
|
||||||
|
* @throws ClassNotFoundException
|
||||||
|
*/
|
||||||
|
public void empUpdate(int idEmployee, String name, String kn, String mailaddress, String password, int idDepartment) throws SQLException, ClassNotFoundException {
|
||||||
|
if (selectById(idEmployee).size()!=0) {
|
||||||
|
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());
|
||||||
|
stmt.setString(1,name);
|
||||||
|
stmt.setString(2,kn);
|
||||||
|
stmt.setString(3,mailaddress);
|
||||||
|
stmt.setString(4,password);
|
||||||
|
stmt.setInt(5,idDepartment);
|
||||||
|
stmt.setInt(6,idEmployee);
|
||||||
|
stmt.executeUpdate();
|
||||||
|
System.out.println("更新が完了しました");
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
59
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
59
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package com.rhizome.example.dto;
|
||||||
|
|
||||||
|
public class EmployeeDTO {
|
||||||
|
|
||||||
|
private int idEmployee;
|
||||||
|
private String nmEmployee;
|
||||||
|
private String knEmployee;
|
||||||
|
private String mailaddress;
|
||||||
|
private String password;
|
||||||
|
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 getMailaddress() {
|
||||||
|
return mailaddress;
|
||||||
|
}
|
||||||
|
public void setMailaddress(String mailaddress) {
|
||||||
|
this.mailaddress = mailaddress;
|
||||||
|
}
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
public int getIdDepartment() {
|
||||||
|
return idDepartment;
|
||||||
|
}
|
||||||
|
public void setIdDepartment(int idDepartment) {
|
||||||
|
this.idDepartment = idDepartment;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.getIdEmployee() + " "
|
||||||
|
+this.getNmEmployee() + " "
|
||||||
|
+this.getKnEmployee() + " "
|
||||||
|
+this.getMailaddress() + " "
|
||||||
|
+this.getPassword() + " "
|
||||||
|
+this.getIdDepartment();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -48,15 +49,33 @@ public class Main {
|
|||||||
System.out.println("現在のメンテナンス対象テーブル:" + targetTableName(tblFlg));
|
System.out.println("現在のメンテナンス対象テーブル:" + targetTableName(tblFlg));
|
||||||
System.out.println("1:検索 2:登録 3:更新 4:削除 9:テーブル切替 0:終了");
|
System.out.println("1:検索 2:登録 3:更新 4:削除 9:テーブル切替 0:終了");
|
||||||
String ope = sc.nextLine();
|
String ope = sc.nextLine();
|
||||||
|
Connection con = DbUtil.getConnection();
|
||||||
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
switch (ope) {
|
switch (ope) {
|
||||||
case "1":
|
case "1":
|
||||||
empSelect();
|
empSelect();
|
||||||
break;
|
break;
|
||||||
case "2":
|
case "2":
|
||||||
|
empDao.empInsert(inputCheck("社員名", false),
|
||||||
|
inputCheck("フリガナ", false),
|
||||||
|
inputCheck("メールアドレス", false),
|
||||||
|
inputCheck("パスワード", false),
|
||||||
|
Integer.parseInt(inputCheck("部署ID", true)));
|
||||||
break;
|
break;
|
||||||
case "3":
|
case "3":
|
||||||
break;
|
int id = Integer.parseInt(inputCheck("ID", true));
|
||||||
|
if (empDao.selectById(id).size()==0) break; // 社員IDの照合に失敗した場合は処理を抜ける
|
||||||
|
else {
|
||||||
|
empDao.empUpdate(id,
|
||||||
|
inputCheck("社員名", false),
|
||||||
|
inputCheck("フリガナ", false),
|
||||||
|
inputCheck("メールアドレス", false),
|
||||||
|
inputCheck("パスワード", false),
|
||||||
|
Integer.parseInt(inputCheck("部署ID", true)));
|
||||||
|
break;
|
||||||
|
}
|
||||||
case "4":
|
case "4":
|
||||||
|
empDao.empDelete(Integer.parseInt(inputCheck("ID", true)));
|
||||||
break;
|
break;
|
||||||
case "9":
|
case "9":
|
||||||
return;
|
return;
|
||||||
@@ -76,7 +95,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,10 +105,13 @@ public class Main {
|
|||||||
case "1":
|
case "1":
|
||||||
empList = empDao.selectAll();
|
empList = empDao.selectAll();
|
||||||
break;
|
break;
|
||||||
|
case "2":
|
||||||
|
empList = empDao.selectById(Integer.parseInt(inputCheck("ID", true)));
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (String emp : empList) {
|
for (EmployeeDTO emp : empList) {
|
||||||
System.out.println(emp);
|
System.out.println(emp);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -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,12 @@ public class DbUtil {
|
|||||||
* 生成されたConnectionを返却する
|
* 生成されたConnectionを返却する
|
||||||
* ※Connection生成には上記の定数を活用してください
|
* ※Connection生成には上記の定数を活用してください
|
||||||
*/
|
*/
|
||||||
|
public static Connection getConnection() throws ClassNotFoundException, SQLException {
|
||||||
|
Class.forName(DB_DRIVER);
|
||||||
|
// Connection : not null && 接続状態 → フィールドを戻す
|
||||||
|
if( !((con!=null)&&(con.isValid(1))) ) con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||||
|
return con;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO 以下の仕様でメソッドを作成してください。
|
* TODO 以下の仕様でメソッドを作成してください。
|
||||||
@@ -52,6 +60,9 @@ public class DbUtil {
|
|||||||
* 例外 :SQLException
|
* 例外 :SQLException
|
||||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||||
*/
|
*/
|
||||||
|
public static void closeStatement(PreparedStatement stmt) throws SQLException {
|
||||||
|
if (stmt!=null) stmt.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -63,6 +74,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