Java Swing Login Form with Database Connection using MySQL

Hello friends. In this tutorial, we will learn how to create a Java Swing Login Form with Database Connection using MySQL Database. In the previous tutorial, we learned how to create a login form in Java Swing, but we had not connected that to the database. In this tutorial, we will connect our Java login page with the MySQL database, and then we will do the login authentication using the database.

In this Login application, we will also add a useful functionality, which is if the user forgets his password, then he can also reset the password.

So let’s start our tutorial about Java Swing Login Form with Database Connection using MySQL Database step by step. The source code of this tutorial is given at the end of the tutorial.

Java Swing Login Form with Database Connection using MySQL

Prerequisite

Before moving ahead to our tutorial, first of all, make sure that you have the following pieces of software in your system.

You’ll need the Java Development Kit installed on your system to compile and run Java code. You can download the latest version of the JDK from the official Oracle website.

To run the program as-is, you need a MySQL database server installed and running on your local machine.

You’ll need the MySQL JDBC driver to enable communication between your Java code and the MySQL database.

While you can write and compile Java code using a simple text editor and command-line tools, using an IDE can greatly simplify development.

1. Setting up MySQL Database

  • In this step, we will create the database and table using the MySQL Command Line Client or MySQL Workbench. For this tutorial, I am going to use MySQL Command Line Client.
  • I assume you have already downloaded and installed the MySQL server in your system. If you haven’t, then you may prefer this tutorial – How to install MySQL server 

Launch MySQL Command Line Client

  • Next, Open the command prompt or terminal and launch the MySQL Command Line Client using the following command. And then, you have to enter the MySQL root password.

Create the Database

  • Once you’re in the MySQL Command Line Client, you can create a new database using the follwing query. In our Java login system example, we’ll name the database “userlogindb” but you can choose a different name if you prefer.

Use the Database

  • After creating the database, switch to it using the following query.

Create the Table

  • Now, in this step, we will create the table where the user login information will be stored. In our example, the table is named “userlogintable,” and it should have at least two columns: “USERNAME” and “PASSWORD” You can execute the following SQL query to create the table.

Insert Sample Data

  • Now, we will populate our table with sample data. For this, we will use the INSERT INTO statement.
  • So, we have created our database and table and will now move to the Java programming part.

2. Set Up Your Java Project

  • Open your Java IDE (Eclipse, IntelliJ, NetBeans etc.).
  • Create a new Java project named “LoginApp”.

If you don’t want to use any IDE, then you can simply start with any text editor like Notepad or Notepad++.

3. Create the Class “LoginFormDatabase”

  • In this step, first of all, we will import all the required libraries for creating our login form, handling GUI components, and interacting with the MySQL database.
  • Next, we will define our main class, LoginFormDatabase, and implement the ActionListener interface to handle GUI events such as button clicks.

4. Create the Main Method

  • In the LoginFormDatabase class, add a main method to start the application, and inside that, create an instance of the LoginFormDatabase class. This is the entry point of the application.

5. Creating GUI Components

  • In this step, inside the “LoginFormDatabase” class, we will declare various components that will be part of the GUI, such as main window, labels, text fields, buttons, and a checkbox.
  • By declaring these components at the class level, outside of any method, we ensure that they can be accessed and modified by different methods within the class.

6. Creating Constructor and Setting Up GUI

  • In this step, we will create the constructor of our class “LoginFormDatabase”. The constructor is where we will set up the GUI by creating the main window of the application using the JFrame and then adding the GUI components (labels, text fields, buttons, and a checkbox) within the window.
  • In this constructor, we will also set up event listeners to various components (buttons and a checkbox) using the addActionListener method.

7. Adding MySQL JDBC Driver JAR File

  • In order to establish a connection between your Java program and the MySQL database, you must include the MySQL JDBC driver, which comes in the form of a JAR file.
  • The latest version of the MySQL Connector can be downloaded from the following link (MySQL Connector Java download).
  • Upon visiting the link, opt for the “Platform Independent” option from the drop-down menu. Then download the Platform Independent (Architecture Independent), ZIP Archive. 
  • After extracting the zip archive, you will get the JAR file.
  • After this, you need to include this JAR file in your program, which will enable the connection of your Java program with the MySQL database.
  • For NetBeans users,
    • Right click on the Libraries folder then select Add JAR/Folder and then you can add the JAR file to your project.
  • For Eclipse users,
    • Right click on the Project then select Properties —>>>>Java Build Path and then you can choose Add External JARs button to add the JAR file.
  • For IntelliJ IDEA users,
    • Go to File––>>>>Project Structure—>>>>Libraries then click on the “+” sign and select Java and then you can add the JAR file.

8. Implementing Event Handling (actionPerformed Method)

  • Now, in this step, we will implement the actionPerformed method to handle actions like login, reset, show password, and forgot password. The behavior we want in response after clicking any button or checkbox is coded inside the actionPerformed() method.

Login Button Code/ Login Validation

When the Login Button is clicked, we want that,

  • It should connect to the MySQL Database named “userlogindb” running on the local machine at port 3306 and use the username “root” and password “root” for authentication.
  • Next, It should handle user authentication for the login form by retrieving user input.
  • Next, it should query the database for the user records and validate them with the entered credentials by the user.
  • Any Exception might occur while connecting to the database, so the code is within the try-catch block.

Show Password Code

We want that when the Show Password checkbox is clicked,

  • It should allow the users to toggle the visibility of the password they’ve entered in the password field.

Reset Button Code

We want that when the Reset Button is clicked,

  • It should clear the input fields for both the username and password.

Forgot Password Button Code

We want that when the Forgot Password Button is clicked,

  • It should close the Login Window and open the “Forgot Password” window for the Password Recovery.
  • We will implement the logic of password recovery in a separate class, “ForgotPassword”.

9. Creating Forgot Password Window for Password Recovery

In this class,

  • we will create a Forgot Password Window with a text field for entering the username and buttons for changing the password or going back to the Login Form.
  • If the correct username is entered and validated against the database and the change password button is clicked, we will move to the next window, where we can change the password.


10. Creating Change Password Window to Change Password

In this class,

  • We’ll design a “Change Password” window that includes two password input fields for setting a new password and confirming it. The user will initiate the password change process by clicking the “Update” button, and the updated password will be saved in the database.
  • If the user chooses to return to the login form, he can do so by clicking the “LOGIN” button.

11. Final Code

  • The final code of our application, Java Swing Login Form with Database Connection using MySQL Database, is the following.
  • Now, Let’s run our program.
Java Swing Login Form with Database Connection
Java Swing Login Form with Database Connection – fig – 1
  • After running the program in the above image, you can see the GUI of our Login form in Java.
  • Now, Enter the login credentials and click the Login Button to see what happens.
Java Swing Login Form with Database Connection
Java Swing Login Form with Database Connection – fig – 2
  • As you can see in the above image, when we entered the right login credentials and then clicked on the Login button, then the message dialog box has been displayed with the message “Login Successful”.
  • Now, we will enter the invalid username or password, tick the checkbox to reveal the password, and then click the “Login Button” to see what happens.
Java Swing Login Form with Database Connection
Java Swing Login Form with Database Connection – fig – 3
  • In the image displayed above, you can observe that incorrect login credentials were input, and when the ‘show password’ checkbox was activated, the password became visible. Following the click on the Login button, a message dialog box appeared with the message “Username or Password did not Match”.
  • Next, clicking on the Reset Button will reset the username text field and password field to blank.
  • Next, we will click the Forgot Password button, which will redirect us to another window “Forgot Password”, where we need to enter the valid username to proceed with the password change process.
Java Swing Login Form with Database Connection
Java Swing Login Form with Database Connection – fig – 4
  • Now, if we enter the valid username and click the Change Password button, it will redirect us to another window, “Change Password.”
  • If we input the invalid username, it will give the message “Invalid Username.”
  • If you want to Go Back to the Login Window again, you can do it by clicking on the “Go Back” Button.
Java Swing Login Form with Database Connection
Java Swing Login Form with Database Connection – fig – 5
  • As you can see in the above image, when the password is entered and confirmed and then clicked on the Update Button, It gave the message “Password Updated Successfully.”
  • If the Passwords do not match, the message box will give the message “Password did not Match.”
  • You can also return to the Login window by clicking the “Login” Button.

Java Swing Login Form with Database Connection Source Code Download

  • You can download the source code of this project by clicking on the link below.

So this was all from this tutorial about Java Swing Login Form with Database Connection using MySQL Database. If you have any questions or doubts regarding this post, feel free to let me know by commenting below.

Related Articles

Leave a Comment