Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d8d9f1d9b7 | |||
| 65ff97be06 | |||
| bb5df7baad |
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
|
||||||
@@ -60,82 +60,48 @@ public class EmployeeDAO {
|
|||||||
|
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
rs = stmt.executeQuery();
|
rs = stmt.executeQuery();
|
||||||
//DBのカラムをリストに追加
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
EmployeeDTO dto = new EmployeeDTO();
|
EmployeeDTO dto = new EmployeeDTO();
|
||||||
dto.setEmpid(rs.getInt(ID_EMPLOYEE));
|
dto.setId_employee(rs.getInt("id_employee"));
|
||||||
dto.setName(rs.getString(NM_EMPLOYEE));
|
dto.setNm_employee(rs.getString("nm_employee"));
|
||||||
dto.setKana(rs.getString(KN_EMPLOYEE));
|
dto.setKn_employee(rs.getString("kn_employee"));
|
||||||
dto.setMail(rs.getString(MAIL_ADDRESS));
|
dto.setMail_address(rs.getString("mail_address"));
|
||||||
dto.setPass(rs.getString(PASSWORD));
|
dto.setPassword(rs.getString("password"));
|
||||||
dto.setDepid(rs.getInt(ID_DEPARTMENT));
|
dto.setId_department(rs.getInt("id_department"));
|
||||||
rtnList.add(dto);
|
rtnList.add(dto);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closestatement(this.stmt);
|
||||||
}
|
}
|
||||||
return rtnList;
|
return rtnList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<EmployeeDTO> selectByld(int id) throws SQLException {
|
public List<EmployeeDTO> selectById(int id_employee) throws SQLException, ClassNotFoundException {
|
||||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
List<EmployeeDTO> rtnList2 = new ArrayList<>();
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
//社員テーブルから指定された社員IDに一致するデータを取得
|
|
||||||
sql.append(" SELECT ");
|
sql.append(" SELECT ");
|
||||||
sql.append(" " + "*");
|
sql.append(" " + "*");
|
||||||
sql.append(" FROM ");
|
sql.append(" FROM ");
|
||||||
sql.append(" " + TBL_NAME);
|
sql.append(" " + TBL_NAME);
|
||||||
sql.append(" WHERE ");
|
sql.append(" WHERE " + ID_EMPLOYEE+ "=?");
|
||||||
sql.append(" " + ID_EMPLOYEE);
|
|
||||||
sql.append(" = ");
|
|
||||||
sql.append(" " + id);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
this.stmt = con.prepareStatement(sql.toString());
|
this.stmt = con.prepareStatement(sql.toString());
|
||||||
rs = stmt.executeQuery();
|
rs = stmt.executeQuery();
|
||||||
//DBのカラムをリストに追加
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
EmployeeDTO dto = new EmployeeDTO();
|
EmployeeDTO dto = new EmployeeDTO();
|
||||||
dto.setEmpid(rs.getInt(ID_EMPLOYEE));
|
dto.setId_employee(rs.getInt("id_employee"));
|
||||||
dto.setName(rs.getString(NM_EMPLOYEE));
|
dto.setNm_employee(rs.getString("nm_employee"));
|
||||||
dto.setKana(rs.getString(KN_EMPLOYEE));
|
dto.setKn_employee(rs.getString("kn_employee"));
|
||||||
dto.setMail(rs.getString(MAIL_ADDRESS));
|
dto.setMail_address(rs.getString("mail_address"));
|
||||||
dto.setPass(rs.getString(PASSWORD));
|
dto.setPassword(rs.getString("password"));
|
||||||
dto.setDepid(rs.getInt(ID_DEPARTMENT));
|
dto.setId_department(rs.getInt("id_department"));
|
||||||
rtnList.add(dto);
|
rtnList2.add(dto);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeStatement(this.stmt);
|
DbUtil.closestatement(this.stmt);
|
||||||
}
|
}
|
||||||
if (rtnList.size() == 0) {
|
return rtnList2;
|
||||||
System.out.println("入力された値で情報が見つかりませんでした");
|
|
||||||
}
|
|
||||||
return rtnList;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void empDelete(int id) throws SQLException {
|
|
||||||
StringBuilder sql = new StringBuilder();
|
|
||||||
//社員テーブルから指定された社員IDに一致するデータを取得
|
|
||||||
sql.append(" DELETE ");
|
|
||||||
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());
|
|
||||||
this.stmt.executeUpdate();
|
|
||||||
System.out.println("削除が完了しました");
|
|
||||||
} finally {
|
|
||||||
DbUtil.closeStatement(this.stmt);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void emplnsert (String name, String kana, String mail, String pass, int id) {
|
|
||||||
StringBuilder sql = new StringBuilder();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
69
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
69
src/com/rhizome/example/dto/EmployeeDTO.java
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package com.rhizome.example.dto;
|
||||||
|
|
||||||
|
public class EmployeeDTO {
|
||||||
|
|
||||||
|
private Integer id_employee;
|
||||||
|
private String nm_employee;
|
||||||
|
private String kn_employee;
|
||||||
|
private String mail_address;
|
||||||
|
private String password;
|
||||||
|
private Integer id_department;
|
||||||
|
|
||||||
|
public Integer getId_employee() {
|
||||||
|
return id_employee;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId_employee(Integer id_employee) {
|
||||||
|
this.id_employee = id_employee;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNm_employee() {
|
||||||
|
return nm_employee;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNm_employee(String nm_employee) {
|
||||||
|
this.nm_employee = nm_employee;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getKn_employee() {
|
||||||
|
return kn_employee;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKn_employee(String kn_employee) {
|
||||||
|
this.kn_employee = kn_employee;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMail_address() {
|
||||||
|
return mail_address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMail_address(String mail_address) {
|
||||||
|
this.mail_address = mail_address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getId_department() {
|
||||||
|
return id_department;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId_department(Integer id_department) {
|
||||||
|
this.id_department = id_department;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return id_employee + "\t"
|
||||||
|
+ nm_employee + "\t"
|
||||||
|
+ kn_employee + "\t"
|
||||||
|
+ mail_address + "\t"
|
||||||
|
+ password + "\t"
|
||||||
|
+ id_department;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,8 +45,6 @@ public class Main {
|
|||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
*/
|
*/
|
||||||
public void start() throws ClassNotFoundException, SQLException {
|
public void start() throws ClassNotFoundException, SQLException {
|
||||||
Connection con = DbUtil.getConnection();
|
|
||||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
|
||||||
System.out.println();
|
System.out.println();
|
||||||
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:終了");
|
||||||
@@ -60,7 +58,6 @@ public class Main {
|
|||||||
case "3":
|
case "3":
|
||||||
break;
|
break;
|
||||||
case "4":
|
case "4":
|
||||||
empDelete();
|
|
||||||
break;
|
break;
|
||||||
case "9":
|
case "9":
|
||||||
return;
|
return;
|
||||||
@@ -73,30 +70,6 @@ public class Main {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void empDelete() throws SQLException {
|
|
||||||
Connection con = DbUtil.getConnection();
|
|
||||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
|
||||||
try {
|
|
||||||
String strId = inputCheck("ID", true);
|
|
||||||
int id = Integer.parseInt(strId);
|
|
||||||
List<EmployeeDTO> empList = empDao.selectByld(id);
|
|
||||||
if (empList.isEmpty()) {
|
|
||||||
} else {
|
|
||||||
System.out.println("指定されたIDの情報を削除します? Y/y");
|
|
||||||
System.out.println("Y/y以外の入力であればキャンセル");
|
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
String str = sc.nextLine();
|
|
||||||
if (str.equals("Y") || str.equals("y")) {
|
|
||||||
empDao.empDelete(id);
|
|
||||||
} else {
|
|
||||||
System.out.println("キャンセルしました");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
DbUtil.closeConnection(empDao.con);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 社員テーブルから情報を検索する
|
* 社員テーブルから情報を検索する
|
||||||
*
|
*
|
||||||
@@ -116,15 +89,14 @@ public class Main {
|
|||||||
break;
|
break;
|
||||||
case "2":
|
case "2":
|
||||||
System.out.println("IDを入力してください");
|
System.out.println("IDを入力してください");
|
||||||
int id = sc.nextInt();
|
|
||||||
empList = empDao.selectByld(id);
|
empList = empDao.selectById(tblFlg);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (EmployeeDTO emp : empList) {
|
for (EmployeeDTO emp : empList) {
|
||||||
System.out.println(emp.getEmpid() + "\t" + emp.getName() + "\t" + emp.getKana() + "\t"
|
System.out.println(emp);
|
||||||
+ emp.getMail() + "\t" + emp.getPass() + "\t" + emp.getDepid());
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
DbUtil.closeConnection(empDao.con);
|
DbUtil.closeConnection(empDao.con);
|
||||||
@@ -158,7 +130,7 @@ public class Main {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String inputCheck(String titleName, boolean flg) {
|
public static String inputCheck(String titleName, boolean flg) {
|
||||||
String str;
|
String str = null;
|
||||||
while (true) {
|
while (true) {
|
||||||
System.out.println(titleName + "を入力してください");
|
System.out.println(titleName + "を入力してください");
|
||||||
str = sc.nextLine();
|
str = sc.nextLine();
|
||||||
|
|||||||
@@ -45,8 +45,8 @@ public class DbUtil {
|
|||||||
* ※Connection生成には上記の定数を活用してください
|
* ※Connection生成には上記の定数を活用してください
|
||||||
*/
|
*/
|
||||||
public static Connection getConnection() throws SQLException {
|
public static Connection getConnection() throws SQLException {
|
||||||
//データベースへの接続
|
//データベースの接続
|
||||||
con = DriverManager.getConnection(DB_URL, DB_USER ,DB_PASS);
|
con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
|
||||||
return con;
|
return con;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,12 +59,14 @@ public class DbUtil {
|
|||||||
* 例外 :SQLException
|
* 例外 :SQLException
|
||||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||||
*/
|
*/
|
||||||
public static void closeStatement(PreparedStatement ps) throws SQLException {
|
|
||||||
//DBステートメントのクローズ
|
public static void closestatement(PreparedStatement stmt) throws SQLException {
|
||||||
if (ps != null) {
|
if (!stmt.equals(null)) {
|
||||||
ps.close(); //DBステートメントをクローズ
|
//DBステートメントを閉じる
|
||||||
|
stmt.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO 以下の仕様でメソッドを作成してください。
|
* TODO 以下の仕様でメソッドを作成してください。
|
||||||
*
|
*
|
||||||
@@ -75,10 +77,8 @@ public class DbUtil {
|
|||||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||||
*/
|
*/
|
||||||
public static void closeConnection(Connection con) throws SQLException {
|
public static void closeConnection(Connection con) throws SQLException {
|
||||||
//コネクションの切断
|
if (!con.equals(null)) {
|
||||||
if (con != null) {
|
con.close();
|
||||||
con.close(); //コネクション切断
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
BIN
トランザクション・メディア・ネットワークス_小川悠里_em-jdbc.zip
Normal file
BIN
トランザクション・メディア・ネットワークス_小川悠里_em-jdbc.zip
Normal file
Binary file not shown.
Reference in New Issue
Block a user