10/28
This commit is contained in:
8
.classpath
Normal file
8
.classpath
Normal 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
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.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
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
|
||||||
@@ -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 dto = new EmployeeDTO();
|
||||||
|
|
||||||
|
dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
||||||
|
dto.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
||||||
|
dto.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
||||||
|
dto.setMailAddress(rs.getString(MAIL_ADDRESS));
|
||||||
|
dto.setPassword(rs.getString(PASSWORD));
|
||||||
|
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
||||||
|
rtnList.add(dto);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closeStatement(this.stmt);
|
||||||
@@ -68,4 +77,111 @@ public class EmployeeDAO {
|
|||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//社員ID検索
|
||||||
|
public List<EmployeeDTO> selectById(int id) throws SQLException {
|
||||||
|
//戻り値用リスト
|
||||||
|
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||||
|
//SQL作成
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" SELECT * ");
|
||||||
|
sql.append(" FROM employee ");
|
||||||
|
sql.append(" WHERE id_employee = ? ");
|
||||||
|
|
||||||
|
try {
|
||||||
|
//PreparedStatement生成
|
||||||
|
stmt = con.prepareStatement(sql.toString());
|
||||||
|
//社員IDを設定
|
||||||
|
stmt.setInt(1, id);
|
||||||
|
//SQL実行
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
//検索結果をDTOへ格納
|
||||||
|
while (rs.next()) {
|
||||||
|
//DTO生成
|
||||||
|
EmployeeDTO dto = new EmployeeDTO();
|
||||||
|
//検索結果をDTOへセット
|
||||||
|
dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
|
||||||
|
dto.setNmEmployee(rs.getString(NM_EMPLOYEE));
|
||||||
|
dto.setKnEmployee(rs.getString(KN_EMPLOYEE));
|
||||||
|
dto.setMailAddress(rs.getString(MAIL_ADDRESS));
|
||||||
|
dto.setPassword(rs.getString(PASSWORD));
|
||||||
|
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
||||||
|
|
||||||
|
//リストへ追加
|
||||||
|
rtnList.add(dto);
|
||||||
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
|
||||||
|
DbUtil.closeStatement(stmt);
|
||||||
|
}
|
||||||
|
//検索結果を返却
|
||||||
|
return rtnList;
|
||||||
|
}
|
||||||
|
|
||||||
|
//社員情報削除
|
||||||
|
public void empDelete(int id) throws SQLException {
|
||||||
|
//SQL作成
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" DELETE ");
|
||||||
|
sql.append(" FROM ");
|
||||||
|
sql.append(" " + TBL_NAME);
|
||||||
|
sql.append(" WHERE ");
|
||||||
|
sql.append(" " + ID_EMPLOYEE + " = ?");
|
||||||
|
|
||||||
|
try {
|
||||||
|
//PreparedStatement生成
|
||||||
|
stmt = con.prepareStatement(sql.toString());
|
||||||
|
//値設定
|
||||||
|
stmt.setInt(1, id);
|
||||||
|
//SQL実行
|
||||||
|
stmt.executeUpdate();
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
|
||||||
|
DbUtil.closeStatement(stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//社員情報登録
|
||||||
|
public void empInsert(
|
||||||
|
String name,
|
||||||
|
String kana,
|
||||||
|
String mail,
|
||||||
|
String password,
|
||||||
|
int departmentId)
|
||||||
|
throws SQLException {
|
||||||
|
//SQL作成
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
|
||||||
|
sql.append(" INSERT INTO employee ");
|
||||||
|
sql.append(" ( ");
|
||||||
|
sql.append(" nm_employee, ");
|
||||||
|
sql.append(" kn_employee, ");
|
||||||
|
sql.append(" mail_address, ");
|
||||||
|
sql.append(" password, ");
|
||||||
|
sql.append(" id_department ");
|
||||||
|
sql.append(" ) ");
|
||||||
|
sql.append(" VALUES ");
|
||||||
|
sql.append(" (?, ?, ?, ?, ?) ");
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
//PreparedStatement生成
|
||||||
|
stmt = con.prepareStatement(sql.toString());
|
||||||
|
//値設定
|
||||||
|
stmt.setString(1, name);
|
||||||
|
stmt.setString(2, kana);
|
||||||
|
stmt.setString(3, mail);
|
||||||
|
stmt.setString(4, password);
|
||||||
|
stmt.setInt(5, departmentId);
|
||||||
|
|
||||||
|
//SQL実行
|
||||||
|
stmt.executeUpdate();
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
|
||||||
|
DbUtil.closeStatement(stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
50
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
50
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package com.rhizome.example.dto;
|
||||||
|
|
||||||
|
public class EmployeeDTO {
|
||||||
|
|
||||||
|
private Integer idEmployee;
|
||||||
|
private String nmEmployee;
|
||||||
|
private String knEmployee;
|
||||||
|
private String mailAddress;
|
||||||
|
private String password;
|
||||||
|
private Integer idDepartment;
|
||||||
|
|
||||||
|
|
||||||
|
public Integer getIdEmployee() {
|
||||||
|
return idEmployee;
|
||||||
|
}
|
||||||
|
public void setIdEmployee(Integer 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 Integer getIdDepartment() {
|
||||||
|
return idDepartment;
|
||||||
|
}
|
||||||
|
public void setIdDepartment(Integer idDepartment) {
|
||||||
|
this.idDepartment = idDepartment;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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,12 @@ public class Main {
|
|||||||
empSelect();
|
empSelect();
|
||||||
break;
|
break;
|
||||||
case "2":
|
case "2":
|
||||||
|
empInsert();
|
||||||
break;
|
break;
|
||||||
case "3":
|
case "3":
|
||||||
break;
|
break;
|
||||||
case "4":
|
case "4":
|
||||||
|
empDelete();
|
||||||
break;
|
break;
|
||||||
case "9":
|
case "9":
|
||||||
return;
|
return;
|
||||||
@@ -76,7 +79,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 +89,33 @@ public class Main {
|
|||||||
case "1":
|
case "1":
|
||||||
empList = empDao.selectAll();
|
empList = empDao.selectAll();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case "2":
|
||||||
|
|
||||||
|
String id = inputCheck("ID", true);
|
||||||
|
empList = empDao.selectById(
|
||||||
|
Integer.parseInt(id));
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (String emp : empList) {
|
|
||||||
System.out.println(emp);
|
if (empList.size() == 0) {
|
||||||
|
System.out.println("入力された値で情報がみつかりませんでした");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (EmployeeDTO emp : empList) {
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
emp.getIdEmployee() + "\t"
|
||||||
|
+ emp.getNmEmployee() + "\t"
|
||||||
|
+ emp.getKnEmployee() + "\t"
|
||||||
|
+ emp.getMailAddress() + "\t"
|
||||||
|
+ emp.getPassword() + "\t"
|
||||||
|
+ emp.getIdDepartment()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeConnection(empDao.con);
|
DbUtil.closeConnection(empDao.con);
|
||||||
@@ -146,4 +171,79 @@ public class Main {
|
|||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
//社員情報を削除する
|
||||||
|
public static void empDelete() throws ClassNotFoundException, SQLException {
|
||||||
|
//DB接続
|
||||||
|
Connection con = DbUtil.getConnection();
|
||||||
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
|
|
||||||
|
try {
|
||||||
|
//削除対象ID入力
|
||||||
|
String strId = inputCheck("ID", true);
|
||||||
|
int id = Integer.parseInt(strId);
|
||||||
|
|
||||||
|
//入力されたID確認
|
||||||
|
List<EmployeeDTO> empList = empDao.selectById(id);
|
||||||
|
//データが存在しない場合
|
||||||
|
if (empList.size() == 0) {
|
||||||
|
System.out.println("入力された値で情報がみつかりませんでした");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//削除確認メッセージ表示
|
||||||
|
System.out.println("指定されたIDの情報を削除しますか? Y/y");
|
||||||
|
System.out.println("(Y/y以外の入力であればキャンセル)");
|
||||||
|
|
||||||
|
String answer = sc.nextLine();
|
||||||
|
|
||||||
|
//Yまたはyが入力された場合のみ削除
|
||||||
|
if ("Y".equals(answer) || "y".equals(answer)) {
|
||||||
|
//社員情報削除
|
||||||
|
empDao.empDelete(id);
|
||||||
|
|
||||||
|
System.out.println("削除が完了しました");
|
||||||
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
|
||||||
|
DbUtil.closeConnection(empDao.con);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//社員情報登録
|
||||||
|
public static void empInsert() throws ClassNotFoundException, SQLException {
|
||||||
|
|
||||||
|
//DB接続
|
||||||
|
Connection con = DbUtil.getConnection();
|
||||||
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
//社員名入力
|
||||||
|
String name = inputCheck("社員名", false);
|
||||||
|
//フリガナ入力
|
||||||
|
String kana = inputCheck("フリガナ", false);
|
||||||
|
//メールアドレス入力
|
||||||
|
String mail = inputCheck("メールアドレス", false);
|
||||||
|
//パスワード入力
|
||||||
|
String password = inputCheck("パスワード", false);
|
||||||
|
//部署ID入力
|
||||||
|
String departmentId = inputCheck("部署ID", true);
|
||||||
|
//登録処理
|
||||||
|
empDao.empInsert(
|
||||||
|
name,
|
||||||
|
kana,
|
||||||
|
mail,
|
||||||
|
password,
|
||||||
|
Integer.parseInt(departmentId));
|
||||||
|
|
||||||
|
System.out.println("登録が完了しました");
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
|
||||||
|
DbUtil.closeConnection(empDao.con);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
||||||
// 定数定義(共通)
|
// 定数定義(共通)
|
||||||
@@ -42,6 +45,18 @@ public class DbUtil {
|
|||||||
* ※Connection生成には上記の定数を活用してください
|
* ※Connection生成には上記の定数を活用してください
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
public static Connection getConnection()
|
||||||
|
throws ClassNotFoundException, SQLException {
|
||||||
|
|
||||||
|
Class.forName(DB_DRIVER);
|
||||||
|
|
||||||
|
con = DriverManager.getConnection(
|
||||||
|
DB_URL,
|
||||||
|
DB_USER,
|
||||||
|
DB_PASS);
|
||||||
|
|
||||||
|
return con;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO 以下の仕様でメソッドを作成してください。
|
* TODO 以下の仕様でメソッドを作成してください。
|
||||||
@@ -53,6 +68,14 @@ public class DbUtil {
|
|||||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
public static void closeStatement(PreparedStatement stmt)
|
||||||
|
throws SQLException {
|
||||||
|
|
||||||
|
if (stmt != null) {
|
||||||
|
stmt.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO 以下の仕様でメソッドを作成してください。
|
* TODO 以下の仕様でメソッドを作成してください。
|
||||||
@@ -64,5 +87,11 @@ public class DbUtil {
|
|||||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
public static void closeConnection(Connection con)
|
||||||
|
throws SQLException {
|
||||||
|
|
||||||
|
if (con != null) {
|
||||||
|
con.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user