
UPDATE : ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ
์ด๋ธ์ ๊ธฐ์กด ๋ ์ฝ๋๋ฅผ ์์ ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
UPDATE ํ ์ด๋ธ๋ช SET ์ปฌ๋ผ๋ช = ๋ฐ๊ฟ๊ฐ, ์ปฌ๋ ด๋ช = ๋ฐ๊ฟ๊ฐ, ... WHERE ๋ ์ฝ๋์ ์กฐ๊ฑด
๐ฅ ์ฃผ์ํ ์ : ๋จผ์ ๋ฐ์ดํฐ๋ฅผ ์์ ํ๊ธฐ ์ ์ ์กฐํ๋ฅผ ํด์ค ๋ค์์ ํด๋น ๋ฐ์ดํฐ๊ฐ ์๋ค๋ฉด ๊ทธ ๋ ์์ ํด์ฃผ๋ ๊ฒ์ด ์ข์ต๋๋ค.
Controller30.java
@Controller
@RequestMapping("main30")
public class Controller30 {
@Autowired
private DataSource dataSource;
@GetMapping("sub1")
public void method1(Integer id, Model model) throws SQLException {
// ๊ณ ๊ฐ ๋ฒํธ๋ฅผ ๋ฐ์ ํ ์กฐํํ ๋ค์์ ์ง์ธ ์์
if (id != null){
String sql = """
SELECT * FROM Customers WHERE CustomerID = ?
""";
Connection conn = dataSource.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,id); //์ฒซ๋ฒ์งธ ๋ฌผ์ํ์ id ๋ฃ๊ธฐ
ResultSet rs = pstmt.executeQuery();
try (rs;pstmt;conn){
if (rs.next()) {
MyBean254Customer bean = new MyBean254Customer();
bean.setId(rs.getInt(1));
bean.setName(rs.getString(2));
bean.setContactName(rs.getString(3));
bean.setAddress(rs.getString(4));
bean.setCity(rs.getString(5));
bean.setPostalCode(rs.getString(6));
bean.setCountry(rs.getString(7));
model.addAttribute("customer", bean);
}
}
}
}
@PostMapping("sub1/update")
public String update1(MyBean254Customer customer, RedirectAttributes rttr) throws SQLException {
System.out.println(customer);
String sql = """
UPDATE Customers
SET CustomerName = ?,
ContactName = ?,
Address = ?,
City = ?,
PostalCode = ?,
Country = ?
WHERE CustomerID = ?
""";
Connection con = dataSource.getConnection();
PreparedStatement pstmt = con.prepareStatement(sql);
try (pstmt;con){
pstmt.setString(1,customer.getName());
pstmt.setString(2,customer.getContactName());
pstmt.setString(3,customer.getAddress());
pstmt.setString(4,customer.getCity());
pstmt.setString(5,customer.getPostalCode());
pstmt.setString(6,customer.getCountry());
pstmt.setInt(7, customer.getId());
int rowCount = pstmt.executeUpdate();
if(rowCount > 0){
rttr.addFlashAttribute("message",customer.getId() + "๋ฒ ๊ณ ๊ฐ์ด ์์ ๋์์ต๋๋ค.");
}else{
rttr.addFlashAttribute("message", "์์ ๋์ง ์์์ต๋๋ค.");
}
}
rttr.addAttribute("id", customer.getId());
return "redirect:/main30/sub1";
}
}
์กฐํ(SELECT) :
์์ (UPDATE) :
sub1.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: khaki; padding: 20px">${message}</div>
</c:if>
<h3>๊ณ ๊ฐ ์ ๋ณด ์์ </h3>
<form action="">
๋ฒํธ
<input type="text" name="id">
<button>์กฐํ</button>
</form>
<hr>
<form action="/main30/sub1/update" method="post" onsubmit="return confirm('์ ๋ง๋ก ์์ ํ์๊ฒ ์ต๋๊น?')">
<div>
๋ฒํธ <input type="text" name="id" value="${customer.id}">
</div>
<div>
์ด๋ฆ <input type="text" name="name" value="${customer.name}">
</div>
<div>
๊ณ์ฝ๋ช
<input type="text" name="contactName" value="${customer.contactName}">
</div>
<div>
์ฃผ์ <input type="text" name="address" value="${customer.address}">
</div>
<div>
๋์ <input type="text" name="city" value="${customer.city}">
</div>
<div>
์ฐํธ๋ฒํธ <input type="text" name="postalCode" value="${customer.postalCode}">
</div>
<div>
๊ตญ๊ฐ <input type="text" name="country" value="${customer.country}">
</div>
<div>
<input type="submit" value="์์ ">
</div>
</form>
</body>
</html>
๊ณ ๊ฐ ์ ๋ณด ์กฐํ

๊ตญ๊ฐ๋ฅผ ๋
์ผ์์ Gremany๋ก ์์
์์ ์ปดํ

์์ ๋ ๊ณ ๊ฐ ๋ค์ ์กฐํ

//Mapper01.java
// Inversion Of Control (IOC)
@Component
public class Mapper01 {
public MyBean254Customer getCustomerById(Integer id) throws SQLException {
// ๊ณ ๊ฐ ๋ฒํธ๋ฅผ ๋ฐ์ ํ ์กฐํํ ๋ค์์ ์ง์ธ ์์
if (id != null){
String sql = """
SELECT * FROM Customers WHERE CustomerID = ?
""";
Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/w3schools", "root", "*****");
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,id); //์ฒซ๋ฒ์งธ ๋ฌผ์ํ์ id ๋ฃ๊ธฐ
ResultSet rs = pstmt.executeQuery();
try (rs;pstmt;conn){
if (rs.next()) {
MyBean254Customer bean = new MyBean254Customer();
bean.setId(rs.getInt(1));
bean.setName(rs.getString(2));
bean.setContactName(rs.getString(3));
bean.setAddress(rs.getString(4));
bean.setCity(rs.getString(5));
bean.setPostalCode(rs.getString(6));
bean.setCountry(rs.getString(7));
return bean;
}
}
}
return null;
}
}
//Controller30.java
@Autowired
private Mapper01 mapper;
@GetMapping("sub1")
public void method1(Integer id, Model model) throws SQLException {
MyBean254Customer c = mapper.getCustomerById(id);
model.addAttribute("customer", c);
}
Mapper๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๊ฐ์ฒด ๊ฐ์ ๋งคํ์ ์ํํ๋ ์ญํ ์
๋๋ค.
Mapper01 ํด๋์ค๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ๋ฐ ์ฟผ๋ฆฌ๋ฅผ ๋ด๋นํฉ๋๋ค. ์ด๊ฒ์ @Component๋ก ์ฃผ์์ด ๋ฌ๋ฆฐ ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋ก๋์ด ์คํ๋ง ์ปจํ
์ด๋์ ์ํด ๊ด๋ฆฌ๋ฉ๋๋ค.
Controller30 ํด๋์ค์์๋ Mapper01 ํด๋์ค๋ฅผ ์๋์ผ๋ก ์ฃผ์
๋ฐ์ ์ฌ์ฉํฉ๋๋ค. ์ด๊ฒ์ ์คํ๋ง์ ์์กด์ฑ ์ฃผ์
(Dependency Injection)์ ํตํด ์ด๋ฃจ์ด์ง๋๋ค.
method1 ๋ฉ์๋์์๋ ๋ ์ด์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ง์ ์ํธ ์์ฉํ์ง ์์ต๋๋ค. ๋์ Mapper01 ํด๋์ค์ getCustomerById ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์
์ ์ฒ๋ฆฌํฉ๋๋ค.