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.
- A pop-up window will be opened just click on ok.
- 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.
- 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
1 2 3 4 5 6 |
create database myDatabase; use myDatabase; create table student(USERNAME varchar(30) not null,GENDER varchar(10) , FATHERS_NAME varchar(30) ,PASSWRD varchar(20), CONFIRMPASSWRD varchar(20) ,CITY varchar(30), EMAIL varchar(30) ); |
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).
- Now give a name to your project (RegistrationForm in this example) and click on “Finish”.
- Now right click on the project and create a new Java class (New>>Class).
- Now give a name to your class(RegistrationForm in this Example) and then click on the Finish button.
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 |
import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.sql.*; public class RegistrationForm implements ActionListener { @Override public void actionPerformed(ActionEvent e) { } } |
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
//importing packages import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.sql.*; public class RegistrationForm implements ActionListener { //Creating object of JFrame class JFrame frame; //Creating user-defined method public void createWindow() { //Setting properties of JFrame frame=new JFrame(); frame.setTitle("Registration Form"); frame.setBounds(40,40,380,600); frame.getContentPane().setBackground(Color.pink); frame.getContentPane().setLayout(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); } @Override public void actionPerformed(ActionEvent e) { } } |
- Now create the constructor of the class and we will call the createWindow() method from constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.sql.*; public class RegistrationForm implements ActionListener { JFrame frame; //Creating constructor RegistrationForm() { createWindow();//calling method from constructor } public void createWindow() { frame=new JFrame(); frame.setTitle("Registration Form"); frame.setBounds(40,40,380,600); frame.getContentPane().setBackground(Color.pink); frame.getContentPane().setLayout(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); } @Override public void actionPerformed(ActionEvent e) { } } |
- 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.
1 2 3 4 5 6 7 |
public class Main { public static void main(String[] args) { //creating object of RegistrationForm class new RegistrationForm(); } } |
- Now run your program.
- 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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.sql.*; public class RegistrationForm implements ActionListener { JFrame frame; //Creating objects String[] gender={"Male","Female"}; JLabel nameLabel=new JLabel("NAME"); JLabel genderLabel=new JLabel("GENDER"); JLabel fatherNameLabel=new JLabel("FATHER NAME"); JLabel passwordLabel=new JLabel("PASSWORD"); JLabel confirmPasswordLabel=new JLabel("CONFIRM PASSWORD"); JLabel cityLabel=new JLabel("CITY"); JLabel emailLabel=new JLabel("EMAIL"); JTextField nameTextField=new JTextField(); JComboBox genderComboBox=new JComboBox(gender); JTextField fatherTextField=new JTextField(); JPasswordField passwordField=new JPasswordField(); JPasswordField confirmPasswordField=new JPasswordField(); JTextField cityTextField=new JTextField(); JTextField emailTextField=new JTextField(); JButton registerButton=new JButton("REGISTER"); JButton resetButton=new JButton("RESET"); RegistrationForm() { createWindow(); } public void createWindow() { frame=new JFrame(); frame.setTitle("Registration Form"); frame.setBounds(40,40,380,600); frame.getContentPane().setBackground(Color.pink); frame.getContentPane().setLayout(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); } @Override public void actionPerformed(ActionEvent e) { } } |
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.sql.*; public class RegistrationForm implements ActionListener { JFrame frame; String[] gender={"Male","Female"}; JLabel nameLabel=new JLabel("NAME"); JLabel genderLabel=new JLabel("GENDER"); JLabel fatherNameLabel=new JLabel("FATHER NAME"); JLabel passwordLabel=new JLabel("PASSWORD"); JLabel confirmPasswordLabel=new JLabel("CONFIRM PASSWORD"); JLabel cityLabel=new JLabel("CITY"); JLabel emailLabel=new JLabel("EMAIL"); JTextField nameTextField=new JTextField(); JComboBox genderComboBox=new JComboBox(gender); JTextField fatherTextField=new JTextField(); JPasswordField passwordField=new JPasswordField(); JPasswordField confirmPasswordField=new JPasswordField(); JTextField cityTextField=new JTextField(); JTextField emailTextField=new JTextField(); JButton registerButton=new JButton("REGISTER"); JButton resetButton=new JButton("RESET"); RegistrationForm() { //Calling methods from constructor createWindow(); setLocationAndSize(); addComponentsToFrame(); } public void createWindow() { frame=new JFrame(); frame.setTitle("Registration Form"); frame.setBounds(40,40,380,600); frame.getContentPane().setBackground(Color.pink); frame.getContentPane().setLayout(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); } public void setLocationAndSize() { //Setting Location and Size of Each Component nameLabel.setBounds(20,20,40,70); genderLabel.setBounds(20,70,80,70); fatherNameLabel.setBounds(20,120,100,70); passwordLabel.setBounds(20,170,100,70); confirmPasswordLabel.setBounds(20,220,140,70); cityLabel.setBounds(20,270,100,70); emailLabel.setBounds(20,320,100,70); nameTextField.setBounds(180,43,165,23); genderComboBox.setBounds(180,93,165,23); fatherTextField.setBounds(180,143,165,23); passwordField.setBounds(180,193,165,23); confirmPasswordField.setBounds(180,243,165,23); cityTextField.setBounds(180,293,165,23); emailTextField.setBounds(180,343,165,23); registerButton.setBounds(70,400,100,35); resetButton.setBounds(220,400,100,35); } public void addComponentsToFrame() { //Adding components to Frame frame.add(nameLabel); frame.add(genderLabel); frame.add(fatherNameLabel); frame.add(passwordLabel); frame.add(confirmPasswordLabel); frame.add(cityLabel); frame.add(emailLabel); frame.add(nameTextField); frame.add(genderComboBox); frame.add(fatherTextField); frame.add(passwordField); frame.add(confirmPasswordField); frame.add(cityTextField); frame.add(emailTextField); frame.add(registerButton); frame.add(resetButton); } @Override public void actionPerformed(ActionEvent e) { } } |
- Now run your program.
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.sql.*; public class RegistrationForm implements ActionListener { JFrame frame; String[] gender={"Male","Female"}; JLabel nameLabel=new JLabel("NAME"); JLabel genderLabel=new JLabel("GENDER"); JLabel fatherNameLabel=new JLabel("FATHER NAME"); JLabel passwordLabel=new JLabel("PASSWORD"); JLabel confirmPasswordLabel=new JLabel("CONFIRM PASSWORD"); JLabel cityLabel=new JLabel("CITY"); JLabel emailLabel=new JLabel("EMAIL"); JTextField nameTextField=new JTextField(); JComboBox genderComboBox=new JComboBox(gender); JTextField fatherTextField=new JTextField(); JPasswordField passwordField=new JPasswordField(); JPasswordField confirmPasswordField=new JPasswordField(); JTextField cityTextField=new JTextField(); JTextField emailTextField=new JTextField(); JButton registerButton=new JButton("REGISTER"); JButton resetButton=new JButton("RESET"); RegistrationForm() { createWindow(); setLocationAndSize(); addComponentsToFrame(); actionEvent(); } public void createWindow() { frame=new JFrame(); frame.setTitle("Registration Form"); frame.setBounds(40,40,380,600); frame.getContentPane().setBackground(Color.pink); frame.getContentPane().setLayout(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); } public void setLocationAndSize() { nameLabel.setBounds(20,20,40,70); genderLabel.setBounds(20,70,80,70); fatherNameLabel.setBounds(20,120,100,70); passwordLabel.setBounds(20,170,100,70); confirmPasswordLabel.setBounds(20,220,140,70); cityLabel.setBounds(20,270,100,70); emailLabel.setBounds(20,320,100,70); nameTextField.setBounds(180,43,165,23); genderComboBox.setBounds(180,93,165,23); fatherTextField.setBounds(180,143,165,23); passwordField.setBounds(180,193,165,23); confirmPasswordField.setBounds(180,243,165,23); cityTextField.setBounds(180,293,165,23); emailTextField.setBounds(180,343,165,23); registerButton.setBounds(70,400,100,35); resetButton.setBounds(220,400,100,35); } public void addComponentsToFrame() { frame.add(nameLabel); frame.add(genderLabel); frame.add(fatherNameLabel); frame.add(passwordLabel); frame.add(confirmPasswordLabel); frame.add(cityLabel); frame.add(emailLabel); frame.add(nameTextField); frame.add(genderComboBox); frame.add(fatherTextField); frame.add(passwordField); frame.add(confirmPasswordField); frame.add(cityTextField); frame.add(emailTextField); frame.add(registerButton); frame.add(resetButton); } public void actionEvent() { //Adding Action Listener to buttons registerButton.addActionListener(this); resetButton.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { } } |
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.
- 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.
- After clicking on the button a pop-up window will appear where you have to select and open the jar file.
- You can see the added Jar file as shown in the figure below. Now click on the Apply and Close button.
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.sql.*; public class RegistrationForm implements ActionListener { JFrame frame; String[] gender={"Male","Female"}; JLabel nameLabel=new JLabel("NAME"); JLabel genderLabel=new JLabel("GENDER"); JLabel fatherNameLabel=new JLabel("FATHER NAME"); JLabel passwordLabel=new JLabel("PASSWORD"); JLabel confirmPasswordLabel=new JLabel("CONFIRM PASSWORD"); JLabel cityLabel=new JLabel("CITY"); JLabel emailLabel=new JLabel("EMAIL"); JTextField nameTextField=new JTextField(); JComboBox genderComboBox=new JComboBox(gender); JTextField fatherTextField=new JTextField(); JPasswordField passwordField=new JPasswordField(); JPasswordField confirmPasswordField=new JPasswordField(); JTextField cityTextField=new JTextField(); JTextField emailTextField=new JTextField(); JButton registerButton=new JButton("REGISTER"); JButton resetButton=new JButton("RESET"); RegistrationForm() { createWindow(); setLocationAndSize(); addComponentsToFrame(); actionEvent(); } public void createWindow() { frame=new JFrame(); frame.setTitle("Registration Form"); frame.setBounds(40,40,380,600); frame.getContentPane().setBackground(Color.pink); frame.getContentPane().setLayout(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); } public void setLocationAndSize() { nameLabel.setBounds(20,20,40,70); genderLabel.setBounds(20,70,80,70); fatherNameLabel.setBounds(20,120,100,70); passwordLabel.setBounds(20,170,100,70); confirmPasswordLabel.setBounds(20,220,140,70); cityLabel.setBounds(20,270,100,70); emailLabel.setBounds(20,320,100,70); nameTextField.setBounds(180,43,165,23); genderComboBox.setBounds(180,93,165,23); fatherTextField.setBounds(180,143,165,23); passwordField.setBounds(180,193,165,23); confirmPasswordField.setBounds(180,243,165,23); cityTextField.setBounds(180,293,165,23); emailTextField.setBounds(180,343,165,23); registerButton.setBounds(70,400,100,35); resetButton.setBounds(220,400,100,35); } public void addComponentsToFrame() { frame.add(nameLabel); frame.add(genderLabel); frame.add(fatherNameLabel); frame.add(passwordLabel); frame.add(confirmPasswordLabel); frame.add(cityLabel); frame.add(emailLabel); frame.add(nameTextField); frame.add(genderComboBox); frame.add(fatherTextField); frame.add(passwordField); frame.add(confirmPasswordField); frame.add(cityTextField); frame.add(emailTextField); frame.add(registerButton); frame.add(resetButton); } public void actionEvent() { registerButton.addActionListener(this); resetButton.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==registerButton) { try { //Creating Connection Object Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/myDatabase","root","root"); //Preapared Statement PreparedStatement Pstatement=connection.prepareStatement("insert into student values(?,?,?,?,?,?,?)"); //Specifying the values of it's parameter Pstatement.setString(1,nameTextField.getText()); Pstatement.setString(2,genderComboBox.getSelectedItem().toString()); Pstatement.setString(3,fatherTextField.getText()); Pstatement.setString(4,passwordField.getText()); Pstatement.setString(5,confirmPasswordField.getText()); Pstatement.setString(6,cityTextField.getText()); Pstatement.setString(7,emailTextField.getText()); //Checking for the Password match if(passwordField.getText().equalsIgnoreCase(confirmPasswordField.getText())) { //Executing query Pstatement.executeUpdate(); JOptionPane.showMessageDialog(null,"Data Registered Successfully"); } else { JOptionPane.showMessageDialog(null,"password did not match"); } } catch (SQLException e1) { e1.printStackTrace(); } } if(e.getSource()==resetButton) { //Clearing Fields nameTextField.setText(""); genderComboBox.setSelectedItem("Male"); fatherTextField.setText(""); passwordField.setText(""); confirmPasswordField.setText(""); cityTextField.setText(""); emailTextField.setText(""); } } } |
1 2 3 4 5 6 |
public class Main { public static void main(String[] args) { new RegistrationForm(); } } |
- Now run your program, enter the information and click on the REGISTER button.
- 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.
1 2 |
use myDatabase; select * from student; |
- 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.
- 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…..
- Menu Driven Program in Java Using Switch Case
- How to Create Calculator in Java Swing
- How to Create Tic Tac Toe Game in Java
- How to Create Login Form in Java Swing
- Java Text to Speech
- How to Create Splash Screen In Java
- How to Create Mp3 Player in Java
- Java Button Click Event
- 11 Best site to Learn Java Online for Free
This is the best article i have ever seen regarding registration form in java with database connectivity
Thank you for posting this
Thank you shreya
Keep visiting for more posts
getSelectedItem().toString() generates random string in mysql
Did you follow the tutorial correctly?
congratulation…your work is good. Go on like that……
Thank You Post
Nice Post
Great work, keep it up so that my kind of person who is slow to java can understand.
Nothing happens when I click the register button.
Hello? Anybody? It doesn’t work!
Thankyou for this poat it is very nice block and all concept declear very simple. Thanks once again.
Thank you Vijendra
thanks it is very good .carry on!
Gods job may god help you, I will like to be communicating with u
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.
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.
Thank you so much Jelili
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,
how to create registration and login page in java using sql
Hello Jayakumar
Thank you for the comment
I will soon publish a post about this..
Keep sharing !!
Yes, I do. Thank you for this tutorial, You are a brilliant person
Keep the handworks✊????