This commit is contained in:
2026-09-29 16:53:19 +09:00
parent 56130f34b2
commit 329c222f56
2 changed files with 33 additions and 9 deletions

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 {
@@ -48,11 +49,16 @@ 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(" " + "*");
sql.append(" " + ID_EMPLOYEE + ",");
sql.append(" " + NM_EMPLOYEE + ",");
sql.append(" " + KN_EMPLOYEE + ",");
sql.append(" " + MAIL_ADDRESS + ",");
sql.append(" " + PASSWORD + ",");
sql.append(" " + ID_DEPARTMENT);
sql.append(" FROM ");
sql.append(" " + TBL_NAME);
try {
@@ -60,12 +66,20 @@ public class EmployeeDAO {
this.stmt = con.prepareStatement(sql.toString());
rs = stmt.executeQuery();
while (rs.next()) {
rtnList.add(rs.getString(NM_EMPLOYEE));
EmployeeDTO dto = new EmployeeDTO();
dto.setIdEmployee(rs.getInt(ID_EMPLOYEE));
dto.setNmEmployee(rs.getString(NM_EMPLOYEE));
dto.setKnEmployee(rs.getString(KN_EMPLOYEE));
dto.setMailAddress(rs.getString(MAIL_ADDRESS));
dto.setPassword(rs.getString(PASSWORD));
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
// dto ကို List ထဲသို့ ထည့်ရပါမည်
rtnList.add(dto);
}
} finally {
DbUtil.closeStatement(this.stmt);
}
return rtnList;
}
}
}

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,8 @@ public class Main {
* @throws SQLException
*/
public static void empSelect() throws ClassNotFoundException, SQLException {
List<String> empList = new ArrayList<>();
//List<String> empList = new ArrayList<>();
List<EmployeeDTO> empList = new ArrayList<>();
Connection con = DbUtil.getConnection();
EmployeeDAO empDao = new EmployeeDAO(con);
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
@@ -84,13 +86,21 @@ public class Main {
try {
switch (select) {
case "1":
//empList = empDao.selectAll();
empList = empDao.selectAll();
break;
default:
return;
}
for (String emp : empList) {
System.out.println(emp);
for (EmployeeDTO emp : empList) {
//for (String emp : empList) {
//System.out.println(emp);
System.out.println(emp.getIdEmployee() + "\t" +
emp.getNmEmployee() + "\t" +
emp.getKnEmployee() + "\t" +
emp.getMailAddress() + "\t" +
emp.getPassword() + "\t" +
emp.getIdDepartment());
}
} finally {
DbUtil.closeConnection(empDao.con);