Compare commits
3 Commits
9932633268
...
kawara
| Author | SHA1 | Date | |
|---|---|---|---|
| 6493baccef | |||
| f909550b87 | |||
| df7972c662 |
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,12 +61,125 @@ 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.setId(rs.getInt("id_employee"));
|
||||||
|
dto.setName(rs.getString("nm_employee"));
|
||||||
|
dto.setKana(rs.getString("kn_employee"));
|
||||||
|
dto.setEmail(rs.getString("mail_address"));
|
||||||
|
dto.setPass(rs.getString("password"));
|
||||||
|
dto.setIdDep(rs.getInt("id_department"));
|
||||||
|
|
||||||
|
rtnList.add(dto);
|
||||||
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closeStatement(this.stmt);
|
||||||
}
|
}
|
||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
public java.util.List<EmployeeDTO> selectById(int id)
|
||||||
|
throws java.sql.SQLException, java.lang.ClassNotFoundException, java.lang.NumberFormatException {
|
||||||
|
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" SELECT * FROM ");
|
||||||
|
sql.append(TBL_NAME);
|
||||||
|
sql.append(" WHERE id_employee = ?");
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
|
stmt.setInt(1, id);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
|
||||||
|
EmployeeDTO dto = new EmployeeDTO();
|
||||||
|
dto.setId(rs.getInt("id_employee"));
|
||||||
|
dto.setName(rs.getString("nm_employee"));
|
||||||
|
dto.setKana(rs.getString("kn_employee"));
|
||||||
|
dto.setEmail(rs.getString("mail_address"));
|
||||||
|
dto.setPass(rs.getString("password"));
|
||||||
|
dto.setIdDep(rs.getInt("id_department"));
|
||||||
|
|
||||||
|
rtnList.add(dto);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rtnList;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void empDelete(int id) throws java.lang.ClassNotFoundException, java.sql.SQLException {
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" DELETE FROM ");
|
||||||
|
sql.append(TBL_NAME);
|
||||||
|
sql.append(" WHERE id_employee = ?");
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.stmt = this.con.prepareStatement(sql.toString());
|
||||||
|
this.stmt.setInt(1, id);
|
||||||
|
this.stmt.executeUpdate();
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void empInsert(java.lang.String name, java.lang.String kana, java.lang.String mail, java.lang.String pass,
|
||||||
|
int depId) throws java.lang.NumberFormatException, java.sql.SQLException, java.lang.ClassNotFoundException {
|
||||||
|
//List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" INSERT INTO " + 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());
|
||||||
|
|
||||||
|
this.stmt.setString(1, name);
|
||||||
|
this.stmt.setString(2, kana);
|
||||||
|
this.stmt.setString(3, mail);
|
||||||
|
this.stmt.setString(4, pass);
|
||||||
|
this.stmt.setInt(5, depId);
|
||||||
|
|
||||||
|
this.stmt.executeUpdate();
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void empUpdate(int id, java.lang.String name, java.lang.String kana, java.lang.String mail,
|
||||||
|
java.lang.String pass, int depId)
|
||||||
|
throws java.lang.ClassNotFoundException, java.sql.SQLException, java.lang.NumberFormatException {
|
||||||
|
StringBuilder sql = new StringBuilder();
|
||||||
|
sql.append(" UPDATE ");
|
||||||
|
sql.append(TBL_NAME);
|
||||||
|
sql.append(" SET ");
|
||||||
|
sql.append(" nm_employee = ?, kn_employee = ?, mail_address = ?, password = ?, id_department = ? ");
|
||||||
|
sql.append(" WHERE id_employee = ? ");
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
|
|
||||||
|
this.stmt.setString(1, name);
|
||||||
|
this.stmt.setString(2, kana);
|
||||||
|
this.stmt.setString(3, mail);
|
||||||
|
this.stmt.setString(4, pass);
|
||||||
|
this.stmt.setInt(5, depId);
|
||||||
|
this.stmt.setInt(6, id);
|
||||||
|
|
||||||
|
this.stmt.executeUpdate();
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeStatement(this.stmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
60
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
60
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package 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;
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getKana() {
|
||||||
|
return kana;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKana(String kana) {
|
||||||
|
this.kana = kana;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPass() {
|
||||||
|
return pass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPass(String pass) {
|
||||||
|
this.pass = pass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getIdDep() {
|
||||||
|
return idDep;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdDep(Integer idDep) {
|
||||||
|
this.idDep = idDep;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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,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,14 +90,120 @@ public class Main {
|
|||||||
case "1":
|
case "1":
|
||||||
empList = empDao.selectAll();
|
empList = empDao.selectAll();
|
||||||
break;
|
break;
|
||||||
|
case "2":
|
||||||
|
String idStr = inputCheck("ID", true);
|
||||||
|
int id = Integer.parseInt(idStr);
|
||||||
|
empList = empDao.selectById(id);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (String emp : empList) {
|
for (EmployeeDTO emp : empList) {
|
||||||
System.out.println(emp);
|
System.out.println(
|
||||||
|
|
||||||
|
emp.getId() + "\t" +
|
||||||
|
emp.getName() + "\t" +
|
||||||
|
emp.getKana() + "\t" +
|
||||||
|
emp.getEmail() + "\t" +
|
||||||
|
emp.getPass() + "\t" +
|
||||||
|
emp.getIdDep()
|
||||||
|
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeConnection(empDao.con);
|
DbUtil.closeConnection(empDao.con);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void empInsert()
|
||||||
|
throws java.lang.NumberFormatException, java.lang.ClassNotFoundException, java.sql.SQLException {
|
||||||
|
//List<EmployeeDTO> empList = new ArrayList<>();
|
||||||
|
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);
|
||||||
|
String depIdstr = inputCheck("部署ID", true);
|
||||||
|
|
||||||
|
int depId = Integer.parseInt(depIdstr);
|
||||||
|
|
||||||
|
System.out.println("登録が完了しました");
|
||||||
|
|
||||||
|
empDao.empInsert(name, kana, mail, pass, depId);
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeConnection(empDao.con);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void empUpdate()
|
||||||
|
throws java.lang.NumberFormatException, java.lang.ClassNotFoundException, java.sql.SQLException {
|
||||||
|
Connection con = DbUtil.getConnection();
|
||||||
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
String idstr = inputCheck("ID", true);
|
||||||
|
int id = Integer.parseInt(idstr);
|
||||||
|
|
||||||
|
List<EmployeeDTO> checkList = empDao.selectById(id);
|
||||||
|
if (checkList.size() == 0) {
|
||||||
|
System.out.println("入力された値で情報がみつかりませんでした");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String name = inputCheck("社員名", false);
|
||||||
|
String kana = inputCheck("フリガナ", false);
|
||||||
|
String mail = inputCheck("メールアドレス", false);
|
||||||
|
String pass = inputCheck("パスワード", false);
|
||||||
|
String depIdstr = inputCheck("部署ID", true);
|
||||||
|
|
||||||
|
int depId = Integer.parseInt(depIdstr);
|
||||||
|
|
||||||
|
empDao.empUpdate(id, name, kana, mail, pass, depId);
|
||||||
|
|
||||||
|
System.out.println("更新が完了しました");
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeConnection(empDao.con);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void empDelete() throws ClassNotFoundException, SQLException {
|
||||||
|
//List<EmployeeDTO> empList = new ArrayList<>();
|
||||||
|
Connection con = DbUtil.getConnection();
|
||||||
|
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
String idStr = inputCheck("ID", true);
|
||||||
|
int id = Integer.parseInt(idStr);
|
||||||
|
|
||||||
|
List<EmployeeDTO> checkList = empDao.selectById(id);
|
||||||
|
if (checkList.size() == 0) {
|
||||||
|
System.out.println("入力された値で情報がみつかりませんでした");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("指定されたIDの情報を削除します? Y/y");
|
||||||
|
System.out.println("(Y/y以外の入力であればキャンセル)");
|
||||||
|
String yY = sc.nextLine();
|
||||||
|
if (!"y".equals(yY) && !"Y".equals(yY)) {
|
||||||
|
System.out.println("削除をキャンセルしました");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
empDao.empDelete(id);
|
||||||
|
System.out.println("削除が完了しました");
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
DbUtil.closeConnection(empDao.con);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.rhizome.example.util;
|
package com.rhizome.example.util;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
|
||||||
public class DbUtil {
|
public class DbUtil {
|
||||||
// 定数定義(共通)
|
// 定数定義(共通)
|
||||||
@@ -42,6 +43,10 @@ public class DbUtil {
|
|||||||
* ※Connection生成には上記の定数を活用してください
|
* ※Connection生成には上記の定数を活用してください
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
public static java.sql.Connection getConnection() throws java.lang.ClassNotFoundException, java.sql.SQLException {
|
||||||
|
con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||||
|
return con;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO 以下の仕様でメソッドを作成してください。
|
* TODO 以下の仕様でメソッドを作成してください。
|
||||||
@@ -53,6 +58,11 @@ public class DbUtil {
|
|||||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
public static void closeStatement(java.sql.PreparedStatement stmt) throws java.sql.SQLException {
|
||||||
|
if (stmt != null) {
|
||||||
|
stmt.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO 以下の仕様でメソッドを作成してください。
|
* TODO 以下の仕様でメソッドを作成してください。
|
||||||
@@ -64,5 +74,15 @@ public class DbUtil {
|
|||||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
//public static void closeConnection(Connection con) throws SQLException {
|
||||||
|
//if (con != null) {
|
||||||
|
//con.close();
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
|
||||||
|
public static void closeConnection(java.sql.Connection con) throws java.sql.SQLException {
|
||||||
|
if (con != null)
|
||||||
|
con.close();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user