-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArchive.java
More file actions
592 lines (505 loc) · 27 KB
/
Copy pathArchive.java
File metadata and controls
592 lines (505 loc) · 27 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
package POSmain;
import DBMySQL.ConnectionProvider;
import POSmain.Employee.ImageRenderer;
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
public class Archive extends javax.swing.JPanel {
private Maintenance i;
private Home home;
public void saveProduct(String imagePath, String name, double price, int stock, String description) throws FileNotFoundException {
try (Connection con = ConnectionProvider.getCon()) {
String sql = "INSERT INTO products (image_path, name, price, stock, description) VALUES (?, ?, ?, ?, ?, ?)";
try (PreparedStatement pst = con.prepareStatement(sql)) {
FileInputStream imageFileStream = new FileInputStream(imagePath);
pst.setBinaryStream(1, imageFileStream, (int) new File(imagePath).length());
pst.setString(2, name);
pst.setDouble(3, price);
pst.setInt(4, stock);
pst.setString(5, description);
pst.executeUpdate();
}
} catch (SQLException | IOException e) {
e.printStackTrace();
}
}
private void restoreEmployee() {
int selectedRow = jTable1.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(this, "Please select an archived employee to restore.", "No Selection", JOptionPane.WARNING_MESSAGE);
return;
}
String id = jTable1.getValueAt(selectedRow, 6).toString();
try (Connection con = ConnectionProvider.getCon()) {
String selectSQL = "SELECT * FROM createacc_archive WHERE ID = ?";
PreparedStatement selectStmt = con.prepareStatement(selectSQL);
selectStmt.setString(1, id);
ResultSet rs = selectStmt.executeQuery();
if (rs.next()) {
String insertSQL = "INSERT INTO createacc (ID, pic, lastname, firstname, middlename, email, address, type, password) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement insertStmt = con.prepareStatement(insertSQL);
insertStmt.setString(1, rs.getString("ID"));
insertStmt.setBytes(2, rs.getBytes("pic"));
insertStmt.setString(3, rs.getString("lastname"));
insertStmt.setString(4, rs.getString("firstname"));
insertStmt.setString(5, rs.getString("middlename"));
insertStmt.setString(6, rs.getString("email"));
insertStmt.setString(7, rs.getString("address"));
insertStmt.setString(8, rs.getString("type"));
insertStmt.setString(9, rs.getString("password"));
int inserted = insertStmt.executeUpdate();
if (inserted > 0) {
String deleteSQL = "DELETE FROM createacc_archive WHERE ID = ?";
PreparedStatement deleteStmt = con.prepareStatement(deleteSQL);
deleteStmt.setString(1, id);
deleteStmt.executeUpdate();
JOptionPane.showMessageDialog(this, "Employee restored successfully.");
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.setRowCount(0);
loadArchivedData(jTable1);
jTable1.clearSelection();
} else {
JOptionPane.showMessageDialog(this, "Failed to restore employee.", "Restore Error", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(this, "Employee not found in archive.", "Not Found", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Error during restore: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
public static void loadArchivedData(JTable table) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
for (int i = 0; i < model.getColumnCount(); i++) {
table.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
}
int imageSize = 100;
int padding = 5;
try {
Connection con = ConnectionProvider.getCon();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT ID, pic, lastname, firstname, middlename, email, address, type, password, archived_at FROM createacc_archive");
while (rs.next()) {
byte[] imgData = rs.getBytes("pic");
ImageIcon icon = null;
if (imgData != null) {
ImageIcon originalIcon = new ImageIcon(imgData);
Image img = originalIcon.getImage().getScaledInstance(imageSize, imageSize, Image.SCALE_SMOOTH);
icon = new ImageIcon(img);
}
String lastname = rs.getString("lastname");
String firstname = rs.getString("firstname");
String middlename = rs.getString("middlename");
String fullName = lastname + ", " + firstname + " " + (middlename != null ? middlename : "");
String email = rs.getString("email");
String address = rs.getString("address");
String type = rs.getString("type");
String archived = rs.getString("archived_at");
String id = rs.getString("ID");
model.addRow(new Object[]{icon, fullName, email, address, type, archived, id});
}
table.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer());
table.setRowHeight(imageSize + padding);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Error loading archive: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
public static void loadProductArchiveData(JTable table) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
for (int i = 0; i < model.getColumnCount(); i++) {
table.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
}
try (Connection con = ConnectionProvider.getCon();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM prodarchive")) {
while (rs.next()) {
byte[] imgData = rs.getBytes("Product");
ImageIcon icon = null;
if (imgData != null) {
ImageIcon originalIcon = new ImageIcon(imgData);
Image img = originalIcon.getImage().getScaledInstance(100, 100, Image.SCALE_SMOOTH);
icon = new ImageIcon(img);
}
Object[] row = {
icon,
rs.getString("Name"),
rs.getDouble("Price"),
rs.getInt("Stocks"),
rs.getString("Description"),
rs.getString("category"),
rs.getString("Brand"),
rs.getString("archived_at"),
rs.getDouble("cost"),
rs.getInt("ID")
};
model.addRow(row);
}
table.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer());
table.setRowHeight(105);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Error loading product archive: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
public Archive(Maintenance i, Home home) {
this.i = i;
this.home = home;
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jButton6 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jPanel4 = new javax.swing.JPanel();
jLabel4 = new javax.swing.JLabel();
jButton5 = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
jTable2 = new javax.swing.JTable();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
setBackground(new java.awt.Color(204, 204, 255));
setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel1.setLayout(new java.awt.BorderLayout());
add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 6, -1, 668));
jLabel1.setFont(new java.awt.Font("Segoe UI Semibold", 0, 18)); // NOI18N
jLabel1.setText("Archive Employee");
jButton6.setBackground(new java.awt.Color(204, 102, 0));
jButton6.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jButton6.setText("Employee");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jTable1.setBackground(new java.awt.Color(153, 153, 153));
jTable1.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jTable1.setForeground(new java.awt.Color(0, 0, 0));
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null}
},
new String [] {
"Employee", "Name", "Email", "Address", "Type", "Archived", "ID"
}
) {
Class[] types = new Class [] {
java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.Integer.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false, false
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTable1.setShowGrid(true);
jTable1.setShowVerticalLines(false);
jScrollPane1.setViewportView(jTable1);
jButton7.setBackground(new java.awt.Color(204, 102, 0));
jButton7.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jButton7.setText("Recover");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jButton8.setBackground(new java.awt.Color(204, 102, 0));
jButton8.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jButton8.setText("Delete");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(16, 16, 16)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 483, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(19, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(jLabel1)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(42, 42, 42)
.addComponent(jButton6)
.addGap(36, 36, 36)
.addComponent(jButton7)
.addGap(36, 36, 36)
.addComponent(jButton8))
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(15, 15, 15)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 546, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(21, Short.MAX_VALUE))
);
add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(650, 20, 620, 640));
jLabel4.setFont(new java.awt.Font("Segoe UI Semibold", 0, 18)); // NOI18N
jLabel4.setText("Archive Product");
jButton5.setBackground(new java.awt.Color(204, 102, 0));
jButton5.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jButton5.setText("Product");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jTable2.setBackground(new java.awt.Color(153, 153, 153));
jTable2.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jTable2.setForeground(new java.awt.Color(0, 0, 0));
jTable2.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null}
},
new String [] {
"Product", "Name", "Price", "Stocks", "Desc", "Category", "Brand", "Archived", "Cost", "ID"
}
) {
Class[] types = new Class [] {
java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.Double.class, java.lang.Integer.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false, false, false, false, false
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTable2.setShowGrid(true);
jTable2.setShowVerticalLines(false);
jScrollPane2.setViewportView(jTable2);
jButton9.setBackground(new java.awt.Color(204, 102, 0));
jButton9.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jButton9.setText("Recover");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
jButton10.setBackground(new java.awt.Color(204, 102, 0));
jButton10.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jButton10.setText("Delete");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(jLabel4)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(jPanel4Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 1, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(jLabel4)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addGap(42, 42, 42)
.addComponent(jButton5)
.addGap(36, 36, 36)
.addComponent(jButton9)
.addGap(36, 36, 36)
.addComponent(jButton10))
.addGroup(jPanel4Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 550, javax.swing.GroupLayout.PREFERRED_SIZE))))
);
add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 630, 640));
}// </editor-fold>//GEN-END:initComponents
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
loadArchivedData(jTable1);
}//GEN-LAST:event_jButton6ActionPerformed
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
restoreEmployee();
}//GEN-LAST:event_jButton7ActionPerformed
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
int selectedRow = jTable1.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(this, "Please select an employee to delete.", "No Selection", JOptionPane.WARNING_MESSAGE);
return;
}
String id = jTable1.getValueAt(selectedRow, 5).toString();
int confirmation = JOptionPane.showConfirmDialog(this,
"Are you sure you want to permanently delete this employee?",
"Confirm Deletion",
JOptionPane.YES_NO_OPTION);
if (confirmation == JOptionPane.YES_OPTION) {
try (Connection con = ConnectionProvider.getCon()) {
String deleteSQL = "DELETE FROM createacc_archive WHERE ID = ?";
PreparedStatement deleteStmt = con.prepareStatement(deleteSQL);
deleteStmt.setString(1, id);
int deleted = deleteStmt.executeUpdate();
if (deleted > 0) {
JOptionPane.showMessageDialog(this, "Employee permanently deleted.");
loadArchivedData(jTable1);
jTable1.clearSelection();
} else {
JOptionPane.showMessageDialog(this, "Failed to delete employee.", "Delete Error", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Error during deletion: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}//GEN-LAST:event_jButton8ActionPerformed
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
int selectedRow = jTable2.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(this, "Please select a product to restore.", "No Selection", JOptionPane.WARNING_MESSAGE);
return;
}
String id = jTable2.getValueAt(selectedRow, 9).toString();
try (Connection con = ConnectionProvider.getCon()) {
String selectSQL = "SELECT * FROM prodarchive WHERE ID = ?";
PreparedStatement selectStmt = con.prepareStatement(selectSQL);
selectStmt.setString(1, id);
ResultSet rs = selectStmt.executeQuery();
if (rs.next()) {
String insertSQL = "INSERT INTO product (ID, Product, Name, Price, Description, Stocks, category, Brand, cost) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement insertStmt = con.prepareStatement(insertSQL);
insertStmt.setInt(1, rs.getInt("ID"));
insertStmt.setBytes(2, rs.getBytes("Product"));
insertStmt.setString(3, rs.getString("Name"));
insertStmt.setDouble(4, rs.getDouble("Price"));
insertStmt.setString(5, rs.getString("Description"));
insertStmt.setInt(6, rs.getInt("Stocks"));
insertStmt.setString(7, rs.getString("category"));
insertStmt.setString(8, rs.getString("Brand"));
insertStmt.setDouble(9, rs.getDouble("cost"));
int inserted = insertStmt.executeUpdate();
if (inserted > 0) {
String deleteSQL = "DELETE FROM prodarchive WHERE ID = ?";
PreparedStatement deleteStmt = con.prepareStatement(deleteSQL);
deleteStmt.setString(1, id);
deleteStmt.executeUpdate();
JOptionPane.showMessageDialog(this, "Product restored successfully.");
loadProductArchiveData(jTable2);
jTable2.clearSelection();
} else {
JOptionPane.showMessageDialog(this, "Failed to restore product.", "Restore Error", JOptionPane.ERROR_MESSAGE);
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Error during restore: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_jButton9ActionPerformed
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
int selectedRow = jTable2.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(this, "Please select a product to delete.", "No Selection", JOptionPane.WARNING_MESSAGE);
return;
}
String id = jTable2.getValueAt(selectedRow, 9).toString();
int confirm = JOptionPane.showConfirmDialog(this,
"Are you sure you want to permanently delete this product?",
"Confirm Deletion",
JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
try (Connection con = ConnectionProvider.getCon()) {
String deleteSQL = "DELETE FROM prodarchive WHERE ID = ?";
PreparedStatement deleteStmt = con.prepareStatement(deleteSQL);
deleteStmt.setString(1, id);
int deleted = deleteStmt.executeUpdate();
if (deleted > 0) {
JOptionPane.showMessageDialog(this, "Product permanently deleted.");
loadProductArchiveData(jTable2);
jTable2.clearSelection();
} else {
JOptionPane.showMessageDialog(this, "Failed to delete product.", "Delete Error", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Error during deletion: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}//GEN-LAST:event_jButton10ActionPerformed
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
loadProductArchiveData(jTable2);
}//GEN-LAST:event_jButton5ActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable jTable1;
private javax.swing.JTable jTable2;
// End of variables declaration//GEN-END:variables
}