Registration Form in Java With Database Connectivity Tutorial

Hello friends, welcome to another tutorial. In this tutorial, we will learn how to create a Gui Registration Form in Java With Database Connectivity. For this example, I will create a student registration form using Java swing with the database connectivity, and the source code can be downloaded at the end of the tutorial.

In my previous tutorial we have learned about how to create a  Login Form in Java, and also we have learned about how to create a Simple Calculator Using Java Swing, and now we will see how we can create the registration form in Java and we will connect it with MySQL database.

I will use Eclipse IDE for the coding part of my program, and for my database, I am going to use MySQL workbench. MySQL workbench is nothing but a GUI version of MySQL console where we can visually design, model, generate and design databases.

So let’s start our tutorial Registration Form in Java With Database Connectivity.

Also Read –> How to Connect MySQL Database in Java Using Eclipse 

Registration Form in Java With Database Connectivity

Prerequisites

First of all, make sure that you have the following piece of software on your system,

After you have downloaded these Softwares from the above links just follow the simple installation process and you are ready to go.

You can also see my Article How to install MySQL Workbench to learn about the installation process of MySQL Workbench with simple and easy steps.

Creating Database in MySQL WorkBench

step 1

  • Open MySQL WorkBench and then go to Database>Connect to Database.
Registration form in java with database connectivity
Registration form in java with database connectivity-fig-1
  • A pop-up window will be opened just click on ok.
Registration form in java with database connectivity-fig-2
  • Once you click on “ok” it will ask you for a password.it is the same password which you have used during the installation process. So enter the correct password and then click on the “ok” button.
Registration form in java with database connectivity-fig-3
  • Now we will create a database (myDatabase in this example)  and inside that database, we will create a table (student in this example) with the following fields,
    • USERNAME
    • GENDER
    • FATHERS_NAME
    • PASSWRD
    • CONFIRMPASSWRD
    • CITY
    • EMAIL
Registration form in java with database connectivity-fig-4

Creating Window For Our Registration Form

Step 2

  • The next thing we have to do is to create the window for our registration form so that we can add components to it.
  • Go to Eclipse IDE and create a new Java Project.
  • For this, you click on the Java Project under the new section of File Menu (File>>New>>Java Project).
Registration form in java with database connectivity
Registration form in java with database connectivity-fig-5
  • Now give a name to your project (RegistrationForm in this example) and click on “Finish”.
Registration form in java with database connectivity
Registration form in java with database connectivity-fig-6
  • Now right click on the project and create a new Java class (New>>Class).
Registration form in java with database connectivity-fig-7
  • Now give a name to your class(RegistrationForm in this Example) and then click on the Finish button.
Registration form in java with database connectivity-fig-8
  • Now import all necessary packages.
  • Implement ActionListener interface to our class so that we will be able to do some button click event in our program.And if we are implementing the ActionListener interface in our class then we have to override it’s method actionPerformed() into our class.
  • Now create an object of the JFrame class.
  • Create a user-defined method createWindow() and inside that, we will set all the properties of our JFrame like its title, its Location and Size, its Visibility, its Default Close Operation, Its Background color, its Layout etc.
  • Now create the constructor of the class and we will call the createWindow() method from constructor.
  • Now again right click on the Project and create another class (Main in this example) so that we can create the object of RegistrationForm class inside main() method.
  • Now run your program.
Registration form in java with database connectivity-fig-9
  • As you can see that we have created our window and now we can add components to it.

Adding Components To Window

Step 3

  • Create the object of
    • 7 JLabel (NAME,GENDER,FATHER NAME,PASSWORD,CONFIRM PASSWORD,CITY,EMAIL).
    • 4 JTextField (For entering the information name,father name,city and email).
    • 2 JPasswordField (one for entering the password and other for confirming the password)
    •  An array of String with two items “Male” and “Female”.
    • One JComboBox for selecting gender of the user and we will pass String array to the constructor of JComboBox.
    • 2 JButton(REGISTER and RESET).
  • Now as we have created components now the next task is to set the location and size of them using the setBounds() method and when we are done with setting the size and location of our components then we have to add them to the JFrame(our Window) using the add() method.
  • Now run your program.
Registration form in java with database connectivity
Registration form in java with database connectivity-fig-10
  • Now you can see that we have successfully added the components to our window.
  • Now we have to do is to add actionListener to the buttons so that when we click on the buttons actionPerformed() method will be called.

Adding ActionListener to Buttons

Step 4

  • For this, we are going to call addActionListener() method using the object of JButton and in its argument we will pass the object of the class in which the ActionListener interface is implemented.

Connecting With Database

Step 5

  • You can refer to this Article How to connect MySQL Database with Java Using Eclipse for the detailed explanations about the connection of MySQL database with Java.
  • In order to connect our java program with MySQL database, we need to include MySQL JDBC driver which is a JAR file, namely mysql-connector-java-5.1.45-bin.jar. The version number in the Jar file can be different.
  • You can download the latest version of MySQL connector from this link (MySQL Connector Java download) As shown in the figure below.
Adding MySQL JDBC Jar File
  • Extract the zip archive and you will get the jar file.
  • Next, we have to include this jar file into our program so that we will be able to connect our java program with MySQL database.
  • Right-click on the project, go to the properties section then select Java Build Path and click on the Add External JARs button.
Adding MySQL JDBC Jar File
  • After clicking on the button a pop-up window will appear where you have to select and open the jar file.
Adding MySQL JDBC Jar File
  • You can see the added Jar file as shown in the figure below. Now click on the Apply and Close button.
Adding MySQL JDBC Jar File
  • Now we want that when we click on the REGISTER button all the entered information should be stored in our database and when we click on the RESET button it should clear the fields and allow us to enter new information also if the passwords entered by the user did not match then it should display us a message that “password did not match”.
  • For doing this, we should know about Java Database Connectivity and also about Prepared Statement in java.
  • Now let’s move to the coding part.
  • Now run your program, enter the information and click on the REGISTER button.
Registration form in java with database connectivity
Registration Form in Java fig-15
  • As we can see that it displayed the message “Data Registered Successfully”.
  • To check whether it has been saved in the database or not go to MySQL workbench and execute this query.
Registration form in java fig-16
  • You can see that our data has been saved successfully.
  • Now click on the RESET button and again input some information with two different passwords.
Registration form in java with database connectivity
Registration form in java fig-17
  • As we can see it displayed the message “password did not match”.
  • You can also download the source code of this project. Download link is given below.

Student Registration Form in Java Swing with Database Connectivity Source Code

  • Here you can download the simple registration form in Java source code.
  • The link of the file is given below.

That’s all for this tutorial in which we have learned how to create a simple registration form in Java with MySQL database connectivity. Please comment below if you have any queries regarding this post. Thank You.

People are also Reading…..

21 thoughts on “Registration Form in Java With Database Connectivity Tutorial”

  1. This is the best article i have ever seen regarding registration form in java with database connectivity

    Thank you for posting this

    Reply
  2. Thanks for the training.
    I love your ideas and visualization of work and I want to learn more from you. Therefore I need your help.

    Best regards.

    Reply
  3. I sincerely thank you very much for this article, it is well explained. Unlike other articles I have read on this topic, yours is very clear and step-by-step instructed for easy understanding. 5***** for you without reservation of any kind, we’ll done, and God bless you.

    Reply
  4. It is very well explained in step by step so nicely . I am able to create my own page in very quick time eventhough I am new to this. Thank you so much,

    Reply

Leave a Comment