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

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

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

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

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

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

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


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

ํ•™์ƒ ์ƒ์„ธ ์ •๋ณด ๋งํฌ ์ถ”๊ฐ€

ํ•™์ƒ ๋ชฉ๋ก ํŽ˜์ด์ง€์— ํ•™์ƒ์˜ ์ƒ์„ธ ์ •๋ณด๋ฅผ ์š”์ฒญํ•˜๋Š” ๋งํฌ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
out.println("<body><h1>ํ•™์ƒ ๋ชฉ๋ก</h1>");
out.println("<p><a href='add'>์‹ ๊ทœ ํ•™์ƒ</a></p>");
while (rs.next()) {
    out.println(
            rs.getString("STUDENT_NO"+ ", " +
                    rs.getString("DEPARTMENT"+ ", " +
                    "<a href='update?student_no=" + rs.getString("STUDENT_NO"+ "'>" +    // ๋งํฌ ์ถ”๊ฐ€
                    rs.getString("STUDENT_NAME"+ "</a>, " +
                    rs.getInt("GRADE"+ ", " +
                    rs.getString("GENDER"+ ", " +
                    rs.getInt("AGE"+ ", " +
                    rs.getString("PHONE_NUMBER"+ ", " +
                    rs.getString("ADDRESS"+ "<br>");
}
out.println("</body></html>");
cs

ํ•™์ƒ ์ƒ์„ธ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์„œ๋ธ”๋ฆฟ์˜ URL์€ '/student/update'๋กœ ํ• ๊ฒƒ์ด๋ฏ€๋กœ href='update'๋กœ ์ง€์ •ํ•ด์ฃผ๊ณ  ์ƒ์„ธ ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๋ ค๋ฉด ํ•™๋ฒˆ์ด ํ•„์š”ํ•˜๋ฏ€๋กœ ?student_no=๋ฅผ ๋ถ™์—ฌ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํฌํ•จ์‹œํ‚จ๋‹ค.

 

ํ•™์ƒ ์ด๋ฆ„์— ์ƒ์„ธ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์„œ๋ธ”๋ฆฟ ์š”์ฒญ ๋งํฌ๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

 

ํ•™์ƒ ์ƒ์„ธ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์„œ๋ธ”๋ฆฟ ๊ตฌํ˜„

StudentUpdateServlet ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ  doGet() ๋ฉ”์†Œ๋“œ๋ฅผ overrideํ•˜์—ฌ ํ•™์ƒ ์ƒ์„ธ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

 

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
@WebServlet("/student/update")
public class StudentUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
 
        try {
            // context init parameter๋ฅผ ์ด์šฉํ•œ JDBC ์—ฐ๊ฒฐ
            ServletContext sc = this.getServletContext();
            Class.forName(sc.getInitParameter("driver"));
            conn = DriverManager.getConnection(
                    sc.getInitParameter("url"),
                    sc.getInitParameter("username"),
                    sc.getInitParameter("password")
            );
            stmt = conn.createStatement();
            rs = stmt.executeQuery(
                    "SELECT STUDENT_NO, DEPARTMENT, STUDENT_NAME, GRADE, GENDER, AGE, PHONE_NUMBER, ADDRESS" +
                            " FROM STUDENT" +
                            " WHERE STUDENT_NO = " + req.getParameter("student_no")
            );
            rs.next();
 
            resp.setContentType("text/html; charset=UTF-8");
            PrintWriter out = resp.getWriter();
            out.println("<html><head><title>ํ•™์ƒ์ •๋ณด</title></head>");
            out.println("<body><h1>ํ•™์ƒ์ •๋ณด</h1>");
            out.println("<form action='update' method='post'>");
            out.println("ํ•™๋ฒˆ: <input type='text' name='student_no' value='" + req.getParameter("student_no"+ "' readonly><br>");
            out.println("ํ•™๊ณผ: <input type='text' name='department' value='" + rs.getString("DEPARTMENT"+ "'><br>");
            out.println("์ด๋ฆ„: <input type='text' name='student_name' value='" + rs.getString("STUDENT_NAME"+ "'><br>");
            out.println("ํ•™๋…„: <input type='text' name='grade' value='" + rs.getInt("GRADE"+ "' readonly><br>");
            out.println("์„ฑ๋ณ„: <input type='text' name='gender' value='" + rs.getString("GENDER"+ "' readonly><br>");
            out.println("๋‚˜์ด: <input type='text' name='age' value='" + rs.getInt("AGE"+ "' readonly><br>");
            out.println("์ „ํ™”๋ฒˆํ˜ธ: <input type='text' name='phone_number' value='" + rs.getString("PHONE_NUMBER"+ "'><br>");
            out.println("์ฃผ์†Œ: <input type='text' name='address' value='" + rs.getString("ADDRESS"+ "'><br>");
            out.println("<input type='submit' value='์ˆ˜์ •'>");
            out.println("<input type='button' value='์ทจ์†Œ' onClick='location.href=\"list\"'>");
            out.println("</form></body></html>");
        } catch (ClassNotFoundException e) {
            throw new ServletException(e);
        } catch (SQLException e) {
            throw new ServletException(e);
        } finally {
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
cs

 

WebServlet ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•ด ์„œ๋ธ”๋ฆฟ URL์„ ์ง€์ •ํ•œ๋‹ค.

ํ•™์ƒ ๋ชฉ๋ก ์กฐํšŒ ์„œ๋ธ”๋ฆฟ๊ณผ ๋‹ฌ๋ฆฌ Context parameter๋ฅผ ์‚ฌ์šฉํ•ด์„œ JDBC ๋“œ๋ผ์ด๋ฒ„๋ฅผ ๋กœ๋”ฉํ•˜๊ณ  ์—ฐ๊ฒฐํ•˜์˜€๋‹ค.

 

์ฐธ๊ณ  : Servlet init parameter์™€ Context init parameter๋ฅผ ์ด์šฉํ•ด DB ์—ฐ๊ฒฐ ์ •๋ณด ๊ด€๋ฆฌํ•˜๊ธฐ

- Servlet Init Parameter(์„œ๋ธ”๋ฆฟ ์ดˆ๊ธฐํ™” ๋งค๊ฐœ๋ณ€์ˆ˜)๋กœ web.xml์— DB ์—ฐ๊ฒฐ ์ •๋ณด ๊ด€๋ฆฌํ•˜๊ธฐ

- Context Parameter(์ปจํ…์ŠคํŠธ ๋งค๊ฐœ๋ณ€์ˆ˜)๋กœ web.xml์— DB ์—ฐ๊ฒฐ ์ •๋ณด ๊ด€๋ฆฌํ•˜๊ธฐ

 

 

์š”์ฒญ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜์–ด์˜จ ํ•™๋ฒˆ(student_no)์œผ๋กœ ํ•™์ƒ ์ •๋ณด๋ฅผ ์งˆ์˜ํ•˜๊ณ  ํ•œ ๋ช…์˜ ํšŒ์› ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋ฏ€๋กœ next()๋ฅผ ํ•œ ๋ฒˆ๋งŒ ํ˜ธ์ถœํ•œ๋‹ค.

 

์ถœ๋ ฅ HTML์€ ํ•™์ƒ ์ƒ์„ธ ํŽ˜์ด์ง€์—์„œ ๋ฐ”๋กœ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋„๋ก <form> ํ˜•ํƒœ๋กœ ๋งŒ๋“ ๋‹ค. ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋Š” ํ•„๋“œ๋Š” readonly ์†์„ฑ์„ ์ถ”๊ฐ€ํ•ด์„œ ์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.

 

'์ทจ์†Œ' ๋ฒ„ํŠผ์€ ๋‹ค์‹œ ํ•™์ƒ ๋ชฉ๋ก์œผ๋กœ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก onClick ์†์„ฑ์— location.href='list' ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ง€์ •ํ•œ๋‹ค.

location์€ ์›น ๋ธŒ๋ผ์šฐ์ €์˜ ํŽ˜์ด์ง€ ์ด๋™์„ ๊ด€๋ฆฌํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋กœ href ํ”„๋กœํผํ‹ฐ์— URL์„ ์„ค์ •ํ•œ๋‹ค.

์ฆ‰ '์ทจ์†Œ' ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด /student/list ์š”์ฒญ์„ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

 

<form>์˜ action ๊ฐ’์€ ํšŒ์› ์ƒ์„ธ ํŽ˜์ด์ง€์™€ ๋™์ผํ•œ URL 'update'๋ฅผ ์ง€์ •ํ•œ๋‹ค. method๋Š” 'post' ์ด๋ฏ€๋กœ ์„œ๋ฒ„์— ์š”์ฒญํ•  ๋•Œ POST ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค.

 

ํ•™์ƒ ์ด๋ฆ„์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ๊ฒฐ๊ณผ ํ™”๋ฉด

 

doPost() ๊ตฌํ˜„

StudentUpdateServlet์— doPost()๋ฅผ overrideํ•˜์—ฌ ํ•™์ƒ ์ •๋ณด๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

 

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
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("UTF-8");    // Filter๋กœ ์ฒ˜๋ฆฌ
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
        // context init parameter๋ฅผ ์ด์šฉํ•œ JDBC ์—ฐ๊ฒฐ
        ServletContext sc = this.getServletContext();
        Class.forName(sc.getInitParameter("driver"));
        conn = DriverManager.getConnection(
                sc.getInitParameter("url"),
                sc.getInitParameter("username"),
                sc.getInitParameter("password")
        );
 
        pstmt = conn.prepareStatement(
                "UPDATE STUDENT SET DEPARTMENT = ?, STUDENT_NAME = ?, PHONE_NUMBER = ?, ADDRESS = ? WHERE STUDENT_NO = ?"
        );
        pstmt.setString(1, req.getParameter("department"));
        pstmt.setString(2, req.getParameter("student_name"));
        pstmt.setString(3, req.getParameter("phone_number"));
        pstmt.setString(4, req.getParameter("address"));
        pstmt.setString(5, req.getParameter("student_no"));
        pstmt.executeUpdate();
        resp.sendRedirect("list");
    } catch (ClassNotFoundException | SQLException e) {
        throw new ServletException(e);
    } finally {
        try {
            if(pstmt != null) pstmt.close();
            if(conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
cs

setCharacterEncoding()์€ ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›๋Š” ๊ณณ๋งˆ๋‹ค ์ž‘์„ฑํ•ด์•ผ ํ•ด์„œ ์ค‘๋ณต๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  Filter๋กœ ์ฒ˜๋ฆฌํ•˜์˜€๋‹ค.

 

์ฐธ๊ณ  : Servlet Filter ์‚ฌ์šฉํ•˜๊ธฐ

 

doGet()์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ JDBC ๋“œ๋ผ์ด๋ฒ„๋ฅผ ๋กœ๋”ฉํ•˜๊ณ  ์—ฐ๊ฒฐํ•˜๋Š” ๋ถ€๋ถ„์€ Context init parameter๋ฅผ ์ด์šฉํ–ˆ๋‹ค.

 

ํ•™์ƒ ์ •๋ณด๋ฅผ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด UPDATE๋ฌธ์„ ์ž‘์„ฑํ•œ๋‹ค.

 

executeUpdate() ํ˜ธ์ถœ ํ›„ sendRedirect("list")๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํ•™์ƒ ์ •๋ณด ๋ณ€๊ฒฝ ํ›„ ํ•™์ƒ ๋ชฉ๋ก ํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•œ๋‹ค. ํ•™์ƒ ๋ชฉ๋ก ํŽ˜์ด์ง€์—์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํ•™๊ณผ๋ฅผ ๋ฒ•ํ•™๊ณผ์—์„œ ๊ฒฝ์˜ํ•™๊ณผ๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  ์ˆ˜์ • ํด๋ฆญ

 

ํ•™์ƒ์˜ ํ•™๊ณผ๊ฐ€ ๊ฒฝ์˜ํ•™๊ณผ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค.


  • ๊ด€๋ จ ๊ธ€

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

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

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

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

 

#JAVA MySQL UPDATE

 

References

์—ดํ˜ˆ๊ฐ•์˜ ์ž๋ฐ” ์›น ๊ฐœ๋ฐœ ์›Œํฌ๋ถ(ํ”„๋ฆฌ๋ ‰, 2016, ์—„์ง„์˜)

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€