-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModifyDatabase.java
344 lines (306 loc) · 13.9 KB
/
ModifyDatabase.java
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
/*
* 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.
*/
package office;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import static javax.swing.JTable.AUTO_RESIZE_OFF;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
/**
*
* @author Chris
*/
public class ModifyDatabase extends JFrame implements ActionListener{
DBConnect dbCon = new DBConnect(); //Connects to the Database
private final int WIDTH = 1000, HEIGHT = 700;
JButton done, cancel, update;
JFrame modFrame = new JFrame("Modify Database");
JScrollPane jsp;
JTable table;
JComboBox comboBox = new JComboBox();
public ModifyDatabase() {
done = new JButton("Done");
cancel = new JButton("Cancel");
update = new JButton("Update");
done.setSize(75,30);
cancel.setSize(75,30);
update.setSize(75,30);
done.addActionListener(this);
cancel.addActionListener(this);
update.addActionListener(this);
modFrame.setLayout(null);
table = loadTable();
table.setAutoResizeMode(AUTO_RESIZE_OFF);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
jsp = new JScrollPane(table,v ,h);
jsp.setSize(985,600);
done.setLocation(350, 625);
cancel.setLocation(576, 625);
update.setLocation(463,625);
jsp.setLocation(5,5);
modFrame.add(jsp);
modFrame.add(done);
modFrame.add(cancel);
modFrame.add(update);
modFrame.setSize(WIDTH, HEIGHT);
modFrame.setLocationRelativeTo(null);
}
public JTable loadTable(){
try{
Connection con = dbCon.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM CHECKOUTS ORDER BY checkoutdate DESC");
// It creates and displays the table
table = new JTable(buildTableModel(rs)){
public boolean isCellEditable(int row, int column) {
if (row >= 0 && (column > 2)) {
return true;
} else {
return false;
}
}
};
// Closes the Connection
rs.close();
con.close();
} catch(Exception e){
System.out.print(e);
e.printStackTrace();
JOptionPane optionPane = new JOptionPane(e.toString());
JDialog dialog = optionPane.createDialog("Error!");
dialog.setAlwaysOnTop(this.isAlwaysOnTopSupported());
dialog.setVisible(true);
}
comboBox.addItem("YES");
comboBox.addItem("NO");
TableColumn column = null;
for (int i = 0; i < 18; i++) {
column = table.getColumnModel().getColumn(i);
if(i == 0){ //Size for Cust_id
column.setMinWidth(60);
column.setMaxWidth(60);
column.setPreferredWidth(60);
} else if(i == 1){ //Size for Check Out Date
column.setMinWidth(120);
column.setMaxWidth(120);
column.setPreferredWidth(120);
} else if(i == 2){ //Size for Staff
column.setMinWidth(60);
column.setMaxWidth(60);
column.setPreferredWidth(60);
} else if(i == 3 || i == 4 || i == 5){ //Size for FName,LName,Anum
column.setMinWidth(100);
column.setMaxWidth(100);
column.setPreferredWidth(100);
} else if(i == 6){ //Size for APT
column.setMinWidth(60);
column.setMaxWidth(60);
column.setPreferredWidth(60);
} else if(i == 7){ //Size for Phone
column.setMinWidth(100);
column.setMaxWidth(100);
column.setPreferredWidth(100);
} else if(i == 8){ //Size for Email
column.setMinWidth(200);
column.setMaxWidth(200);
column.setPreferredWidth(200);
} else if(i == 9){ //Size for Item
column.setMinWidth(80);
column.setMaxWidth(80);
column.setPreferredWidth(80);
} else if(i == 10){ //Size for Description
column.setMinWidth(200);
column.setMaxWidth(200);
column.setPreferredWidth(200);
} else if(i == 11){ //Size for Due Date
column.setMinWidth(80);
column.setMaxWidth(80);
column.setPreferredWidth(80);
} else if(i == 12){ //Size for Returned
column.setMinWidth(80);
column.setMaxWidth(80);
column.setPreferredWidth(80);
} else if(i == 13){ //Size for Checkin date
column.setMinWidth(100);
column.setMaxWidth(100);
column.setPreferredWidth(100);
} else if(i == 14){ //Size for Staff checked in
column.setMinWidth(60);
column.setMaxWidth(60);
column.setPreferredWidth(60);
} else if(i == 15){ //Size for Amount Owed
column.setMinWidth(90);
column.setMaxWidth(90);
column.setPreferredWidth(90);
} else if(i == 16){ //Size for 1st Follow up
column.setMinWidth(200);
column.setMaxWidth(200);
column.setPreferredWidth(200);
} else if(i == 17){ //Size for 2nd Follow up
column.setMinWidth(200);
column.setMaxWidth(200);
column.setPreferredWidth(200);
}
if (i == 12) {
column.setCellEditor(new DefaultCellEditor(comboBox));
}
}
return table;
}
public static DefaultTableModel buildTableModel(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == cancel){
modFrame.dispose();
}
if(ae.getSource() == update){
if(table.getSelectedRow() == -1){
JFrame dummy = new JFrame();
dummy.setVisible(false);
dummy.setLocationRelativeTo(null);
dummy.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dummy, "Please select the row you wish to update!", "ATTENTION! ", JOptionPane.INFORMATION_MESSAGE);
dummy.dispose();
} else {
ExecuteCommand();
}
}
if(ae.getSource() == done){
if(table.getSelectedRow() == -1){
JFrame dummy = new JFrame();
dummy.setVisible(false);
dummy.setLocationRelativeTo(null);
dummy.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dummy, "Please select the row you wish to update!", "ATTENTION! ", JOptionPane.INFORMATION_MESSAGE);
dummy.dispose();
} else {
ExecuteCommand();
modFrame.dispose();
}
}
}
public void ExecuteCommand(){
int selectedRowIndex = table.getSelectedRow();
int selectedColumnIndex = table.getSelectedColumn();
int custID = (int)table.getModel().getValueAt(selectedRowIndex, 0);
String staff = (String)table.getModel().getValueAt(selectedRowIndex, 2);
String fName = (String)table.getModel().getValueAt(selectedRowIndex, 3);
String lName = (String)table.getModel().getValueAt(selectedRowIndex, 4);
String aNum = (String)table.getModel().getValueAt(selectedRowIndex, 5);
String apt = (String)table.getModel().getValueAt(selectedRowIndex, 6);
String phone = (String)table.getModel().getValueAt(selectedRowIndex, 7);
String email = (String)table.getModel().getValueAt(selectedRowIndex, 8);
String item = (String)table.getModel().getValueAt(selectedRowIndex, 9);
String descript = (String)table.getModel().getValueAt(selectedRowIndex, 10);
String dueDate = table.getModel().getValueAt(selectedRowIndex, 11).toString();
String returned = (String)table.getModel().getValueAt(selectedRowIndex, 12);
Object checkInDate = table.getModel().getValueAt(selectedRowIndex, 13);
String staff2 = (String)table.getModel().getValueAt(selectedRowIndex, 14);
Object amountOwe = table.getModel().getValueAt(selectedRowIndex, 15);
String follow1 = (String)table.getModel().getValueAt(selectedRowIndex, 16);
String follow2 = (String)table.getModel().getValueAt(selectedRowIndex, 17);
if(checkInDate != null){
String query = "UPDATE CHECKOUTS SET STAFF=?,FNAME=?,LNAME=?,ANUM=?,APT=?, PHONE=?, EMAIL=?, ITEM=?, DESCRIPTION=?, DUEDATE=?, RETURNED=?, CHECKINDATE=?, STAFF2=?, AMOUNTOWE=?, FOLLOWUP1=?, FOLLOWUP2=? WHERE CUST_ID=?";
try{
Connection con = dbCon.getConnection();
PreparedStatement stmt = con.prepareStatement(query);
stmt.setString(1, staff);
stmt.setString(2, fName);
stmt.setString(3, lName);
stmt.setString(4, aNum);
stmt.setString(5, apt);
stmt.setString(6, phone);
stmt.setString(7, email);
stmt.setString(8, item);
stmt.setString(9, descript);
stmt.setString(10, dueDate);
stmt.setString(11, returned);
stmt.setObject(12, checkInDate);
stmt.setString(13, staff2);
stmt.setObject(14, amountOwe);
stmt.setString(15, follow1);
stmt.setString(16, follow2);
stmt.setInt(17, custID);
stmt.executeUpdate();
con.close();
} catch (Exception e){
System.out.print(e);
e.printStackTrace();
JOptionPane optionPane = new JOptionPane(e.toString());
JDialog dialog = optionPane.createDialog("Error!");
dialog.setAlwaysOnTop(this.isAlwaysOnTopSupported());
dialog.setVisible(true);
}
} else if(checkInDate == null){
String query = "UPDATE CHECKOUTS SET STAFF=?,FNAME=?,LNAME=?,ANUM=?,APT=?, PHONE=?, EMAIL=?, ITEM=?, DESCRIPTION=?, DUEDATE=?, RETURNED=?, CHECKINDATE=?, STAFF2=?, AMOUNTOWE=?, FOLLOWUP1=?, FOLLOWUP2=? WHERE CUST_ID=?";
try{
Connection con = dbCon.getConnection();
PreparedStatement stmt = con.prepareStatement(query);
stmt.setString(1, staff);
stmt.setString(2, fName);
stmt.setString(3, lName);
stmt.setString(4, aNum);
stmt.setString(5, apt);
stmt.setString(6, phone);
stmt.setString(7, email);
stmt.setString(8, item);
stmt.setString(9, descript);
stmt.setString(10, dueDate);
stmt.setString(11, returned);
stmt.setObject(12, null);
stmt.setString(13, staff2);
stmt.setObject(14, amountOwe);
stmt.setString(15, follow1);
stmt.setString(16, follow2);
stmt.setInt(17, custID);
stmt.executeUpdate();
con.close();
} catch (Exception e){
System.out.print(e);
e.printStackTrace();
JOptionPane optionPane = new JOptionPane(e.toString());
JDialog dialog = optionPane.createDialog("Error!");
dialog.setAlwaysOnTop(this.isAlwaysOnTopSupported());
dialog.setVisible(true);
}
}
}
}