-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBApp.java
More file actions
444 lines (439 loc) · 17.7 KB
/
DBApp.java
File metadata and controls
444 lines (439 loc) · 17.7 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
import java.awt.*;
import java.sql.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class DBApp
{
static Connection conn;
public static void connectDB()
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521/XEPDB1",
"DBSLAB",
"7116"
);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
public static void showLogin()
{
JFrame frame = new JFrame("Login");
frame.setSize(450, 250);
frame.setLayout(new GridLayout(3, 2, 10, 10));
JTextField userField = new JTextField();
JPasswordField passField = new JPasswordField();
JButton loginBtn = new JButton("Login");
frame.add(new JLabel("Username:"));
frame.add(userField);
frame.add(new JLabel("Password:"));
frame.add(passField);
frame.add(new JLabel(""));
frame.add(loginBtn);
loginBtn.addActionListener(e ->
{
if (userField.getText().equals("admin") && new String(passField.getPassword()).equals("12345"))
{
frame.dispose();
showAdmin();
}
else JOptionPane.showMessageDialog(frame, "Invalid Login");
});
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void showAdmin()
{
JFrame frame = new JFrame("Admin Panel");
frame.setSize(550, 500);
frame.setLayout(new GridLayout(4, 2, 10, 10));
JButton btn1 = new JButton("View All Table Names");
JButton btn2 = new JButton("View Table Info");
JButton btn3 = new JButton("View Table Data");
JButton btn4 = new JButton("Insert Into Table");
JButton btn5 = new JButton("Delete From Table");
JButton btn6 = new JButton("Update Table");
JButton btn7 = new JButton("View Player Rewards (Join)");
JButton btn8 = new JButton("Back to Login");
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
frame.add(btn7);
frame.add(btn8);
btn1.addActionListener(e -> viewTables());
btn2.addActionListener(e -> viewTableInfo());
btn3.addActionListener(e -> viewTableData());
btn4.addActionListener(e -> insertData());
btn5.addActionListener(e -> deleteMenu());
btn6.addActionListener(e -> updateData());
btn7.addActionListener(e -> viewJoinData());
btn8.addActionListener(e -> { frame.dispose(); showLogin(); });
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static String[] getTables()
{
try
{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT table_name FROM user_tables");
Vector<String> list = new Vector<>();
while (rs.next()) list.add(rs.getString(1));
return list.toArray(new String[0]);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return new String[0];
}
public static void viewTables()
{
JTextArea area = new JTextArea(String.join("\n", getTables()));
area.setFont(new Font("Monospaced", Font.PLAIN, 16));
JScrollPane scroll = new JScrollPane(area);
scroll.setPreferredSize(new Dimension(600, 400));
JOptionPane.showMessageDialog(null, scroll);
}
public static void viewTableInfo()
{
String table = (String) JOptionPane.showInputDialog(null, "Select Table",
"Tables", JOptionPane.QUESTION_MESSAGE, null, getTables(), null);
if (table == null) return;
try
{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT column_name, data_type, nullable FROM user_tab_columns WHERE table_name='" + table + "' ORDER BY column_id");
StringBuilder sb = new StringBuilder();
while (rs.next())
{
sb.append(rs.getString(1)).append("\t")
.append(rs.getString(2)).append("\t")
.append(rs.getString(3)).append("\n");
}
JTextArea area = new JTextArea(sb.toString());
area.setFont(new Font("Monospaced", Font.PLAIN, 16));
JScrollPane scroll = new JScrollPane(area);
scroll.setPreferredSize(new Dimension(700, 500));
JOptionPane.showMessageDialog(null, scroll);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
public static void viewTableData()
{
String table = (String) JOptionPane.showInputDialog(null, "Select Table",
"Tables", JOptionPane.QUESTION_MESSAGE, null, getTables(), null);
if (table == null) return;
try
{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM " + table);
ResultSetMetaData md = rs.getMetaData();
int cols = md.getColumnCount();
DefaultTableModel model = new DefaultTableModel();
for (int i = 1; i <= cols; i++)
model.addColumn(md.getColumnName(i));
while (rs.next())
{
Vector<String> row = new Vector<>();
for (int i = 1; i <= cols; i++) row.add(rs.getString(i));
model.addRow(row);
}
JTable tableUI = new JTable(model);
tableUI.setFont(new Font("Monospaced", Font.PLAIN, 16));
tableUI.setRowHeight(28);
JFrame f = new JFrame("Data: " + table);
f.add(new JScrollPane(tableUI));
f.setSize(1100, 600); // 🔥 BIG
f.setLocationRelativeTo(null);
f.setVisible(true);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
public static void insertData()
{
String table = (String) JOptionPane.showInputDialog(null, "Select Table",
"Insert", JOptionPane.QUESTION_MESSAGE, null, getTables(), null);
if (table == null) return;
try
{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT column_name, data_type FROM user_tab_columns WHERE table_name='" + table + "' ORDER BY column_id");
Vector<String> cols = new Vector<>();
Vector<String> types = new Vector<>();
while (rs.next())
{
cols.add(rs.getString(1));
types.add(rs.getString(2));
}
JPanel panel = new JPanel(new GridLayout(cols.size(), 2, 10, 10));
JTextField[] fields = new JTextField[cols.size()];
for (int i = 0; i < cols.size(); i++)
{
String example = "";
if (types.get(i).equals("NUMBER")) example = "e.g., 101";
else if (types.get(i).equals("VARCHAR2")) example = "e.g., John";
else if (types.get(i).equals("DATE")) example = "YYYY-MM-DD (e.g., 2000-07-02)";
JLabel label = new JLabel(cols.get(i) + " (" + types.get(i) + " - " + example + ")");
fields[i] = new JTextField();
panel.add(label);
panel.add(fields[i]);
}
JScrollPane scroll = new JScrollPane(panel);
scroll.setPreferredSize(new Dimension(800, 500));
int result = JOptionPane.showConfirmDialog(null, scroll, "Insert Data", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION)
{
StringBuilder q = new StringBuilder("INSERT INTO " + table + " VALUES (");
for (int i = 0; i < cols.size(); i++)
{
q.append("?");
if (i < cols.size() - 1) q.append(",");
}
q.append(")");
PreparedStatement ps = conn.prepareStatement(q.toString());
for (int i = 0; i < cols.size(); i++)
if (types.get(i).equals("DATE")) ps.setDate(i + 1, Date.valueOf(fields[i].getText()));
else ps.setString(i + 1, fields[i].getText());
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Data inserted successfully!");
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
public static void deleteRow()
{
String table = (String) JOptionPane.showInputDialog(null, "Select Table",
"Delete Row", JOptionPane.QUESTION_MESSAGE, null, getTables(), null);
if (table == null) return;
String pk = getPrimaryKey(table);
while (true)
{
try
{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT " + pk + " FROM " + table);
Vector<String> ids = new Vector<>();
while (rs.next()) ids.add(rs.getString(1));
if (ids.size() == 0)
{
JOptionPane.showMessageDialog(null, "No data available to delete");
return;
}
String selectedID = (String) JOptionPane.showInputDialog(
null,
"Select " + pk + " to delete",
"Delete Row",
JOptionPane.QUESTION_MESSAGE,
null,
ids.toArray(),
null
);
if (selectedID == null) return;
int confirm = JOptionPane.showConfirmDialog(null,
"Delete row where " + pk + " = " + selectedID + " ?",
"Confirm",
JOptionPane.YES_NO_OPTION);
if (confirm != JOptionPane.YES_OPTION) return;
st.executeUpdate("DELETE FROM " + table + " WHERE " + pk + "=" + selectedID);
JOptionPane.showMessageDialog(null, "Row deleted successfully!");
return; // success → exit loop
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Invalid input, try again!");
System.out.println(e.getMessage());
}
}
}
public static void deleteMenu()
{
String[] options = {"Delete Table", "Delete Row"};
int choice = JOptionPane.showOptionDialog(null, "Choose Option", "Delete",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, options, options[0]);
if (choice == 0) deleteTable();
else if (choice == 1) deleteRow();
}
public static void deleteTable()
{
String table = (String) JOptionPane.showInputDialog(null, "Select Table",
"Delete", JOptionPane.QUESTION_MESSAGE, null, getTables(), null);
if (table == null) return;
int confirm = JOptionPane.showConfirmDialog(null,
"Are you sure you want to delete table " + table + " ?", "Confirm", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION)
{
try
{
Statement st = conn.createStatement();
st.executeUpdate("DROP TABLE " + table);
JOptionPane.showMessageDialog(null, "Table dropped successfully!");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
public static String getPrimaryKey(String table)
{
try
{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(
"SELECT cols.column_name FROM user_constraints cons, user_cons_columns cols " +
"WHERE cons.constraint_type='P' AND cons.constraint_name=cols.constraint_name AND cols.table_name='" + table + "'"
);
if (rs.next()) return rs.getString(1);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return null;
}
public static void updateData()
{
String table = (String) JOptionPane.showInputDialog(null,
"Select Table", "Update",
JOptionPane.QUESTION_MESSAGE, null,
getTables(), null);
if (table == null) return;
String pk = getPrimaryKey(table);
try
{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT " + pk + " FROM " + table);
Vector<String> ids = new Vector<>();
while (rs.next()) ids.add(rs.getString(1));
String selectedID = (String) JOptionPane.showInputDialog(
null,
"Select " + pk + " (e.g., " + (ids.size()>0?ids.get(0):"1") + ")",
"Select Row",
JOptionPane.QUESTION_MESSAGE,
null,
ids.toArray(),
null
);
if (selectedID == null) return;
rs = st.executeQuery("SELECT * FROM " + table + " WHERE " + pk + "=" + selectedID);
ResultSetMetaData md = rs.getMetaData();
int cols = md.getColumnCount();
rs.next();
JPanel panel = new JPanel(new GridLayout(cols, 2, 10, 10));
JTextField[] fields = new JTextField[cols];
Vector<String> colNames = new Vector<>();
Vector<String> types = new Vector<>();
for (int i = 1; i <= cols; i++)
{
colNames.add(md.getColumnName(i));
types.add(md.getColumnTypeName(i));
String example = "";
if (types.get(i-1).equals("NUMBER")) example = "e.g., 101";
else if (types.get(i-1).contains("CHAR")) example = "e.g., text";
else if (types.get(i-1).equals("DATE")) example = "YYYY-MM-DD (e.g., 2000-07-02)";
panel.add(new JLabel(colNames.get(i-1) + " (" + example + ")"));
JTextField tf = new JTextField(rs.getString(i));
fields[i-1] = tf;
panel.add(tf);
}
JScrollPane scroll = new JScrollPane(panel);
scroll.setPreferredSize(new Dimension(900, 600));
int result = JOptionPane.showConfirmDialog(null, scroll, "Update Row", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION)
{
for (JTextField f : fields)
if (f.getText().trim().isEmpty())
{
JOptionPane.showMessageDialog(null, "Fill all the fields");
return;
}
StringBuilder q = new StringBuilder("UPDATE " + table + " SET ");
for (int i = 0; i < cols; i++)
{
q.append(colNames.get(i)).append("=?");
if (i < cols - 1) q.append(",");
}
q.append(" WHERE ").append(pk).append("=?");
PreparedStatement ps = conn.prepareStatement(q.toString());
try
{
for (int i = 0; i < cols; i++)
if (types.get(i).equals("DATE"))
ps.setDate(i+1, Date.valueOf(fields[i].getText()));
else
ps.setString(i+1, fields[i].getText());
ps.setString(cols+1, selectedID);
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Update successful");
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, "Invalid data type entered!");
System.out.println(ex.getMessage());
}
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
public static void viewJoinData()
{
try
{
Statement st = conn.createStatement();
String query = "SELECT p.PLAYER_ID, p.PLAYER_NAME, r.REWARD_ID, r.REWARDED_TIME, r.LOGIN_TIME " +
"FROM PLAYERS p JOIN REWARD r ON p.PLAYER_ID = r.PLAYER_ID";
ResultSet rs = st.executeQuery(query);
ResultSetMetaData md = rs.getMetaData();
int cols = md.getColumnCount();
DefaultTableModel model = new DefaultTableModel();
for (int i = 1; i <= cols; i++) model.addColumn(md.getColumnName(i));
while (rs.next())
{
Vector<String> row = new Vector<>();
for (int i = 1; i <= cols; i++) row.add(rs.getString(i));
model.addRow(row);
}
JTable tableUI = new JTable(model);
tableUI.setFont(new Font("Monospaced", Font.PLAIN, 16));
tableUI.setRowHeight(28);
JFrame f = new JFrame("Player - Reward Join Data");
f.add(new JScrollPane(tableUI));
f.setSize(1000, 600);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
catch (Exception e)
{
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, "Error fetching join data");
}
}
public static void main(String[] args)
{
connectDB();
showLogin();
}
}