This commit is contained in:
2026-10-29 15:47:58 +09:00
parent 5210508a78
commit 3cc08bdf7a
10 changed files with 147 additions and 7 deletions

8
.classpath Normal file
View 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
View File

@@ -0,0 +1 @@
/bin/

23
.project Normal file
View 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
View 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>

View File

@@ -0,0 +1 @@
org.eclipse.wst.jsdt.launching.JRE_CONTAINER

View File

@@ -0,0 +1 @@
Global

View File

@@ -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 {
@@ -26,7 +27,7 @@ public class EmployeeDAO {
/** DBコネクション */
public Connection con;
/** DBステートメント */
/** DBステートPreparedStatement stmtメント */
PreparedStatement stmt;
/** 検索結果 */
ResultSet rs;
@@ -48,8 +49,8 @@ public class EmployeeDAO {
* @throws SQLException
* @throws ClassNotFoundException
*/
public List<String> selectAll() throws SQLException, ClassNotFoundException {
List<String> rtnList = new ArrayList<>();
public List<EmployeeDTO> selectAll() throws SQLException, ClassNotFoundException {
List<EmployeeDTO> rtnList = new ArrayList<>();
StringBuilder sql = new StringBuilder();
sql.append(" SELECT ");
sql.append(" " + "*");
@@ -60,7 +61,20 @@ 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);

View File

@@ -0,0 +1,50 @@
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 String getName() {
return name;
}
public String getKana() {
return kana;
}
public String getEmail() {
return email;
}
public String getPass() {
return pass;
}
public Integer getIdDep() {
return idDep;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setKana(String kana) {
this.kana = kana;
}
public void setEmail(String email) {
this.email = email;
}
public void setPass(String pass) {
this.pass = pass;
}
public void setIdDep(Integer idDep) {
this.idDep = idDep;
}
}

View File

@@ -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;
/**
@@ -76,7 +77,7 @@ public class Main {
* @throws SQLException
*/
public static void empSelect() throws ClassNotFoundException, SQLException {
List<String> empList = new ArrayList<>();
List<EmployeeDTO> empList = new ArrayList<>();
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
@@ -89,7 +90,7 @@ public class Main {
default:
return;
}
for (String emp : empList) {
for (EmployeeDTO emp : empList) {
System.out.println(emp);
}
} finally {

View File

@@ -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,6 +44,23 @@ public class DbUtil {
* 生成されたConnectionを返却する
* ※Connection生成には上記の定数を活用してください
*/
public static Connection getConnection() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/rezodb" // データベース
, "rezouser" // ユーザ
, "Rezo_0000" // パスワード
);
}catch (ClassNotFoundException e){
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
/*
@@ -53,6 +73,14 @@ public class DbUtil {
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
*/
public static void closeStatement(PreparedStatement stmt){
try {
if (stmt != null) stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
* TODO 以下の仕様でメソッドを作成してください。
@@ -63,6 +91,12 @@ public class DbUtil {
* 例外 :SQLException
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
*/
public static void closeConnection(Connection con) {
try {
if (con != null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}