How to Create Multi User Login Form in Java using MySQL Database

Hello Friends, In this tutorial, we will learn How to Create Multi User Login Form in Java using MySQL Database step by step. You can implement this functionality in your Java Projects for improved security and robustness.

It will be a role-based login in Java in which the user will enter the username and password and his account type (Admin or User). Upon successful validation of the credentials, the user will be redirected to their respective dashboard.

Preview

Take a look at the preview of our Multi User Login form using Java, as it will provide you with a clearer understanding of the objective we aim to accomplish in this tutorial.

Prerequisite

Softwares

First, make sure you have downloaded and installed the following piece of software in your system.

Tutorials

To create our multi-user login form in Java, it is important to have a solid understanding of the following Java Swing components and Java Button click Event.

Once you have completed the topics mentioned above, we can now proceed with our tutorial on How to Create a Multi User Login form in Java using MySQL Database.

How to Create Multi User Login Form in Java using MySQL Database

#1 Setting up MySQL Database 

  • In this step, we will set up the MySQL Database. I am going to use MySQL Command Line Client for this purpose. If you don’t know how to correctly download and install MySQL Command Line Client in your system, then you can refer to my previous article How to install MySQL Commend Line Client.
  • Once you have successfully downloaded and installed the MySQL Command Line Client on your system, proceed to open it.
  • The MySQL Command Line Client will prompt you to enter your password, which should be the same password you provided during its installation. Simply input the password and press the enter button.
How to Create Multi User Login Form in Java using MySQL Database
How to Create Multi User Login Form in Java using MySQL Database fig – 1

#2 Creating Database and Table in MySQL 

  • Now in this step, we will create the database (multiuserdb) in this example and inside that database, we will create a table (accoutnsinfo).
  • Next, we will insert values and create two rows in our table, as given below.
How to Create Multi User Login Form in Java using MySQL Database fig – 2

#3 Designing the Login Form

  • In this step, we will design our Multi-User Login Form.
  • To begin, ensure you import all the essential packages required for our program. This step will enable us to access the necessary interfaces and classes used throughout our code.
  • Next, create a class MultiUserLoginForm. Inside this class, create the constructor of the class along with the main method.
  • Next, create the object of the class MultiUserLoginForm, inside the main method.
  • Next, inside the class, MultiUserLoginForm, create the objects of all the swing components that we want to add in our Multi-User Login Form.
  • Next, inside the constructor of the class MultiUserLoginForm, we will set the properties of all the swing components that we have created.
  • I have also added the icons to our Login Form, which I will provide you along with the source code. You have to add those icons inside the project folder.
  • Code is given below.
  • Now run your program.
How to Create Multi User Login Form in Java using MySQL Database
How to Create Multi User Login Form in Java using MySQL Database fig – 3
  • As you can see that we have successfully designed our Login Form.

#4 Adding ActionListener to Buttons Using Anonymous Inner Class

  • Next, inside the constructor of the class MultiUserLoginForm, we will implement the ActionListener to both of our buttons using the anonymous inner class.

#5 Adding MySQL Connector JAR file

  • Now in this step, we will connect our program with the MySQL Database using JDBC (Java Database Connectivity).
  • 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, as demonstrated in the figure below.
fig – 4
  • Then download the Platform Independent (Architecture Independent), ZIP Archive as shown below.
fig – 5
  • 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.

#6 Establishing Database Connection

With the JAR file already added, we are now prepared to establish the connection between our Java program and the MySQL Database.

We want that when someone clicks on the Login Button, then our program should be connected to the database. Therefore, we will code this inside the actionPerformed() method, which is related to the Login Button.

  • Establish the connection using , DriverManager.getConnection(String URL), which returns a Connection reference.
  • In the String URL parameter, You have to write in the following format: jdbc:mysql://localhost:3306/multiuserdb, “root”, “root” where:
  • jdbc represents the API.
  • mysql indicates the database.
  • localhost denotes the server name where MySQL is running.
  • 3306 represents the port number.
  • multiuserdb is the name of the database. If your database has a different name, replace it accordingly.
  • The first “root” stands for the MySQL database’s default username.
  • The second “root” is the password provided during MySQL database installation.
  • To handle potential SQL Exceptions during the connection process, ensure to enclose the code within a try-catch block.

#7 Validating User Credentials and Redirecting based on User Role

As we know, there are 3 fields in our Multi Login Form, Account_type, Username, and Password.
Now we want that,

  • if any of the field is empty and the person clicks on the Login Button then a message dialog box should appear with the message “Please Enter all Fields“.
  • When the Username and Password are incorrect, then the message should appear “Username & Password did not match“.
  • If the Username & Password are correct and the right account type has been chosen, then the new window should open related to the user type. For this, we will create a new JFrame in a separate class.
  • If the person clicks on the close button, then the program should exit.
  • The whole program of our Multi User Login Form in Java is given below.
  • Now run your program and enter the credentials.

Download Source Code

  • You can download the source of the Multi User Login Form in Java Swing by clicking the link below.

Download Source Code

So this was all from this tutorial about how to create multi user login form in Java 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