-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregg.java
More file actions
454 lines (397 loc) · 22.5 KB
/
Copy pathregg.java
File metadata and controls
454 lines (397 loc) · 22.5 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
/*
* 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 java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class regg extends javax.swing.JFrame {
/**
* Creates new form regg
*/
public regg() {
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() {
in_lname = new javax.swing.JTextField();
in_username = new javax.swing.JTextField();
in_email = new javax.swing.JTextField();
Fname = new javax.swing.JLabel();
in_passsword = new javax.swing.JPasswordField();
Lname = new javax.swing.JLabel();
in_age = new javax.swing.JTextField();
username = new javax.swing.JLabel();
re_password = new javax.swing.JLabel();
email = new javax.swing.JLabel();
in_repassword = new javax.swing.JPasswordField();
password = new javax.swing.JLabel();
in_address = new javax.swing.JTextField();
age = new javax.swing.JLabel();
submit = new javax.swing.JButton();
address = new javax.swing.JLabel();
in_fname = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
gender = new javax.swing.JLabel();
in_gender = new javax.swing.JComboBox<>();
signin = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(248, 197, 197));
setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
setIconImages(null);
in_lname.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
in_lnameActionPerformed(evt);
}
});
in_username.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
in_usernameActionPerformed(evt);
}
});
in_email.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
in_emailActionPerformed(evt);
}
});
Fname.setText("First Name");
Fname.setToolTipText("");
in_passsword.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
in_passswordActionPerformed(evt);
}
});
Lname.setText("Last Name");
in_age.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
in_ageActionPerformed(evt);
}
});
username.setText("Username");
re_password.setText("re-Password");
email.setText("Email :");
in_repassword.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
in_repasswordActionPerformed(evt);
}
});
password.setText("Password");
in_address.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
in_addressActionPerformed(evt);
}
});
age.setText("Age");
submit.setBackground(new java.awt.Color(51, 153, 255));
submit.setForeground(new java.awt.Color(255, 255, 255));
submit.setText("Submit");
submit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
submitActionPerformed(evt);
}
});
address.setText("Address");
in_fname.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
in_fnameActionPerformed(evt);
}
});
jLabel1.setText("Already have an accccount?");
gender.setText("Gender");
in_gender.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Male", "Female", "Custom" }));
signin.setForeground(new java.awt.Color(0, 153, 255));
signin.setText("Sign in");
signin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
signinActionPerformed(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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(password)
.addComponent(re_password)
.addComponent(email)
.addComponent(username)
.addComponent(age)
.addComponent(address)
.addComponent(gender)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(Fname)
.addComponent(Lname))
.addGap(12, 12, 12)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(submit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(in_address)
.addComponent(in_gender, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(in_lname, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(in_fname, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(in_username, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(in_email, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(in_passsword, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(in_repassword, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(in_age, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(16, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(98, 98, 98))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(signin)
.addGap(39, 39, 39))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2)
.addGap(9, 9, 9)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(re_password)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(in_fname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Fname))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(in_lname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Lname))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(in_username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(username))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(in_email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(email))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(in_passsword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(password))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(in_repassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(age)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(address)
.addComponent(in_address, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(gender)
.addComponent(in_gender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(in_age, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(submit, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(signin))
.addGap(254, 254, 254))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void in_lnameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_in_lnameActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_in_lnameActionPerformed
private void in_usernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_in_usernameActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_in_usernameActionPerformed
private void in_emailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_in_emailActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_in_emailActionPerformed
private void in_passswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_in_passswordActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_in_passswordActionPerformed
private void in_ageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_in_ageActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_in_ageActionPerformed
private void in_repasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_in_repasswordActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_in_repasswordActionPerformed
private void in_addressActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_in_addressActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_in_addressActionPerformed
private void submitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitActionPerformed
String firstName = in_fname.getText();
String lastName = in_lname.getText();
String username = in_username.getText();
String email = in_email.getText();
String password = new String(in_passsword.getPassword());
String rePassword = new String(in_repassword.getPassword());
String age = in_age.getText();
String address = in_address.getText();
String gender = (String) in_gender.getSelectedItem();
// Validate required fields
if (firstName.isEmpty() || lastName.isEmpty() || username.isEmpty() || email.isEmpty() ||
password.isEmpty() || rePassword.isEmpty() || age.isEmpty() || address.isEmpty()) {
JOptionPane.showMessageDialog(this, "All fields must be filled out.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
// Validate password and rePassword match
if (!password.equals(rePassword)) {
JOptionPane.showMessageDialog(this, "Passwords do not match.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
// Validate age as a positive integer
try {
int ageValue = Integer.parseInt(age);
if (ageValue <= 0) {
JOptionPane.showMessageDialog(this, "Age must be a positive integer.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Age must be a valid number.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
// JDBC connection parameters
String url = "jdbc:mysql://localhost:3306/login_form";
String user = "root";
String pass = "";
try {
// Explicitly load the MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
try (Connection connection = DriverManager.getConnection(url, user, pass)) {
// Check if the username or email already exists
if (userExists(connection, username, email)) {
JOptionPane.showMessageDialog(this, "Username or email already exists.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
// SQL query to insert data into the database
String query = "INSERT INTO user (first_name, last_name, username, email, password, age, address, gender) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, firstName);
preparedStatement.setString(2, lastName);
preparedStatement.setString(3, username);
preparedStatement.setString(4, email);
preparedStatement.setString(5, password);
preparedStatement.setString(6, age);
preparedStatement.setString(7, address);
preparedStatement.setString(8, gender);
// Execute the update
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
// Data inserted successfully
JOptionPane.showMessageDialog(this, "User registered successfully!");
NewJFrame loginForm = new NewJFrame();
loginForm.setVisible(true);
// Close or hide the registration form
this.dispose();
} else {
// Handle registration failure
JOptionPane.showMessageDialog(this, "User registration failed!");
}
}
}
} catch (ClassNotFoundException | SQLException ex) {
// Handle exceptions
ex.printStackTrace();
}
}
// Helper method to check if a user with the same username or email already exists
private boolean userExists(Connection connection, String username, String email) throws SQLException {
String query = "SELECT * FROM user WHERE username = ? OR email = ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, username);
preparedStatement.setString(2, email);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
return resultSet.next(); // Returns true if a user with the same username or email exists
}
}
// TODO add your handling code here:
}//GEN-LAST:event_submitActionPerformed
private void in_fnameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_in_fnameActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_in_fnameActionPerformed
private void signinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_signinActionPerformed
NewJFrame loginForm = new NewJFrame();
loginForm.setVisible(true);
// Close or hide the registration form
this.dispose(); }//GEN-LAST:event_signinActionPerformed
/**
* @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(regg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(regg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(regg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(regg.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 regg().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel Fname;
private javax.swing.JLabel Lname;
private javax.swing.JLabel address;
private javax.swing.JLabel age;
private javax.swing.JLabel email;
private javax.swing.JLabel gender;
private javax.swing.JTextField in_address;
private javax.swing.JTextField in_age;
private javax.swing.JTextField in_email;
private javax.swing.JTextField in_fname;
private javax.swing.JComboBox<String> in_gender;
private javax.swing.JTextField in_lname;
private javax.swing.JPasswordField in_passsword;
private javax.swing.JPasswordField in_repassword;
private javax.swing.JTextField in_username;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel password;
private javax.swing.JLabel re_password;
private javax.swing.JButton signin;
private javax.swing.JButton submit;
private javax.swing.JLabel username;
// End of variables declaration//GEN-END:variables
}