-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackendFunctions.java
More file actions
122 lines (109 loc) · 4.58 KB
/
Copy pathBackendFunctions.java
File metadata and controls
122 lines (109 loc) · 4.58 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
117
118
119
120
121
122
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author SoloCode JSHDVR
*/
public class BackendFunctions {
public static String databaseName = "inventory_system";
PreparedStatement ps;
ResultSet rs;
//functon to create to connect to the database
public static Connection getConnection() {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + databaseName, "root", "");
} catch (ClassNotFoundException | SQLException ex) {
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "INVALID INPUT");
}
return con;
}
public ArrayList<Inventory> inventoryList() {
ArrayList<Inventory> inventory = new ArrayList<>();
String sortGradesQuery = "SELECT * FROM `inventory_table`";
Inventory inventoryItem;
try {
ps = getConnection().prepareStatement(sortGradesQuery);
rs = ps.executeQuery();
while (rs.next()) {
inventoryItem = new Inventory(
rs.getString("inventory_id"),
rs.getString("item_name"),
rs.getString("delivery_date"),
rs.getString("expiration_date"),
rs.getString("item_price"),
rs.getString("item_quantity"),
rs.getString("inventory_person"));
inventory.add(inventoryItem);
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
}
return inventory;
}
public void addItem(String item_name, String delivery_date, String expiration_date, String item_price, String item_quantity, String inventory_person) {
String Addquery = "INSERT INTO `inventory_table` (`item_name`, `delivery_date`, `expiration_date`, `item_price`, `item_quantity`, `inventory_person`) VALUES (?,?,?,?,?,?);";
try {
ps = getConnection().prepareStatement(Addquery);
ps.setString(1, item_name);
ps.setString(2, delivery_date);
ps.setString(3, expiration_date);
ps.setString(4, item_price);
ps.setString(5, item_quantity);
ps.setString(6, inventory_person);
if (ps.executeUpdate() > 0) {
JOptionPane.showMessageDialog(null, "New item: " + item_name);
} else {
JOptionPane.showMessageDialog(null, "Adding item error");
}
} catch (SQLException ex) {
System.out.println(ex);
System.out.println("shits");
JOptionPane.showMessageDialog(null, "SQLException");
}
}
public void updateItem(String item_name, String delivery_date, String expiration_date, String item_price, String item_quantity, String inventory_person, String inventory_id) {
String UpdateQuery = "UPDATE `inventory_table` SET `item_name`=?,`delivery_date`=?,`expiration_date`=?,`item_price`=?,`item_quantity`=?,`inventory_person`=? WHERE `inventory_id`= ?;";
try {
ps = getConnection().prepareStatement(UpdateQuery);
ps.setString(1, item_name);
ps.setString(2, delivery_date);
ps.setString(3, expiration_date);
ps.setString(4, item_price);
ps.setString(5, item_quantity);
ps.setString(6, inventory_person);
ps.setString(7, inventory_id);
if (ps.executeUpdate() > 0) {
JOptionPane.showMessageDialog(null, "UPDATED");
} else {
JOptionPane.showMessageDialog(null, "UPDATED ERROR");
}
} catch (SQLException ex) {
System.out.println(ex);
}
}
public void deleteItem(String inventory_id) {
String deleteQuery = "DELETE FROM `inventory_table` WHERE `inventory_id` =" + inventory_id + ";";
try {
ps = getConnection().prepareStatement(deleteQuery);
if (ps.executeUpdate() > 0) {
JOptionPane.showMessageDialog(null, "DELETED");
}
} catch (SQLException ex) {
System.out.println(ex);
}
}
}
//end of code