231227 JAVA EmployeesDAO

2023. 12. 27. 19:352023.11.21-2024.05.31

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.sukgi.dao;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
import com.sukgi.dto.EmployeesDTO;
 
import conn.sukgi.db.DBConnection;
 
public class EmployeesDAO {
    DBConnection dbCon = new DBConnection();//인스턴스 생성
 
    public List<EmployeesDTO> selectEmployees() {//메소드
        List<EmployeesDTO> result = new ArrayList<EmployeesDTO>();
        Connection conn = dbCon.getConn();//데이터베이스 연결 설정
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "SELECT * FROM employees LIMIT 0,10";
        //조회할 데이터베이스 쿼리를 정의합니다. 
        //employees 테이블에서 처음 10개의 레코드를 가져오는 쿼리를 사용
 
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
 
            while (rs.next()) {
                EmployeesDTO em = new EmployeesDTO();
                em.setEmp_no(rs.getInt(1));
                em.setBirth_date(rs.getString(2));
                em.setFirst_name(rs.getString(3));
                em.setLast_name(rs.getString(4));
                em.setGender(rs.getString(5));
                em.setHire_date(rs.getString(6));
                result.add(em);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
 
    }
 
}
 
cs

'2023.11.21-2024.05.31' 카테고리의 다른 글

231227 JAVA DepartmentsDTO  (0) 2023.12.27
231227 JAVA Test-Employees  (0) 2023.12.27
231227 JAVA EmployeesDTO  (1) 2023.12.27
231227 JAVA DBConnection  (0) 2023.12.27
231226 JAVA EmployeesDTO  (0) 2023.12.27