
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) :
์ญ์ (DELETE) :
๐ข 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>
form ์์ ์๋ id๋ฅผ ์ญ์ ํ๊ธฐ ๋๋ฌธ์ id๋ฅผ ๋ณด์ฌ์ฃผ๋ input ์์๋ฅผ ์์ฑํ๊ณ ํด๋น ์ง์์ ์ญ์ ํฉ๋๋ค. ์ด๋ฏธ ํด๋น ์ง์์ ๋ณด์ฌ์ฃผ๋ input์ด ์๊ธฐ ๋๋ฌธ์ hidden์ผ๋ก ๊ฐ๋ ค์ค๋๋ค.onsubmit="return confirm('์ ๋ง๋ก ์ญ์ ํ์๊ฒ ์ต๋๊น?')" : ์๋ฒ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ถํ๊ธฐ ์ ํ๋ฒ๋ ํ์ธํ๋ JavaScript ์ฝ๋์
๋๋ค. ์ด๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์๊ฐ ์ญ์ ์์
์ ์งํํ๊ธฐ ์ ์ ํ์ธ ๋ํ ์์๋ฅผ ํ์ํ ์ ์์ต๋๋ค.์ง์ ์กฐํ

์ง์ ์ญ์

์ญ์ ์ปดํ

์ญ์ ๋ ์ง์ ๋ค์ ์กฐํ
