How to Create a Complete Login and Forgot Password System in java ?
If you are planning to create a project in java, then you have to have login and forgot password system in your system. In this tutorial i am going to show you how you can create a complete login and forgot password system in java using MySQL Database.
Topics covered by this video:
1. How to create Login form in java.
2. How to send an email from java.
3. How to update database from java.
4. How to access variable from one jform to another jform.
Note: you have to have an internet connection.
STEPS:
[sociallocker]
1. Open Netbeans IDE and click on file > new project. Select categories as java and project as java application from that window:
2. Click next and then give your project name and then click finished.
3. Now right click on your project and select new > JFrame Form give the name of this form as Forgot:
4. After this new window will appear from this window drag Panel and extend it, and drag three label, two Text Field, one Button and drop to the jframe from. change the name of the label to username, password and Forgot Password? and change the variable name of the Text Field by right clicking it and named it as txtUser and txtPass respectively which will looks like this:
5. After download and installing wamp server open it, and then open your localhost, for this open up your any browsing software and type localhost or 127.0.0.1 in the address bar. Then new page will appear then scroll down and find phpmyadmin and click on that. Then you will be able to see the login page. Login in by entering username and password (Default username is root and password is empty).
And then click on new and create one database having any name you want and the click ok, and then create one table having two field in it and click on go. After that, you need to insert the field name like: username and password. and then select the length as well as data type as your requirement and then click on save. And then insert username as example@example.com and password as example. You can watch the video for this designing part, you may understand better in video.
6. Now double click on login button, after doing this you will go to the source code file. Scroll up and find the main class of your project and under that class declare these variable:
Connection con = null; ResultSet rs = null; PreparedStatement pst = null;
7. Go back to Design and double click that Login Button and write the following piece of code:
String query = "select * from forgotpassjava where username=? and password=?"; try{ con=DriverManager.getConnection("jdbc:mysql://localhost/forgotpassjava","root",""); pst=con.prepareStatement(query); pst.setString(1, txtUser.getText()); pst.setString(2, txtPass.getText()); rs=pst.executeQuery(); if(rs.next()){ JOptionPane.showMessageDialog(null, "Login Successfully"); }else{ JOptionPane.showMessageDialog(null, "username or password do not match"); } } catch (SQLException ex) { Logger.getLogger(Forgot.class.getName()).log(Level.SEVERE, null, ex); }
8. Now, When the user move their mouse over Forgot Password the mouse pointer need to be change to the hand because it should be clickable. To do so, Right click over Forgot Password>Events>Mouse Motion> and click mouse move and write the following code inside that event in source file. Don’t worry when you click that events you will automatically redirect to the the source code section of this event.
jLabel3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
9. Now when you click this forgot password you need to see new jform where the application show the form to enter the registered email address. So, To do so first of all we need to add new jform ( SO right click on your project and select new > JFrame Form> give the form name as Sendcode: change the name of two textbox as txtEmail and txtVer) and only then when user click this forgot password label we can display that form. So,design the form in which user can enter the email and after clicking the send button they can enter the code that our application generate and send to their email. The form looks like this:
10. Now to open this form from the login form what you need to do is create one more event of label3 i.e. the text of this label is forgot password. So create the mouse pressed event; To do so, Right click over Forgot Password>Events>Mouse > and click mousePressed and write the following code inside that event.
Sendcode sc = new Sendcode(); this.setVisible(false); sc.setVisible(true);
11. Now go to the Sendcode form and double click that send button to go to the source file and then scroll up and Declare one variable inside the class as :
int randomCode;
12. Now go back to design, double click that send button and write the following code:
try{ Random rand = new Random(); randomCode=rand.nextInt(999999); String host = "smtp.gmail.com"; String user ="YOUR EMAIL"; String pass="YOUR EMAIL PASSWORD"; String to = txtEmail.getText(); String subject="Reseting Code"; String message ="Your reset code is "+randomCode; boolean sessionDebug = false; Properties pros = System.getProperties(); pros.put("mail.smtp.starttls.enable", "true"); pros.put("mail.smtp.host", "host"); pros.put("mail.smtp.port","587"); pros.put("mail.smtp.auth","true"); pros.put("mail.smtp.starttls.required", "true"); java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); Session mailSession = Session.getDefaultInstance(pros, null); mailSession.setDebug(sessionDebug); Message msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress(user)); InternetAddress [] address = {new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject(subject); msg.setText(message); Transport transport = mailSession.getTransport("smtp"); transport.connect(host, user, pass); transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); JOptionPane.showMessageDialog(null, "code has been send to the email"); }catch(Exception ex){ JOptionPane.showMessageDialog(rootPane, ex); }
13. Now double click that Verify button and write the following code because when user click that send button we are sent code in their email:
if(Integer.valueOf(txtVer.getText())==randomCode){ Reset rs = new Reset(txtEmail.getText()); rs.setVisible(true); this.setVisible(false); }else{ JOptionPane.showMessageDialog(null, "code do not match"); }
14. When the user enter correct verification code we need to display another form in which user can reset their password. So, right click on your project and select new > JFrame Form> give the form name as Reset: change the name of two textbox as txtResetPass and txtVerResetPass. The form looks like:
15. Now double click this reset button to go to the source code file, scroll up and declare some variable inside the class because we are going to update our database password so we need to access our database.
Connection con = null; ResultSet rs = null; PreparedStatement pst = null; public String user;
16. Now create one constructor of this class and assign the value of user to the value that we have send from the Sendcode form to this form using the constructor see the verify button click event code, you will understand.
public Reset(String username){ this.user=username; initComponents(); }
17. Now inside the Reset button code write the following Code:
if(txtResetPass.getText().equals(txtVerResetPass.getText())){ //check whether the user enter same password in both textfield try{ String updateQuery = "UPDATE `forgotpassjava` SET `password`=? WHERE username=?"; con = DriverManager.getConnection("jdbc:mysql://localhost/forgotpassjava", "root",""); pst=con.prepareStatement(updateQuery); pst.setString(1, txtVerResetPass.getText()); pst.setString(2, user); pst.executeUpdate(); JOptionPane.showMessageDialog(null, "Reset Successfully"); }catch(Exception ex){ JOptionPane.showMessageDialog(null, ex); } }else{ JOptionPane.showMessageDialog(null, "password do not match"); }
NOTE: Change the connection string ( jdbc:mysql://localhost/forgotpassjava) because your database name may be different than mine.
18. Now download some libraries file from here:
Download mail.jar file: http://www.java2s.com/Code/Jar/m/Downloadmailjar.htm
Download activation.jar file: http://www.java2s.com/Code/Jar/a/Downloadactivationjar.htm
MySQL connector for java: https://dev.mysql.com/downloads/connector/j/
19. Add these jar file to your libraries of your project as:
20. Now right click over your Forgot form run the file.
Enjoy. If you like this tutorial,subscribe our youtube channel and share with your friends.
If you have any confusion, watch the video:
[/sociallocker]