๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP

[IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (7) DAO ๋ถ„๋ฆฌํ•˜๊ธฐ

by Leica 2020. 2. 15.
๋ฐ˜์‘ํ˜•
  • ๊ด€๋ จ ๊ธ€

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (1) ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (2) ํšŒ์› ๊ฐ€์ž…

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (3) ํšŒ์› ์ •๋ณด ์ˆ˜์ •

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (4) ๋ทฐ(JSP) ๋ถ„๋ฆฌํ•˜๊ธฐ

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (5) ๋กœ๊ทธ์ธ/๋กœ๊ทธ์•„์›ƒ(HttpSession)

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (6) ํšŒ์› ์‚ญ์ œ

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (7) DAO ๋ถ„๋ฆฌํ•˜๊ธฐ

 

์ง€๊ธˆ๊นŒ์ง€ ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ๋Š” ์„œ๋ธ”๋ฆฟ์—์„œ JSP๋กœ ํ™”๋ฉด ์ƒ์„ฑ์„ ์œ„์ž„ํ•˜์—ฌ ๋ทฐ๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ ๊นŒ์ง€ ๊ตฌํ˜„๋˜์—ˆ๋‹ค.

์ด์ œ DB์™€ ์—ฐ๋™ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„์„ DAO(Data Access Object)๋กœ ๋งŒ๋“ค์–ด ๋ถ„๋ฆฌํ•  ๊ฒƒ์ด๋‹ค.

 

DAO(Data Access Object)

๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ์ „๋ฌธ์œผ๋กœ ํ•˜๋Š” ๊ฐ์ฒด

DB, ํŒŒ์ผ, ๋ฉ”๋ชจ๋ฆฌ ๋“ฑ์„ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑ, ์กฐํšŒ, ๋ณ€๊ฒฝ, ์‚ญ์ œํ•˜๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
DAO๋Š” ๋ณดํ†ต ํ•˜๋‚˜์˜ DB ํ…Œ์ด๋ธ”์ด๋‚˜ ๋ทฐ์— ๋Œ€์‘ํ•œ๋‹ค.
์—…๋ฌด ๋กœ์ง์—์„œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋ถ€๋ถ„์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๋ณ„๋„์˜ ๊ฐ์ฒด๋กœ ์ •์˜ํ•˜๋ฉด ์—ฌ๋Ÿฌ ์—…๋ฌด์—์„œ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์‰ฌ์›Œ์ง€๊ณ  ์žฌ์‚ฌ์šฉ์„ฑ์ด ๋†’์•„์ง„๋‹ค.

 

DAO ์ƒ์„ฑ

dao ํŒจํ‚ค์ง€์™€ Dao ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  DB ์—ฐ๋™ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

 

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
50
51
52
53
54
55
56
public class StudentDao {
 
    Connection connection;
 
    public void setConnection(Connection connection) {
        this.connection = connection;
    }
 
    /**
     * ํ•™์ƒ ๋ชฉ๋ก ์กฐํšŒ
     *
     * @return ํ•™์ƒ ๋ชฉ๋ก
     * @throws SQLException
     */
    public List<Student> selectList() throws SQLException {
        Statement stmt = null;
        ResultSet rs = null;
 
        List<Student> students = null;
        try {
            stmt = connection.createStatement();
            rs = stmt.executeQuery(
                    "SELECT STUDENT_NO, DEPARTMENT, STUDENT_NAME, GRADE, GENDER, AGE, PHONE_NUMBER, ADDRESS " +
                            " FROM STUDENT " +
                            " WHERE STUDENT_NO != 'admin'" +
                            " ORDER BY STUDENT_NO"
            );
 
            students = new ArrayList<>();
 
            while (rs.next()) {
                students.add(new Student()
                        .setStudentNo(rs.getString("STUDENT_NO"))
                        .setDepartment(rs.getString("DEPARTMENT"))
                        .setStudentName(rs.getString("STUDENT_NAME"))
                        .setGrade(rs.getInt("GRADE"))
                        .setGender(rs.getString("GENDER"))
                        .setAge(rs.getInt("AGE"))
                        .setPhoneNumber(rs.getString("PHONE_NUMBER"))
                        .setAddress(rs.getString("ADDRESS"))
                );
            }
 
            return students;
 
        } catch (SQLException e) {
            throw e;
        } finally {
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
            } catch (SQLException e) {
            }
        }
    }
}
cs

 

Connection ๊ฐ์ฒด ์ฃผ์ž…

StudentDao์—์„œ๋Š” ServletContext์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ Connection ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…๋ฐ›๊ธฐ ์œ„ํ•œ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ setter()๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

Connection connection;

public void setConnection(Connection connection) {
    this.connection = connection;
}

 

selectList()๊ฐ€ ํ˜ธ์ถœ๋˜๊ธฐ ์ „์— Connection ๊ฐ์ฒด๊ฐ€ ๋จผ์ € ์„ค์ •๋˜์–ด์•ผ ํ•œ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ์ฃผ์ž…๋ฐ›๋Š” ๊ฒƒ์„ '์˜์กด์„ฑ ์ฃผ์ž…(DI, Dependency Injection)' ๋˜๋Š” '์—ญ์ œ์–ด(IoC, Inversion of Control)'์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

 

ํ•™์ƒ ๋ชฉ๋ก ์„œ๋ธ”๋ฆฟ์—์„œ DAO ์‚ฌ์šฉํ•˜๊ธฐ

๋‹ค์Œ์€ ํ•™์ƒ ๋ชฉ๋ก ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” StudentListServlet.java์˜ ๊ธฐ์กด ๋‚ด์šฉ์ด๋‹ค.

 

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
50
51
52
53
@WebServlet("/student/list")
public class StudentListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
 
        try {
            ServletContext sc = this.getServletContext();
            // ServletContext์— ์ €์žฅ๋œ DB ์ปค๋„ฅ์…˜ ๊ฐ์ฒด ์‚ฌ์šฉ
            conn = (Connection) sc.getAttribute("conn");
            stmt = conn.createStatement();
            rs = stmt.executeQuery(
                    "SELECT STUDENT_NO, DEPARTMENT, STUDENT_NAME, GRADE, GENDER, AGE, PHONE_NUMBER, ADDRESS FROM STUDENT" +
                            " ORDER BY STUDENT_NO");
 
            resp.setContentType("text/html; charset=UTF-8");
 
            List<Student> students = new ArrayList<>();
            while (rs.next()) {
                students.add(new Student()
                        .setStudentNo(rs.getString("STUDENT_NO"))
                        .setDepartment(rs.getString("DEPARTMENT"))
                        .setStudentName(rs.getString("STUDENT_NAME"))
                        .setGrade(rs.getInt("GRADE"))
                        .setGender(rs.getString("GENDER"))
                        .setAge(rs.getInt("AGE"))
                        .setPhoneNumber(rs.getString("PHONE_NUMBER"))
                        .setAddress(rs.getString("ADDRESS"))
                );
            }
 
            req.setAttribute("students", students);
 
            RequestDispatcher rd = req.getRequestDispatcher(
                    "/student/StudentList.jsp"
            );
            rd.include(req, resp);
        } catch (SQLException e) {
            req.setAttribute("error", e);
            RequestDispatcher rd = req.getRequestDispatcher("/Error.jsp");
            rd.forward(req, resp);
        } finally {
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
cs

 

์ด ์„œ๋ธ”๋ฆฟ์˜ doGet()์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•œ๋‹ค.

 

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
@WebServlet("/student/list")
public class StudentListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            ServletContext sc = this.getServletContext();
            Connection conn = (Connection) sc.getAttribute("conn");
 
            StudentDao studentDao = new StudentDao();
            studentDao.setConnection(conn);
 
            req.setAttribute("students", studentDao.selectList());
 
            resp.setContentType("text/html; charset=UTF-8");
            RequestDispatcher rd = req.getRequestDispatcher(
                    "/student/StudentList.jsp"
            );
            rd.include(req, resp);
        } catch (SQLException e) {
            req.setAttribute("error", e);
            RequestDispatcher rd = req.getRequestDispatcher("/Error.jsp");
            rd.forward(req, resp);
        }
    }
}
cs

 

StudentDao๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— setter๋ฅผ ๋จผ์ € ํ˜ธ์ถœํ•ด์„œ ServletContext์—์„œ ๊บผ๋‚ธ DB ์ปค๋„ฅ์…˜ ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•˜์˜€๋‹ค.

 

StudentListServlet์˜ DB ๊ด€๋ จ ์ฝ”๋“œ๊ฐ€ ๋ชจ๋‘ StudentDao์— ์ด๊ด€๋˜์—ˆ๋‹ค.

StudentListServlet์€ ์ปจํŠธ๋กค๋Ÿฌ๋กœ์จ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ๋Œ€ํ•ด ์–ด๋–ค Dao๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์–ด๋Š JSP๋กœ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋‚ด์•ผ ํ•˜๋Š”์ง€ ์กฐ์ •ํ•œ๋‹ค.

 

์‹ ๊ทœ ํšŒ์› ๊ฐ€์ž…(StudentAddServlet), ํšŒ์› ์‚ญ์ œ(StudentDeleteServlet), ํšŒ์› ์ •๋ณด ์ˆ˜์ •(StudentUpdateServlet), ๋กœ๊ทธ์ธ(LogInServlet)๋„ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ DAO๋ฅผ ๋ถ„๋ฆฌํ•˜๋ฉด ๋œ๋‹ค.

 

์ด๋กœ์จ ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ๋Š” MVC ์•„ํ‚คํ…์ฒ˜์— ๋”ฐ๋ผ ์„œ๋ธ”๋ฆฟ์€ ์ปจํŠธ๋กค๋Ÿฌ, Dao๋Š” ๋ชจ๋ธ, JSP๋Š” ๋ทฐ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.


  • ๊ด€๋ จ ๊ธ€

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (1) ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (2) ํšŒ์› ๊ฐ€์ž…

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (3) ํšŒ์› ์ •๋ณด ์ˆ˜์ •

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (4) ๋ทฐ(JSP) ๋ถ„๋ฆฌํ•˜๊ธฐ

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (5) ๋กœ๊ทธ์ธ/๋กœ๊ทธ์•„์›ƒ(HttpSession)

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (6) ํšŒ์› ์‚ญ์ œ

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (7) DAO ๋ถ„๋ฆฌํ•˜๊ธฐ

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€