-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnect.java
More file actions
116 lines (107 loc) · 4.19 KB
/
Connect.java
File metadata and controls
116 lines (107 loc) · 4.19 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import javax.swing.*;
import java.sql.*;
public class Connect {
private Connection connect() {
String url = "jdbc:sqlite:InventoryDatabase.sqlite";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
public void checkInventory(int sku) {
String query = "SELECT * FROM Inventory where SKU=?";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(query)) {
// set the value
pstmt.setInt(1, sku);
//
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
System.out.println("its in there");
} else {
System.out.println("Value not in Table");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
// and Quantity = ?
}
public String displayInventory() {
String message = null;
String sql = "SELECT SKU, Quantity FROM Inventory";
try (Connection conn = this.connect();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
// loop through the result set
while (rs.next()) {
message = "SKU" + "\t" + "Quantity" + "\n" + rs.getInt("Sku") + "\t" +
rs.getInt("Quantity");
//System.out.println(rs.getInt("SKU") + "\t" +
// rs.getInt("Quantity"));
}
} catch (SQLException e) {
message = e.getMessage();
}
return message;
}
public void transaction(int sku, int quant, boolean addition) {
String sql = "SELECT SKU, Quantity FROM Inventory";
try (Connection conn = this.connect();
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(sql)) {
while (rs.next()) {
int SKU = rs.getInt("Sku");
int Quantity = rs.getInt("Quantity");
if (sku == SKU) {
if(!addition) {
if (Quantity < quant) {
String outMessage = "Item amount exceeds level of Inventory" + SKU + "is" + quant;
JOptionPane.showMessageDialog(null, outMessage);
} else {
int q = Quantity - quant;
conn.close();
finalizeAmount(sku, q);
}
} else {
if(quant > 0) {
conn.close();
int amount = quant + Quantity;
finalizeAmount(sku, amount);
}
}
} else {
JOptionPane.showMessageDialog(null, "Item not in Inventory");
}
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e);
}
}
public void finalizeAmount(int SKU, int quantity) {
System.out.println("Success");
String sql = "UPDATE Inventory SET Quantity = ? WHERE SKU = ?";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
// set the corresponding param
pstmt.setInt(1, quantity);
pstmt.setInt(2, SKU);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public void addRow(int sku, int quant) {
String sql = "INSERT INTO warehouses(name,capacity) VALUES(?,?)";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, sku);
pstmt.setInt(2, quant);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}