๐Ÿ’ป ์ฝ”๋”ฉ ์ผ๊ธฐ : [Spring JDBC] 'DELETE' ํŽธ

ybkยท2024๋…„ 4์›” 22์ผ

spring

๋ชฉ๋ก ๋ณด๊ธฐ
15/55
post-thumbnail

๐Ÿ”” 'Spring JDBC๋ฅผ ํ™œ์šฉํ•œ DB์— ๋ฐ์ดํ„ฐ ์‚ญ์ œํ•˜๋Š” ๋ฐฉ๋ฒ•'์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž!


๐Ÿ’Ÿ DELETE

DELETE : ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์—์„œ ํ•˜๋‚˜ ์ด์ƒ์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

DELETE FROM ํ…Œ์ด๋ธ”๋ช… WHERE ์ปฌ๋Ÿผ๋ช… = ์‚ญ์ œํ•˜๊ณ  ์‹ถ์€ ์ปฌ๋Ÿผ๊ฐ’;

๐ŸŸฅ ์ฃผ์˜ํ•  ์  : ๋จผ์ € ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์กฐํšŒ๋ฅผ ํ•ด์ค€ ๋‹ค์Œ์— ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ทธ ๋•Œ ์‚ญ์ œํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.


Controller29.java

@Controller
@RequestMapping("main29")
public class Controller29 {

    @Autowired
    private DataSource dataSource;

    @GetMapping("sub2")
    public void method3(@RequestParam("id") Integer id, Model model) throws SQLException {

        if (id !=  null){
            String sql = "SELECT * FROM Employees WHERE EmployeeID = ?";

            Connection conn = dataSource.getConnection();
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            ResultSet rs = preparedStatement.executeQuery();

            try (rs;preparedStatement;conn) {
                if (rs.next()){
                    MyBean258Employee employee = new MyBean258Employee();
                    employee.setId(rs.getString(1));
                    employee.setLastName(rs.getString(2));
                    employee.setFirstName(rs.getString(3));
                    employee.setBirthDate(rs.getString(4));
                    employee.setPhoto(rs.getString(5));
                    employee.setNotes(rs.getString(6));

                    model.addAttribute("employee", employee);
                }
            }
        }

    }

    @PostMapping("sub2/delete")
    public String method4(Integer id, RedirectAttributes rttr) throws SQLException {

        String sql = "DELETE FROM Employees WHERE EmployeeID = ?";

        Connection conn = dataSource.getConnection();
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1,id);
        try (pstmt;conn){
			int rowCount = pstmt.executeUpdate();
            if (rowCount == 1){
                rttr.addFlashAttribute("message", "์ง์›"+id+" ์ด ์‚ญ์ œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
            }else {
                rttr.addFlashAttribute("message", "ํ•ด๋‹น ์ง์›์€ ์‚ญ์ œ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.");
            }

        }
        return "redirect:/main29/sub2";
    }
}
  • ์กฐํšŒ(SELECT) :

    • "/main29/sub2"๋กœ์˜ GET ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ œ๊ณต๋œ ์ง์› ID๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์ง์› ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ  ๋ชจ๋ธ ์†์„ฑ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
    • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๊ฒฐํ•˜๊ณ  SQL ์ฟผ๋ฆฌ ์‹คํ–‰์„ ์œ„ํ•ด PreparedStatement๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ณ  SQL ์ฟผ๋ฆฌ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์‚ฌ์šฉ๋  ๊ฐ’(?์— ๋“ค์–ด๊ฐˆ ๊ฐ’) ์„ค์ •(set)ํ•˜๊ณ  SQL ์ฟผ๋ฆฌ ์‹คํ–‰ํ•˜๊ณ  ResultSet ๊ฐ์ฒด์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  • ์‚ญ์ œ(DELETE) :

    • "/main29/sub2/delete"๋กœ POST ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ง์› ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์‚ญ์ œ ๊ฒฐ๊ณผ์— ๋”ฐ๋ฅธ ํ•ด๋‹น ๋ฉ”์‹œ์ง€๋ฅผ ์„ค์ •ํ•˜๊ณ  ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค.
    • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๊ฒฐํ•˜๊ณ  SQL ์ฟผ๋ฆฌ ์‹คํ–‰์„ ์œ„ํ•ด PreparedStatement๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ณ  SQL ์ฟผ๋ฆฌ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์‚ฌ์šฉ๋  ๊ฐ’(?์— ๋“ค์–ด๊ฐˆ ๊ฐ’) ์„ค์ •(set)ํ•˜๊ณ  executeUpdate() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DELETE ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ์‚ญ์ œ๋œ ํ–‰์˜ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ "/main29/sub2"๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•˜์—ฌ ์‚ญ์ œ ์ž‘์—… ํ›„์˜ ๊ฒฐ๊ณผ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“ข ResultSet๊ณผ executeUpdate()์˜ ์ฐจ์ด?

  • ResultSet :

    • SELECT ์ฟผ๋ฆฌ ์‹คํ–‰ ํ›„, ๊ทธ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ ResultSet ๊ฐ์ฒด์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    • ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜๋ณตํ•˜์—ฌ ์ฝ๊ฑฐ๋‚˜ ํŠน์ • ๋ ˆ์ฝ”๋“œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • executeUpdate():

    • ์‹คํ–‰ํ•œ ์ฟผ๋ฆฌ์— ์˜ํ•ด ๋ณ€๊ฒฝ๋œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํ–‰ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.(INSERT, UPDATE, DELETE)

sub2.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<c:if test="${not empty message}">
    <div style="background-color: rosybrown; padding: 20px">${message}</div>
</c:if>
<h3>์ง์› ์กฐํšŒ</h3>
<form>
    ์ง์› ๋ฒˆํ˜ธ
    <input type="text" name="id">
    <button>์กฐํšŒ</button>
</form>
<hr>
<c:if test="${empty customer}">
    <p>์กฐํšŒ๋œ ๊ณ ๊ฐ์ด ์—†์Šต๋‹ˆ๋‹ค.</p>
</c:if>
<c:if test="${not empty employee}">
    <div>
        ์ง์› ๋ฒˆํ˜ธ <input type="text" readonly value="${employee.id}">
    </div>
    <div>
        ์„ฑ <input type="text" readonly value="${employee.lastName}">
    </div>
    <div>
        ์ด๋ฆ„ <input type="text" readonly value="${employee.firstName}">
    </div>
    <div>
        ์ƒ๋…„์›”์ผ <input type="text" readonly value="${employee.birthDate}">
    </div>
    <div>
        ์‚ฌ์ง„ <input type="text" readonly value="${employee.photo}">
    </div>
    <div>
        ์„ค๋ช… <input type="text" readonly value="${employee.notes}">
    </div>

    <form action="/main29/sub2/delete" method="post" onsubmit="return confirm('์ •๋ง๋กœ ์‚ญ์ œํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?')">
        <div>
            <input hidden="hidden" type="text" name="id" value="${employee.id}">
        </div>
        <button style="background-color: rosybrown">์‚ญ์ œ</button>
    </form>
</c:if>
</body>
</html>
  • @RequestParam("id") ์ƒ๋žต ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. (๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„๊ณผ HTML ํผ ์š”์†Œ์˜ name ์†์„ฑ์ด ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ ์ž๋™์œผ๋กœ ๋งคํ•‘)
  • ์‚ญ์ œ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•  ๋•Œ, form ์•ˆ์— ์žˆ๋Š” id๋ฅผ ์‚ญ์ œํ•˜๊ธฐ ๋•Œ๋ฌธ์— id๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” input ์š”์†Œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ํ•ด๋‹น ์ง์›์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ ํ•ด๋‹น ์ง์›์„ ๋ณด์—ฌ์ฃผ๋Š” input์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— hidden์œผ๋กœ ๊ฐ€๋ ค์ค๋‹ˆ๋‹ค.
  • onsubmit="return confirm('์ •๋ง๋กœ ์‚ญ์ œํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?')" : ์„œ๋ฒ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ์ถœํ•˜๊ธฐ ์ „ ํ•œ๋ฒˆ๋„ ํ™•์ธํ•˜๋Š” JavaScript ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž๊ฐ€ ์‚ญ์ œ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ธฐ ์ „์— ํ™•์ธ ๋Œ€ํ™” ์ƒ์ž๋ฅผ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ง์› ์กฐํšŒ

์ง์› ์‚ญ์ œ

์‚ญ์ œ ์ปดํŽŒ

์‚ญ์ œ๋œ ์ง์› ๋‹ค์‹œ ์กฐํšŒ

profile
๊ฐœ๋ฐœ์ž ์ค€๋น„์ƒ~

0๊ฐœ์˜ ๋Œ“๊ธ€