Login Form in JavaFX with MySQL Database

JavaFX is used to create GUI, Desktop applications in Java. The problem with Java Swing is that it is outdated, people are started using JavaFX. So, it’s time to learn JavaFX.



Note: I will use Netbeans IDE throughout this article, you can use any other IDE’s you want.

Create a new project in Netbeans IDE
File-> New Project->JavaFX->JavaFX FXML Application-> Project Name-> Finish

Steps to be followed:

Step 1: Add MySQL JDBC Driver to your project

Step 2: Create two packages util and frames



Step 3: Inside util package, Create a java class with a name ‘ConnectionUtil’

Step 4: Inside frames package, Create two java class and two fxml files with name ‘FXMLDocument.fxml’, ‘FXMLMenu.fxml’, ‘FXMLDocumentController.java’ and ‘LoginApplication.java’

Step 5: Create DB a with name ‘swingapp’ and create a table with a name ’employee’




Open MySQL DB and execute this command,

To create a table inside the newly created DB, execute this command,

Let’s create our table, by executing this query,



Now, insert some values into our table

Here is the project structure for your reference

Now,  Let’s write our JDBC connection code,

ConnectionUtil.java



Now, let’s write the main method, this class will extends the Application class

LoginApplication.java

Now, let’s design our login window, I created this login window using Scene builder, and here is the fxml version of that file,

FXMLDocument.fxml



Once we logged into the application, another window will be open, and here is the fxml file for that,

FXMLMenu.fxml

Now, let’s write the controller part, here will get the connection, and we will query to the DB, if the email and password exists then we will allow the user, otherwise, we will display an alert message,

FXMLDocumentController.java

Now, clean the project and run, you will see this window,



Enter wrong email and password, you will get this alert window,

Now enter the correct email and password, it will allow you to enter the application,

Once you click the ok, the current window/login window will get close and new window will open,




That’s it for this article, do let me know about this article, you can reach me on all my social media profiles, if you like this content, share with your friends.
Thanks for reading, I will see you in the next article.



Love this post?


Help me to buy a cup of coffee/Support us by donating.
All the donations will be going to the website maintenance/improvement.

Pay Now

About the author

Bushan Sirgur

Well, I am Bushan Sirgur from Banglore, India. Currently, I am working as a Software Developer in a Service Base Company. I am interested in JAVA/J2EE, Angular 2, JavaScript, jQuery, MongoDB.

View all posts

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *