개발자 도전기
[Spring] JDBC로 데이터 수정하기 (UPDATE) 본문
UPDATE
# UPDATE 테이블명
# SET 컬럼명 = 바꿀값, 컬렴명 = 바꿀값 ,,,
# WHERE 레코드의 조건
UPDATE Customers
SET Address = 'STARK TOWER',
City = '맨하탄',
PostalCode = '12345'
WHERE CustomerID = 10;
# WHERE 생략하면 전체 레코드 바뀜
UPDATE Customers
SET Country = 'uk'
# 기존 값 사용 가능
UPDATE
Products
SET Price = Price * 2
WHERE ProductID = 1;
UPDATE 사용해서 데이터 수정하기
어떤 값을 수정할 지 알 수 없기 때문에 새로운 데이터는 수정하고 기존의 데이터는 덮어씌우는 방식으로 수정
@Controller
@RequestMapping("main30")
public class Controller30 {
@Autowired
private DataSource dataSource;
// GET 방식으로 수정할 데이터 조회
@GetMapping("sub2")
public void method2(Integer id, Model model) throws Exception {
if (id != null) {
String sql = """
SELECT *
FROM Employees
WHERE EmployeeID = ?
""";
Connection conn = dataSource.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
try (rs; pstmt; conn) {
if (rs.next()) {
MyBean255Employee e = new MyBean255Employee();
e.setEmployeeID(rs.getString(1));
e.setLastName(rs.getString(2));
e.setFirstName(rs.getString(3));
e.setBirthDate(rs.getString(4));
e.setPhoto(rs.getString(5));
e.setNotes(rs.getString(6));
model.addAttribute("employee", e);
}
}
}
}
// 조회한 데이터 수정(UPADTE)
@PostMapping("sub2/update")
public String update2(MyBean255Employee employee, RedirectAttributes rttr) throws Exception {
String sql = """
UPDATE Employees
Set LastName = ?,
FirstName = ?,
BirthDate = ?,
Photo = ?,
Notes = ?
WHERE EmployeeID = ?
""";
Connection conn = dataSource.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
try (conn; pstmt) {
pstmt.setString(1, employee.getLastName());
pstmt.setString(2, employee.getFirstName());
pstmt.setString(3, employee.getBirthDate());
pstmt.setString(4, employee.getPhoto());
pstmt.setString(5, employee.getNotes());
pstmt.setString(6, employee.getEmployeeID());
int rowCount = pstmt.executeUpdate();
if (rowCount > 0) {
rttr.addFlashAttribute("message", employee.getEmployeeID() + "번 직원이 수정되었습니다");
} else {
rttr.addFlashAttribute("message", "수정되지 않았습니다");
}
}
rttr.addFlashAttribute("id", employee.getEmployeeID());
// 해당 직원 조회한 GET 방식으로 리다이렉트
return "redirect:/main30/sub2?id=" + employee.getEmployeeID();
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyBean255Employee {
private String employeeID;
private String lastName;
private String firstName;
private String birthDate;
private String photo;
private String notes;
}
<html>
<head>
<title>Title</title>
</head>
<body>
<c:if test="${not empty message}">
<div style="background-color: skyblue; padding: 20px">
${message}
</div>
</c:if>
<h3>직원 정보 수정</h3>
<form action="">
직원 번호
<input type="text" name="id">
<button>조회</button>
</form>
<hr>
<form action="/main30/sub2/update" method="post">
<div>
번호
<input type="text" value="${employee.employeeID}" name="employeeID">
</div>
<div>
성
<input type="text" value="${employee.lastName}" name="lastName">
</div>
<div>
이름
<input type="text" value="${employee.firstName}" name="firstName">
</div>
<div>
생년월일
<input type="date" value="${employee.birthDate}" name="birthDate">
</div>
<div>
사진
<input type="text" value="${employee.photo}" name="photo">
</div>
<div>
노트
<textarea name="notes">
${employee.notes}
</textarea>
</div>
<div>
<button style="background-color: palevioletred">수정</button>
</div>
</form>
</body>
</html>
'개발공부 > Spring' 카테고리의 다른 글
[Spring] MyBatis란? (0) | 2024.04.29 |
---|---|
[Spring] IOC (제어의 역전) (0) | 2024.04.25 |
[Spring] JDBC로 데이터 삭제하기(DELETE) (0) | 2024.04.23 |
[Spring] JDBC로 데이터 등록하기 (INSERT INTO) (0) | 2024.04.23 |
[STUDY] JDBC란? (0) | 2024.04.23 |