-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdmin_data.java
More file actions
294 lines (257 loc) · 13 KB
/
Copy pathAdmin_data.java
File metadata and controls
294 lines (257 loc) · 13 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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
/**
*
* @author drago
*/
import javax.swing.*;
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.SQLException;
public class Admin_data extends javax.swing.JFrame {
/**
* Creates new form Admin_data
*/
public Admin_data() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
View = new javax.swing.JButton();
back = new javax.swing.JButton();
insert_data = new javax.swing.JButton();
Search_btn = new javax.swing.JButton();
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}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
View.setBackground(new java.awt.Color(102, 153, 255));
View.setFont(new java.awt.Font("Segoe UI", 3, 12)); // NOI18N
View.setForeground(new java.awt.Color(255, 255, 255));
View.setText("View");
View.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ViewActionPerformed(evt);
}
});
back.setBackground(new java.awt.Color(0, 153, 153));
back.setFont(new java.awt.Font("Segoe UI", 3, 12)); // NOI18N
back.setForeground(new java.awt.Color(255, 255, 255));
back.setText("<");
back.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
backActionPerformed(evt);
}
});
insert_data.setBackground(new java.awt.Color(0, 153, 0));
insert_data.setFont(new java.awt.Font("Segoe UI", 3, 12)); // NOI18N
insert_data.setForeground(new java.awt.Color(255, 255, 255));
insert_data.setText("Insert");
insert_data.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
insert_dataActionPerformed(evt);
}
});
Search_btn.setText("Search");
Search_btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Search_btnActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(170, 170, 170)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(back, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(View, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(insert_data, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
.addComponent(Search_btn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(0, 192, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(73, 73, 73)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(View)
.addComponent(insert_data))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(back)
.addComponent(Search_btn))
.addContainerGap(467, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void ViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ViewActionPerformed
// TODO add your handling code here:
admin_viewData avd = new admin_viewData();
avd.setVisible(true);
// Close or hide the registration form
this.dispose();
}//GEN-LAST:event_ViewActionPerformed
private void backActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backActionPerformed
welcome ad = new welcome();
ad.setVisible(true);
// Close or hide the registration form
this.dispose();
}//GEN-LAST:event_backActionPerformed
private void insert_dataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insert_dataActionPerformed
insert_admin_data iad = new insert_admin_data();
iad.setVisible(true);
// Close or hide the registration form
this.dispose(); }//GEN-LAST:event_insert_dataActionPerformed
private void Search_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Search_btnActionPerformed
String searchTerm = JOptionPane.showInputDialog(this, "Enter search term:");
// JDBC connection parameters
String url = "jdbc:mysql://localhost:3306/login_form";
String user = "root";
String pass = "";
try (Connection connection = DriverManager.getConnection(url, user, pass)) {
// SQL query to select data from the admin_table based on the search term
String query = "SELECT * FROM admin_table WHERE name LIKE ? OR email LIKE ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
// Set the parameters in the prepared statement
preparedStatement.setString(1, "%" + searchTerm + "%");
preparedStatement.setString(2, "%" + searchTerm + "%");
try (ResultSet resultSet = preparedStatement.executeQuery()) {
// Process the result set and display the data in a JOptionPane dialog
StringBuilder resultMessage = new StringBuilder("Search Results:\n");
while (resultSet.next()) {
String adminId = resultSet.getString("id");
String adminName = resultSet.getString("name");
String adminEmail = resultSet.getString("email");
// Append the retrieved data to the message
resultMessage.append("Admin ID: ").append(adminId)
.append(", Name: ").append(adminName)
.append(", Email: ").append(adminEmail).append("\n");
}
// Display the message in a JOptionPane dialog
JOptionPane.showMessageDialog(this, resultMessage.toString());
}
}
} catch (SQLException ex) {
// Handle exceptions
ex.printStackTrace();
} }//GEN-LAST:event_Search_btnActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Admin_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Admin_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Admin_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Admin_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Admin_data().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton Search_btn;
private javax.swing.JButton View;
private javax.swing.JButton back;
private javax.swing.JButton insert_data;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration//GEN-END:variables
private void admin_viewData() {
String url = "jdbc:mysql://localhost:3306/login_form";
String user = "root";
String pass = "";
try (Connection connection = DriverManager.getConnection(url, user, pass)) {
// SQL query to select all data from the admin_table
String query = "SELECT * FROM admin_table";
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
try (ResultSet resultSet = preparedStatement.executeQuery()) {
// Process the result set and display the data
while (resultSet.next()) {
String adminId = resultSet.getString("id"); // Assuming you have an "id" column
String adminName = resultSet.getString("name");
String adminEmail = resultSet.getString("email");
// Display or log the retrieved data
System.out.println("Admin ID: " + adminId + ", Name: " + adminName + ", Email: " + adminEmail);
}
}
}
} catch (SQLException ex) {
// Handle exceptions
ex.printStackTrace();
}
}
private void DeleteData() {
// Prompt the user for an admin ID to delete
String adminIdToDelete = JOptionPane.showInputDialog(this, "Enter Admin ID to delete:");
// JDBC connection parameters
String url = "jdbc:mysql://localhost:3306/login_form";
String user = "root";
String pass = "";
try (Connection connection = DriverManager.getConnection(url, user, pass)) {
// SQL query to delete data from the admin_table based on ID
String query = "DELETE FROM admin_table WHERE id = ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, adminIdToDelete);
// Execute the update
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
// Data deleted successfully
JOptionPane.showMessageDialog(this, "Admin data deleted successfully!");
} else {
// Handle deletion failure
JOptionPane.showMessageDialog(this, "Failed to delete admin data. Admin ID not found.");
}
}
} catch (SQLException ex) {
// Handle exceptions
ex.printStackTrace();
}
}
}