-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInventory.java
More file actions
210 lines (177 loc) · 7.47 KB
/
Inventory.java
File metadata and controls
210 lines (177 loc) · 7.47 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package hospital_management;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
public class Inventory extends JFrame {
private JLabel lab = new JLabel("Prescription ID ");
private JTextField tf = new JTextField();
private JLabel lab1 = new JLabel("Drug Code ");
private JTextField tf1 = new JTextField();
private JLabel lab2 = new JLabel("Drug Name ");
private JTextField tf2 = new JTextField();
private JLabel lab3 = new JLabel("Quantity ");
private JTextField tf3 = new JTextField();
private JButton add = new JButton("ADD");
private JLabel lab4 = new JLabel("Total Cost ");
private JTextField tf4 = new JTextField();
private JLabel lab5 = new JLabel("Pay ");
private JTextField tf5 = new JTextField();
private JTable table = new JTable();
private JButton create = new JButton("Create");
private JButton cancel = new JButton("Cancel");
private JButton close = new JButton("Close");
private JPanel pan = new JPanel();
private JPanel pan2 = new JPanel();
private JLabel labLL = new JLabel("HOSPITAL MANAGEMENT SYSTEM ");
private JPanel pan1 = new JPanel(new BorderLayout());
Inventory(String pid) {
table1();
tf5.setEditable(false);
tf.setText(pid);
pan2.add(labLL);
pan.add(lab);
pan.add(tf);
pan.add(lab1);
pan.add(tf1);
pan.add(lab2);
pan.add(tf2);
pan.add(lab3);
pan.add(tf3);
pan.add(lab4);
pan.add(tf4);
pan.add(lab5);
pan.add(tf5);
pan.add(create);
pan.add(cancel);
pan.add(close);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
int selectedRow = table.getSelectedRow();
if (selectedRow != -1) { // If a row is selected
DefaultTableModel model = (DefaultTableModel) table.getModel();
tf.setText(model.getValueAt(selectedRow, 0).toString());
tf1.setText(model.getValueAt(selectedRow, 1).toString());
tf2.setText(model.getValueAt(selectedRow, 2).toString());
tf3.setText(model.getValueAt(selectedRow, 3).toString());
tf4.setText(model.getValueAt(selectedRow, 4).toString());
tf5.setText(model.getValueAt(selectedRow, 5).toString());
}
}
});
pan.setLayout(new GridLayout(9, 2, 5, 6));
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tf.setText("");
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
}
});
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new View_Prescription();
setVisible(false);
}
});
create.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double d;
int n;
Connection con = null;
Statement stmt = null;
table1();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/java_project", "root", "1234");
stmt = con.createStatement();
PreparedStatement pst = con.prepareStatement("SELECT * FROM item WHERE itemid = ?");
pst.setString(1, tf1.getText());
ResultSet rs = pst.executeQuery();
rs.next();
n = Integer.parseInt(tf3.getText());
d = Double.parseDouble(rs.getString(4)) * n;
String insert = "INSERT INTO inventory VALUES('" + tf.getText() + "','" + tf1.getText() + "','"
+ tf2.getText() + "','" + tf3.getText() + "','" + d + "','" + tf5.getText() + "')";
stmt.executeUpdate(insert);
JOptionPane.showMessageDialog(null, "CREATED SUCCESSFULLY!");
tf.setText("");
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
} catch (ClassNotFoundException ex) {
Logger.getLogger(CreateUser.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(CreateUser.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
Logger.getLogger(CreateUser.class.getName()).log(Level.SEVERE, null, ex);
}
}
table1();
}
});
JScrollPane scrollPane = new JScrollPane(table);
pan1.add(pan, BorderLayout.WEST);
pan1.add(scrollPane, BorderLayout.CENTER);
pan1.add(pan2,BorderLayout.PAGE_START);
add(pan1);
setTitle("Inventory");
setSize(650, 350);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void table1() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/java_project", "root", "1234");
PreparedStatement pst = con.prepareStatement("SELECT * FROM inventory");
ResultSet rs = pst.executeQuery();
DefaultTableModel model = new DefaultTableModel();
model.addColumn("pres_id");
model.addColumn("drug_code");
model.addColumn("drug_name");
model.addColumn("quantity");
model.addColumn("total_cost");
model.addColumn("pay status");
while (rs.next()) {
Object[] rowData = new Object[6];
rowData[0] = rs.getString("pres_id");
rowData[1] = rs.getString("drug_code");
rowData[2] = rs.getString("drug_name");
rowData[3] = rs.getString("quantity");
rowData[4] = rs.getString("total_cost");
rowData[5] = rs.getString("pay");
model.addRow(rowData);
}
table.setModel(model);
rs.close();
pst.close();
con.close();
} catch (ClassNotFoundException | SQLException ex) {
ex.printStackTrace();
}
}
}