-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateOrderDao.java
More file actions
64 lines (55 loc) · 1.86 KB
/
UpdateOrderDao.java
File metadata and controls
64 lines (55 loc) · 1.86 KB
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
57
58
59
60
61
62
63
64
package com.example.order.dao;
import com.example.order.dto.ParamsDto;
import com.example.order.util.Database;
import com.example.order.util.ExceptionHandler;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* DAO to update an order
*/
public class UpdateOrderDao {
private final Database database;
/**
* Constructor
*
* @param database Database object
*/
public UpdateOrderDao(Database database) {
this.database = database;
}
/**
* Updates the status of an order
*
* @param paramsDto Object with the parameters for the operation
* @return Number of affected rows
*/
public int updateOrderStatus(ParamsDto paramsDto) throws IOException {
int numberResults = 0;
try (Connection con = database.getConnection();
PreparedStatement ps = createPreparedStatement(con, paramsDto)
) {
numberResults = ps.executeUpdate();
} catch (SQLException ex) {
ExceptionHandler.handleException(ex);
}
return numberResults;
}
/**
* Creates a PreparedStatement object to update the order
*
* @param con Connnection object
* @param paramsDto Object with the parameters to set on the PreparedStatement
* @return A PreparedStatement object
* @throws SQLException In case of an error
*/
private @NotNull PreparedStatement createPreparedStatement(@NotNull Connection con, @NotNull ParamsDto paramsDto) throws SQLException {
String query = "UPDATE orders o SET o.order_status = ? WHERE o.order_id = ?";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, paramsDto.getStatus());
ps.setLong(2, paramsDto.getOrderId());
return ps;
}
}