TechSupportNepTechSupportNep - Programming & Tech

  • Home
  • C#.NET
  • PHP
  • JAVA
  • Python
  • ASP.NET
  • VB.NET
  • Android
  • C
  • Tech
    • Tech News
  • Patreoner
TechSupportNep
  • Home
  • Programming
  • JAVA
  • How to Create a Complete Login and Forgot Password System in java ?
JAVA

How to Create a Complete Login and Forgot Password System in java ?

By Er. Ran Bahadur B.K. Last updated Aug 15, 2021
0 5,836
Share

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:

 

1. Open Netbeans IDE and click on file > new project. Select categories as java and project as java application from that window:

How to Create Login From in Java

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:

how to create a complete login and forgot password systme in java?
how to create a complete login and forgot password systme in java?

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:

how to create a complete login and forgot password systme in java?
how to create a complete login and forgot password system in java?

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.

how to create a complete login and forgot password system in java?
how to create a complete login and forgot password system in java?

how to create a complete login and forgot password system in java?
how to create a complete login and forgot password system in java?

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:

how to create a complete login and forgot password systme in java?
how to create a complete login and forgot password system in java?

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:

how to create a complete login and forgot password system in java?
how to create a complete login and forgot password system in java?

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:

how to create a complete login and forgot password system in java?
how to create a complete login and forgot password system in java?

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:

 

https://youtu.be/bAqt5rQELR

 

 

Continue Reading
complete login and forgot password system in java using mysql databaseforgot password system in javaforgot password system in java source codehow to create forgot password system in javalogin and forgot password system in javalogin and reset password system in javapassing value from one form in another form in javapassing variable value from one jform to another jform in javarandom number generation in javareset password form in javareset password system in java
0 5,836
Share FacebookTwitterReddItWhatsAppPinterestEmail
Er. Ran Bahadur B.K.60 posts 56 comments

Ran Bahadur B.K. is the owner of TechSupportNep who is working with the aim of providing the quality Technology content. He is creating Tech and Programming videos, you can see all them in his YouTube Channel(https://www.youtube.com/TechSupportNep). He is also working with some company.

Prev Post

How to Create Complete Forgot Password System in C#.NET using SQL Server Database?

Next Post

Windows 10 Build 1909 Features that you should know.

You might also like More from author
JAVA

How to Generate and Send OTP SMS for Free Using java?

Leave a comment
Help Needed

This website is free of annoying ads. We want to keep it like this. You can help with your support:

Donate!

The need for Support! Why to Support?

If you need your own website? contact me!
erranbahadurbk
Fiverr
Seller
Hello there,This is me Ran Bahadur B.K., A graduated computer engineer. I have 3+ years of work experience in wordpress design and development. I have strong knowledge of HTML, CSS and PHP which are the three ingredients of web development. Currently i am working with the software company so i also have experience in desktop apps and mobile apps development.

Latest

Python

How to Create Login Application in Python using SQLite Database?

How to use View Binding in Android?

Windows 10 Build 1909 Features that you should know.

How to Create a Complete Login and Forgot Password System in java ?

Prev Next 1 of 15
  • About us
  • Contact us
  • Privacy Policy
  • Disclaimer
© 2022 All Rights Reserved.
Website By: Ran Bahadur B.K.