-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManager.java
More file actions
36 lines (34 loc) · 1.43 KB
/
Copy pathManager.java
File metadata and controls
36 lines (34 loc) · 1.43 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
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Manager {
public void addProduct(Product product) {
String sql = "INSERT INTO Products (name, price, stock) VALUES (?, ?, ?)";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, product.getName());
pstmt.setDouble(2, product.getPrice());
pstmt.setInt(3, product.getStock());
pstmt.executeUpdate();
System.out.println("Product added successfully.");
} catch (SQLException e) {
e.printStackTrace();
}
}
public void viewInventory() {
String sql = "SELECT * FROM Products";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
System.out.println("ID: " + rs.getInt("productID") +
", Name: " + rs.getString("name") +
", Price: " + rs.getDouble("price") +
", Stock: " + rs.getInt("stock"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}