Compare commits
1 Commits
s-sakiyama
...
m-endo2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1c04363bb7 |
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.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
|
||||
@@ -61,9 +61,14 @@ public class EmployeeDAO {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
EmployeeDTO dto = new EmployeeDTO(rs.getInt(ID_EMPLOYEE), rs.getString(NM_EMPLOYEE),
|
||||
rs.getString(KN_EMPLOYEE), rs.getString(MAIL_ADDRESS), rs.getString(PASSWORD),
|
||||
rs.getInt(ID_DEPARTMENT));
|
||||
//インスタンス生成
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
dto.setId_employee(rs.getInt("id_employee"));
|
||||
dto.setNm_employee(rs.getString("nm_employee"));
|
||||
dto.setKn_employee(rs.getString("kn_employee"));
|
||||
dto.setMail_address(rs.getString("mail_address"));
|
||||
dto.setPassword(rs.getString("password"));
|
||||
dto.setId_department(rs.getInt("id_department"));
|
||||
rtnList.add(dto);
|
||||
}
|
||||
} finally {
|
||||
@@ -72,159 +77,4 @@ public class EmployeeDAO {
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルから社員IDに合致する情報を取得する
|
||||
* @param id
|
||||
* 検索する社員ID
|
||||
* @return 検索結果
|
||||
* @throws SQLException
|
||||
* @throws ClassNotFoundException
|
||||
* @throws NumberFormatException
|
||||
*/
|
||||
public List<EmployeeDTO> selectById(int id)
|
||||
throws SQLException, ClassNotFoundException, NumberFormatException {
|
||||
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);
|
||||
sql.append(" = ");
|
||||
sql.append(" " + id);
|
||||
try {
|
||||
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
|
||||
EmployeeDTO dto = new EmployeeDTO(rs.getInt(ID_EMPLOYEE), rs.getString(NM_EMPLOYEE),
|
||||
rs.getString(KN_EMPLOYEE), rs.getString(MAIL_ADDRESS), rs.getString(PASSWORD),
|
||||
rs.getInt(ID_DEPARTMENT));
|
||||
rtnList.add(dto);
|
||||
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
if (rtnList.size() == 0) {
|
||||
System.out.println("入力された値で情報がみつかりませんでした");
|
||||
}
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルの社員IDに合致する情報を削除する
|
||||
*
|
||||
* @param id
|
||||
* 削除ID
|
||||
* @throws ClassNotFoundException
|
||||
* @throws SQLException
|
||||
* @throws NumberFormatException
|
||||
*/
|
||||
public void empDelete(int id)
|
||||
throws ClassNotFoundException, SQLException, NumberFormatException {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" DELETE ");
|
||||
sql.append(" FROM ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
sql.append(" WHERE ");
|
||||
sql.append(" " + ID_EMPLOYEE);
|
||||
sql.append(" = ");
|
||||
sql.append(" " + id);
|
||||
|
||||
try {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
stmt.executeUpdate();
|
||||
System.out.println("削除が完了しました");
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルに指定された情報を新規登録する
|
||||
*
|
||||
* @param name - 社員名
|
||||
* @param kana - 社員名カナ
|
||||
* @param mail - メールアドレス
|
||||
* @param pass - パスワード
|
||||
* @param depId - 部署ID
|
||||
* @throws NumberFormatException
|
||||
* @throws SQLException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
public void empInsert(String name, String kana, String mail, String pass, int depId)
|
||||
throws NumberFormatException, SQLException, ClassNotFoundException {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" INSERT ");
|
||||
sql.append(" INTO ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
sql.append(" ( " + NM_EMPLOYEE);
|
||||
sql.append(" , " + KN_EMPLOYEE);
|
||||
sql.append(" , " + MAIL_ADDRESS);
|
||||
sql.append(" , " + PASSWORD);
|
||||
sql.append(" , " + ID_DEPARTMENT + " ) ");
|
||||
sql.append(" VALUES ");
|
||||
sql.append(" ( '" + name);
|
||||
sql.append("' , '" + kana);
|
||||
sql.append("' , '" + mail);
|
||||
sql.append("' , '" + pass);
|
||||
sql.append("' , " + depId + " ) ");
|
||||
|
||||
try {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
stmt.executeUpdate();
|
||||
System.out.println("登録が完了しました");
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルの社員IDに合致する情報を更新する
|
||||
*
|
||||
* @param id - 社員ID
|
||||
* @param name - 社員名
|
||||
* @param kana - 社員名カナ
|
||||
* @param mail - メールアドレス
|
||||
* @param pass - パスワード
|
||||
* @param depId - 部署ID
|
||||
* @throws ClassNotFoundException
|
||||
* @throws SQLException
|
||||
* @throws NumberFormatException
|
||||
|
||||
*/
|
||||
public void empUpdate(int id, String name, String kana, String mail, String pass, int depId)
|
||||
throws ClassNotFoundException, SQLException, NumberFormatException {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" UPDATE ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
sql.append(" SET ");
|
||||
sql.append(" " + NM_EMPLOYEE);
|
||||
sql.append(" = '" + name + " ' ");
|
||||
sql.append(" , " + KN_EMPLOYEE);
|
||||
sql.append(" = '" + kana + " ' ");
|
||||
sql.append(" , " + MAIL_ADDRESS);
|
||||
sql.append(" = '" + mail + " ' ");
|
||||
sql.append(" , " + PASSWORD);
|
||||
sql.append(" = '" + pass + " ' ");
|
||||
sql.append(" , " + ID_DEPARTMENT);
|
||||
sql.append(" = " + depId);
|
||||
sql.append(" WHERE ");
|
||||
sql.append(" " + ID_EMPLOYEE);
|
||||
sql.append(" = ");
|
||||
sql.append(" " + id);
|
||||
|
||||
try {
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
stmt.executeUpdate();
|
||||
System.out.println("更新が完了しました");
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,79 +1,61 @@
|
||||
package com.rhizome.example.dto;
|
||||
|
||||
public class EmployeeDTO {
|
||||
private int ID_EMPLOYEE;
|
||||
private String NM_EMPLOYEE;
|
||||
private String KN_EMPLOYEE;
|
||||
private String MAIL_ADDRESS;
|
||||
private String PASSWORD;
|
||||
private int ID_DEPARTMENT;
|
||||
|
||||
public EmployeeDTO() {
|
||||
//フィールド宣言
|
||||
private Integer id_employee;//社員ID
|
||||
private String nm_employee;//社員名
|
||||
private String kn_employee;//社員名カナ
|
||||
private String mail_address;//メールアドレス
|
||||
private String password;//パスワード
|
||||
private Integer id_department;//部署ID
|
||||
|
||||
//getter/setter
|
||||
public Integer getId_employee() {
|
||||
return id_employee;
|
||||
}
|
||||
|
||||
public EmployeeDTO(int ID_EMPLOYEE, String NM_EMPLOYEE, String KN_EMPLOYEE, String MAIL_ADDRESS, String PASSWORD,
|
||||
int ID_DEPARTMENT) {
|
||||
setID_EMPLOYEE(ID_EMPLOYEE);
|
||||
setNM_EMPLOYEE(NM_EMPLOYEE);
|
||||
setKN_EMPLOYEE(KN_EMPLOYEE);
|
||||
setMAIL_ADDRESS(MAIL_ADDRESS);
|
||||
setPASSWORD(PASSWORD);
|
||||
setID_DEPARTMENT(ID_DEPARTMENT);
|
||||
public void setId_employee(Integer id_employee) {
|
||||
this.id_employee = id_employee;
|
||||
}
|
||||
|
||||
public int getID_EMPLOYEE() {
|
||||
return ID_EMPLOYEE;
|
||||
public String getNm_employee() {
|
||||
return nm_employee;
|
||||
}
|
||||
|
||||
public void setID_EMPLOYEE(int iD_EMPLOYEE) {
|
||||
ID_EMPLOYEE = iD_EMPLOYEE;
|
||||
public void setNm_employee(String nm_employee) {
|
||||
this.nm_employee = nm_employee;
|
||||
}
|
||||
|
||||
public String getNM_EMPLOYEE() {
|
||||
return NM_EMPLOYEE;
|
||||
public String getKn_employee() {
|
||||
return kn_employee;
|
||||
}
|
||||
|
||||
public void setNM_EMPLOYEE(String nM_EMPLOYEE) {
|
||||
NM_EMPLOYEE = nM_EMPLOYEE;
|
||||
public void setKn_employee(String kn_employee) {
|
||||
this.kn_employee = kn_employee;
|
||||
}
|
||||
|
||||
public String getKN_EMPLOYEE() {
|
||||
return KN_EMPLOYEE;
|
||||
public String getMail_address() {
|
||||
return mail_address;
|
||||
}
|
||||
|
||||
public void setKN_EMPLOYEE(String kN_EMPLOYEE) {
|
||||
KN_EMPLOYEE = kN_EMPLOYEE;
|
||||
public void setMail_address(String mail_address) {
|
||||
this.mail_address = mail_address;
|
||||
}
|
||||
|
||||
public String getMAIL_ADDRESS() {
|
||||
return MAIL_ADDRESS;
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setMAIL_ADDRESS(String mAIL_ADDRESS) {
|
||||
MAIL_ADDRESS = mAIL_ADDRESS;
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getPASSWORD() {
|
||||
return PASSWORD;
|
||||
public Integer getId_department() {
|
||||
return id_department;
|
||||
}
|
||||
|
||||
public void setPASSWORD(String pASSWORD) {
|
||||
PASSWORD = pASSWORD;
|
||||
}
|
||||
|
||||
public int getID_DEPARTMENT() {
|
||||
return ID_DEPARTMENT;
|
||||
}
|
||||
|
||||
public void setID_DEPARTMENT(int iD_DEPARTMENT) {
|
||||
ID_DEPARTMENT = iD_DEPARTMENT;
|
||||
}
|
||||
|
||||
public void showInfo() {
|
||||
System.out.println(ID_EMPLOYEE + "\t" + NM_EMPLOYEE +
|
||||
"\t" + KN_EMPLOYEE + "\t" + MAIL_ADDRESS +
|
||||
"\t" + PASSWORD + "\t" + ID_DEPARTMENT);
|
||||
public void setId_department(Integer id_department) {
|
||||
this.id_department = id_department;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -54,13 +54,10 @@ public class Main {
|
||||
empSelect();
|
||||
break;
|
||||
case "2":
|
||||
empInsert();
|
||||
break;
|
||||
case "3":
|
||||
empUpdate();
|
||||
break;
|
||||
case "4":
|
||||
empDelete();
|
||||
break;
|
||||
case "9":
|
||||
return;
|
||||
@@ -73,93 +70,6 @@ public class Main {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルの社員IDに合致する情報を更新する
|
||||
*
|
||||
* @throws ClassNotFoundException
|
||||
* @throws SQLException
|
||||
* @throws NumberFormatException
|
||||
*/
|
||||
public static void empUpdate()
|
||||
throws ClassNotFoundException, SQLException, NumberFormatException {
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
int id = Integer.parseInt(inputCheck("ID", true));
|
||||
|
||||
if (empDao.selectById(id).size() == 0) {
|
||||
return;
|
||||
}
|
||||
String name = inputCheck("社員名", false);
|
||||
String kana = inputCheck("フリガナ", false);
|
||||
String mail = inputCheck("メールアドレス", false);
|
||||
String pass = inputCheck("パスワード", false);
|
||||
int depId = Integer.parseInt(inputCheck("部署ID", true));
|
||||
|
||||
try {
|
||||
empDao.empUpdate(id, name, kana, mail, pass, depId);
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルに指定された情報を新規登録する
|
||||
* @throws NumberFormatException
|
||||
* @throws SQLException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
public static void empInsert()
|
||||
throws NumberFormatException, SQLException, ClassNotFoundException {
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
|
||||
String name = inputCheck("社員名", false);
|
||||
String kana = inputCheck("フリガナ", false);
|
||||
String mail = inputCheck("メールアドレス", false);
|
||||
String pass = inputCheck("パスワード", false);
|
||||
int depId = Integer.parseInt(inputCheck("部署ID", true));
|
||||
|
||||
try {
|
||||
empDao.empInsert(name, kana, mail, pass, depId);
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルの社員IDに合致する情報を削除する
|
||||
*
|
||||
* @throws ClassNotFoundException
|
||||
* @throws SQLException
|
||||
*/
|
||||
public static void empDelete()
|
||||
throws ClassNotFoundException, SQLException, NumberFormatException {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
int id = Integer.parseInt(inputCheck("ID", true));
|
||||
rtnList = empDao.selectById(id);
|
||||
if (rtnList.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("指定されたIDの情報を削除します? Y/y\r\n"
|
||||
+ "(Y/y以外の入力であればキャンセル)");
|
||||
|
||||
String str = inputCheck(false);
|
||||
|
||||
if (!str.equalsIgnoreCase("y")) {
|
||||
System.out.println("キャンセルしました");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
empDao.empDelete(id);
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルから情報を検索する
|
||||
*
|
||||
@@ -177,14 +87,11 @@ public class Main {
|
||||
case "1":
|
||||
empList = empDao.selectAll();
|
||||
break;
|
||||
case "2":
|
||||
empList = empDao.selectById(Integer.parseInt(inputCheck("ID", true)));
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
for (EmployeeDTO emp : empList) {
|
||||
emp.showInfo();
|
||||
System.out.println(emp);
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
@@ -240,26 +147,4 @@ public class Main {
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
public static String inputCheck(boolean flg) {
|
||||
String str = null;
|
||||
while (true) {
|
||||
str = sc.nextLine();
|
||||
if (str.trim().length() == 0) {
|
||||
|
||||
continue;
|
||||
}
|
||||
if (flg) {
|
||||
if (str.trim().length() > 0) {
|
||||
try {
|
||||
Integer.parseInt(str);
|
||||
} catch (NumberFormatException e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Objects;
|
||||
|
||||
public class DbUtil {
|
||||
// 定数定義(共通)
|
||||
@@ -45,14 +44,11 @@ public class DbUtil {
|
||||
* 生成されたConnectionを返却する
|
||||
* ※Connection生成には上記の定数を活用してください
|
||||
*/
|
||||
|
||||
public static Connection getConnection() throws ClassNotFoundException, SQLException {
|
||||
if (con != null && con.isValid(1)) {
|
||||
public static Connection getConnection() throws SQLException {
|
||||
//データベースへの接続
|
||||
con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||
return con;
|
||||
}
|
||||
Class.forName(DB_DRIVER);
|
||||
return con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
@@ -63,10 +59,11 @@ public class DbUtil {
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||
*/
|
||||
|
||||
public static void closeStatement(PreparedStatement ps) throws SQLException {
|
||||
if (Objects.nonNull(ps))
|
||||
ps.close();
|
||||
public static void closeStatement(PreparedStatement stmt) throws SQLException {
|
||||
//nullチェック
|
||||
if (stmt != null) {
|
||||
stmt.close();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -79,8 +76,10 @@ public class DbUtil {
|
||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||
*/
|
||||
public static void closeConnection(Connection con) throws SQLException {
|
||||
if (Objects.nonNull(con))
|
||||
//nullチェック
|
||||
if (con != null) {
|
||||
con.close();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user