From 9f7fa737ef391f1e2f500d3acbc7c7d5b045a42e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E5=B7=9D=E3=80=80=E8=A3=95=E6=A8=B9?= <26-y-ishikawa-stellatecnica@it-college.co.jp> Date: Wed, 28 Oct 2026 16:58:01 +0900 Subject: [PATCH] =?UTF-8?q?20261028=20JavaSQL=E6=BC=94=E7=BF=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .classpath | 8 ++++ .gitignore | 1 + .project | 23 +++++++++ .settings/.jsdtscope | 7 +++ ...rg.eclipse.wst.jsdt.ui.superType.container | 1 + .../org.eclipse.wst.jsdt.ui.superType.name | 1 + src/com/rhizome/example/dao/EmployeeDAO.java | 45 ++++++++++++++++-- src/com/rhizome/example/dto/EmployeeDTO.java | 47 +++++++++++++++++++ src/com/rhizome/example/main/Main.java | 9 ++-- src/com/rhizome/example/util/DbUtil.java | 25 ++++++++-- 10 files changed, 158 insertions(+), 9 deletions(-) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 .settings/.jsdtscope create mode 100644 .settings/org.eclipse.wst.jsdt.ui.superType.container create mode 100644 .settings/org.eclipse.wst.jsdt.ui.superType.name create mode 100644 src/com/rhizome/example/dto/EmployeeDTO.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..8ddd4c7 --- /dev/null +++ b/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..2e9776e --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + em-jdbc + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + + org.eclipse.wst.jsdt.core.jsNature + org.eclipse.jdt.core.javanature + + diff --git a/.settings/.jsdtscope b/.settings/.jsdtscope new file mode 100644 index 0000000..cca691f --- /dev/null +++ b/.settings/.jsdtscope @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.container b/.settings/org.eclipse.wst.jsdt.ui.superType.container new file mode 100644 index 0000000..49c8cd4 --- /dev/null +++ b/.settings/org.eclipse.wst.jsdt.ui.superType.container @@ -0,0 +1 @@ +org.eclipse.wst.jsdt.launching.JRE_CONTAINER \ No newline at end of file diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.name b/.settings/org.eclipse.wst.jsdt.ui.superType.name new file mode 100644 index 0000000..11006e2 --- /dev/null +++ b/.settings/org.eclipse.wst.jsdt.ui.superType.name @@ -0,0 +1 @@ +Global \ No newline at end of file diff --git a/src/com/rhizome/example/dao/EmployeeDAO.java b/src/com/rhizome/example/dao/EmployeeDAO.java index ef63c49..8c92b9c 100644 --- a/src/com/rhizome/example/dao/EmployeeDAO.java +++ b/src/com/rhizome/example/dao/EmployeeDAO.java @@ -13,6 +13,7 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import com.rhizome.example.dto.EmployeeDTO; import com.rhizome.example.util.DbUtil; public class EmployeeDAO { @@ -48,8 +49,8 @@ public class EmployeeDAO { * @throws SQLException * @throws ClassNotFoundException */ - public List selectAll() throws SQLException, ClassNotFoundException { - List rtnList = new ArrayList<>(); + public List selectAll() throws SQLException, ClassNotFoundException { + List rtnList = new ArrayList<>(); StringBuilder sql = new StringBuilder(); sql.append(" SELECT "); sql.append(" " + "*"); @@ -60,7 +61,15 @@ public class EmployeeDAO { this.stmt = con.prepareStatement(sql.toString()); rs = stmt.executeQuery(); while (rs.next()) { - rtnList.add(rs.getString(NM_EMPLOYEE)); + EmployeeDTO emp = new EmployeeDTO(); + emp.setId(rs.getInt("id_employee")); + emp.setName(rs.getString("nm_employee")); + emp.setKana(rs.getString("kn_employee")); + emp.setEmail(rs.getString("mail_address")); + emp.setPass(rs.getString("password")); + emp.setIdDep(rs.getInt("id_department")); + + rtnList.add(emp); } } finally { DbUtil.closeStatement(this.stmt); @@ -68,4 +77,34 @@ public class EmployeeDAO { return rtnList; } + public List selectById(int id_employee)throws SQLException, ClassNotFoundException{ + List 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_DEPARTMENT = ?"); + + try { + this.stmt = con.prepareStatement(sql.toString()); + this.stmt.setInt(1,id_employee ); + rs = stmt.executeQuery(); + while (rs.next()) { + EmployeeDTO emp = new EmployeeDTO(); + emp.setId(rs.getInt("id_employee")); + emp.setName(rs.getString("nm_employee")); + emp.setKana(rs.getString("kn_employee")); + emp.setEmail(rs.getString("mail_address")); + emp.setPass(rs.getString("password")); + emp.setIdDep(rs.getInt("id_department")); + + rtnList.add(emp); + } + }finally { + DbUtil.closeStatement(this.stmt); + } + return rtnList; + } } diff --git a/src/com/rhizome/example/dto/EmployeeDTO.java b/src/com/rhizome/example/dto/EmployeeDTO.java new file mode 100644 index 0000000..51f4a6b --- /dev/null +++ b/src/com/rhizome/example/dto/EmployeeDTO.java @@ -0,0 +1,47 @@ +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; + } +} diff --git a/src/com/rhizome/example/main/Main.java b/src/com/rhizome/example/main/Main.java index 14fb220..e6464da 100644 --- a/src/com/rhizome/example/main/Main.java +++ b/src/com/rhizome/example/main/Main.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Scanner; import com.rhizome.example.dao.EmployeeDAO; +import com.rhizome.example.dto.EmployeeDTO; import com.rhizome.example.util.DbUtil; /** @@ -53,6 +54,7 @@ public class Main { empSelect(); break; case "2": + inputCheck("id_employee"); break; case "3": break; @@ -76,7 +78,7 @@ public class Main { * @throws SQLException */ public static void empSelect() throws ClassNotFoundException, SQLException { - List empList = new ArrayList<>(); + List empList = new ArrayList<>(); Connection con = DbUtil.getConnection(); EmployeeDAO empDao = new EmployeeDAO(con); System.out.println("1:全検索 2:条件検索 それ以外:戻る"); @@ -89,8 +91,9 @@ public class Main { default: return; } - for (String emp : empList) { - System.out.println(emp); + for (EmployeeDTO emp : empList) { + System.out.println(emp.getId() + "\t" + emp.getName() + "\t" + + emp.getKana() + "\t" + emp.getEmail() + "\t" + emp.getPass() + "\t" + emp.getIdDep()); } } finally { DbUtil.closeConnection(empDao.con); diff --git a/src/com/rhizome/example/util/DbUtil.java b/src/com/rhizome/example/util/DbUtil.java index d2bb82e..ee5a43c 100644 --- a/src/com/rhizome/example/util/DbUtil.java +++ b/src/com/rhizome/example/util/DbUtil.java @@ -1,6 +1,9 @@ package com.rhizome.example.util; import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; public class DbUtil { // 定数定義(共通) @@ -41,7 +44,15 @@ public class DbUtil { * 生成されたConnectionを返却する * ※Connection生成には上記の定数を活用してください */ - + public static Connection getConnection() throws ClassNotFoundException, SQLException{ + Class.forName("com.mysql.cj.jdbc.Driver"); + con = DriverManager.getConnection( + "jdbc:mysql://127.0.0.1:3306/rezodb", + "rezouser", + "Rezo_0000" + ); + return con; + } /* * TODO 以下の仕様でメソッドを作成してください。 @@ -52,7 +63,11 @@ public class DbUtil { * 例外 :SQLException * 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする */ - + public static void closeStatement(PreparedStatement stmt) throws SQLException{ + if(stmt != null) { + stmt.close(); + } + } /* * TODO 以下の仕様でメソッドを作成してください。 @@ -63,6 +78,10 @@ public class DbUtil { * 例外 :SQLException * 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する */ - + public static void closeConnection(Connection con) throws SQLException{ + if(con != null) { + con.close(); + } + } }