-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoreItem.java
More file actions
48 lines (38 loc) · 1.38 KB
/
StoreItem.java
File metadata and controls
48 lines (38 loc) · 1.38 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
package StoreItem;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class StoreItem {
private int id;
private String name;
private float price;
public static StoreItem find(int id) throws SQLException {
StoreItem item = null;
try (
Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT id, name, price FROM items WHERE id=?");
) {
statement.setInt(1, id);
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
item = new StoreItem();
item.setId(resultSet.getInt("id"));
item.setName(resultSet.getString("name"));
item.setPrice(resultSet.getFloat("price"));
}
}
}
return item;
}
public void save() throws SQLException {
try (
Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement("REPLACE INTO items VALUES (? ? ?)");
) {
statement.setInt(1, this.id);
statement.setString(2, this.name);
statement.setFloat(3, this.price);
statement.executeQuery()
}
}
}