Registration Form in Java Swing with MySql Database

Registration Form in Java Swing is covered in this Post.

To develop this project we used NetBeans IDE.

Steps to develop Registration Form in Java Swing is as below

  1. Create User Interface (UI) Using JFrame
  2. Collect all data in Model sent from UI
  3. Validate and Process Received data
  4. Saving data to MySql Database
Project Explorer for Registration Form

Here we have added three external Jar files

  1. mysql-connector-jar-5.1.14-bin.jar – To connect Java program with MySql Database
  2. jBCrypt-0.4.jar – To encrypt password
  3. jdatepicker-1.3.4.jar– To include date picker in JFrame

To Develop Registration form we are considering example of Student.

We will create basic interface to collect student data in JFrame.

Our Final UI JFrame will as below

Registration Form in Java Swing

1 Create User Interface using JFrame

A. Create a basic structure for frame

In main method we created RegisterFrame class object.

set title, visibility, specified frame coordinated and height and width with bounds and other properties.

Created constructor RegisterFrame() to create and initialize frame components (JLable, JTextBox, JButton, …) .

Method setBounds() is used to set location, width and height of components.

addComponents() is used to add components to Container.

Output of above code is as below

JFrame Basic Size
Fig: JFrame height and width

This is basic layout for our Registration form.

B. Adding Components to JFrame

Components JLabel, JTextField, JRadioButton, JPasswordField, JCombobox, JButton etc are used here.

To select Date from Date Picker additional Jar jdatepicker-1.3.4.jar is added to project.

B Add the basic component in JFrame

  1. Components are Declare as Instance variable
  2. Objects of components are created inside Default Constructor
  3. setBounds() is used to set position ,width and height of components.
  4. addComponents() is used to add components in Container.
  5. addActionListener() is used to add action listener on registerButton.
  6. actionPerformed() is over ridden to handle event

Last two steps are handled later on.

After adding basic components it will create Frame as below

Adding Components in JFrame
Fig: Adding Basic Components in JFrame

C Add JDatePicker in JFrame

First check jdatepicker-1.3.4.jar is added in project

Imports for JDatePicker

Adding JDatePicker in constructor

complete code after adding JDatePicker

DateLabelFormatter class is used to show date selector.

JDatePicker Example
Fig: JDatePicker Addition in JFrame

It will complete our UI

2 Collect all data in Model sent from UI

To collect data Register button is clicked and all data from text box and other input fields are collected and set to student Model.

Lets First See student model Students.java

This class contains basic fields of student, constructors, getter and setter methods and to string method.

Adding following code to RegisterFrame.java

  1. ActionLister is added for registerButton
  2. actionPerformed() is overridden to handle click event on register button

Based on genderFemale and genderMale is selected gender variable.is set based on that.

Similar way storing programName, branchName and semesterName from respective dropdown list.

To get value from JDatePicker datePicker.getJFormattedTextField().getText() is used.

It returns value in string to convert string date to java.sql.date Date.valueOf(dobString) is used.

3 Validate and Process Received data

Received data is stored in Student object.

We have already validated dob field.

Other fields are validated in Validation.java

  1. All fields are checked for Empty.
  2. Name length must be more than 4 and less than 20
  3. Validation for Mail id
  4. Mobile Number must be 10 digits long
  5. Password must be between 8 to 20 characters
  6. Password must contain One digit one upper case letter and special symbol
  7. RePassword and password must match

Creating object of validation and and checking for errors if error occurs then it is shown using showMessageDialog().

Processing Data

Processing data is done before and after validation

  1. Based on male and female JRadioButton we assigned male or female to gender variable.
  2. Get Dob from JDatePicker as string and then converted again to java.sql.Date.
  3. BCrypt is used to encrypt password and set to encPassword().

4 Saving data to MySql Database

Validated and processed data and stored in Student object.

To save data of student object first create student table as given below.

ConnectionFactory.java

This file is used to connect java with database and returns the connection object.

Database name :ebhor
Username: ebhor_user
Password: 21V6

RegisterDAO.java

registerStudent() takes a student object and save each field to database using java preparedStatement();

RegisterFrame.java

Receiving response from registerStudent().

Finally Saving data on student table

Student Registration Example
Fig: Registration Done
MySql student table perview
Fig: MySql student table perview

Download Registration Form in Java Swing

Read More

  1. JLabel in Java Swing
  2. JComboBox in Java Swing
  3. JTable in Java Swing
  4. JTable Pagination in Java JDBC
  5. Login form in Java Swing
  6. Simple Calculator in Java Applet
  7. Applet Life Cycle in Java

Login form in Java Swing and MySql Database with source code

Login form in Java Swing and MySql Database

We considered a small example with student details to login From Java Swing and redirecting to another frame after login.

Java code for login page with database in netbeans

We have covered

  • Login Page design using JFrame
  • Validation on Login Page
  • Event Handling for Login button
  • Accessing Student details from MySQL database
  • Matching encrypted password using jBCrypt.

To make our project functionating, we have included additional jar files mysql-connector-java jar file and jBCrypt jar file.

You can check both jar version in below Project Explorer.

Login form in java swing with source code example is developed using NetBeans IDE.

Project Explorer for this project is as below.

Project Explorer for Login form in Java Swing
Fig: Project Explorer

In above Figure you can see, there is a package name login and its three sub packages dao, frames and model.

login.dao contains following three files

1. ConnectionFactory.java -Used to establish connection from database.

2. LoginDAO.java– Extracts user details from student table from MySQL database.

3. Student.sql– It is sql query to create table structure. Also contains a record of a student.

login.frame package contains following files

1 LoginFrame.java – It contains JFrame to create login Frame.

2. HomeFrame.java – It contains Jframe to create home Frame.

3. Validation.java – This file is used to validate userid and password field of LoginFrame

login.model this package contains a file

1 Student.java – This file contains necessary fields of students its getter setter and toString() method. This is a simple Pojo File.

Libraries contains jar files

1 jdk1.8 – Using jdk1.8 for this project

2. mysql-connector-java – Used to connect MySQL database with Java

3 JBCrypt – Used to encrypt password.

To Start a new Project in NetBeans click on

File->New Project

From Category select Java and at Projects select Java Application

then click on next

then provide project Name and select Use dedicated folder for Storing libraries then click on Finish.

This will create a project.

Create a package login and inside login create three packages frames, model and dao.

Create a Login Frame

Create a login frame in login.frames package.

General methods in side login frame is as below.

1 Declare component like buttons,textbos, password field etc in side LoginFrame class

2. Create a default constructor inside this create object for declared components.

3. Create a method setBounds() to assing positions for components

4. Add all components to Container in addComponent()

5 Method addActionListener() is created to add actionListeners() to components

6 actionPerformed() overridden method is used to handle the event

7 main() is creating object of class and setting title, visibility, frame size, EXIT_ON_CLOSE and resizable properties of frame.

Following code contains the general structure of frame

Add Components to Login Frame

Here components JLabel, JTextField, JButton, Container are declared and its objects are created in default constructor , bounds are assigned in setBounds() and componets are added to container in addComponent().

Java Swing Login Example
Fig: Java Swing Login Example

Learn basic JFrame tutorial here

Add Action Listener and perform check for correct userid and password

Added actionListener for loginButton. Action handling is performed in actionPerformed().

We are overriding actionPerformed() of ActionListener Interface.

IF any event occurs then we check its source by e.getSource() method.

Here we are comparing e.getSource() with loginButton. java button click event is handled here.

If login button is clicked then statements inside if statement is executed.

Inside if statment we are getting values of userNameTextField.getText() and passwordField.getText();

getText() is used to get textbox value as text.

getText() of JPassword field is depricated but for demo purpose we are using it.

after getting userid and password we are mathing both with our user id and password

if (userName.equalsIgnoreCase("Test") && password.equalsIgnoreCase("[email protected]"))

if both are matching then print logged in else unable to login.

login page in java swing
Fig: Login Page in Java Swing

Login is check with different user id and password with its output in console is shown below.

Console Result for different userid and password input
Fig: Console Result for different userid and password input

Our code is working properly.

Now we will validate user id and password field.

Adding Validation for userid and password

For validation created a class Validation.java in login.frames package.

For validation we are considering following rules

  1. User id and password must not be empty.
  2. User id and password must have minimum 4 characters
  3. User id and password must have maximum 20 characters

Here Done only basic validation. Not validating valid mail id .

Later or you can also use login with mobile number or any other unique id.

According to this rule we have written following validateLogin().

This will accept userid and password and check for above conditions.

validateLogin() returns err ArrayList and that list is displayed in LoginFrame.

Updating LoginFrame to add validation

Here we created object of Validation and called validateLogin().

If any error occurs then errors are displayed with

JOptionPane.showMessageDialog(null, errors.toArray());

Output for LoginFrame validation is as below

Validation in login form
Fig: Userid and password validation in login form

Checking user id and password from MySQL database

Create MySQL database and table and insert value that is given below

Student Table Description
Fig: Student Table Description

Model to Assing JDBC value to Student Object

Student.java is added in login.model

This contains basic fields of students and getter setter and toString().

Object of this class is created in LoginDAO.java to assign value that are extracted from student table.

Establishing Connection with MySQL Database

ConnectionFactory.java is used to connect MySQL database with username and password and return Connection object.

Following code is how to connect MySQL database with java

Accessing Student details from MySQL Database

Following JDBC program (LoginDAO.java) is used to get student details from database.

For this we used PreparedStatement of JDBC.

Here we fetch data from database in java and assigned to student object.

Here we checked user detail with mail id, you can use with mobile number or with both.

Comparing user id and password from DAO in LoginFrame.java

  1. Create object for LoginDAO
  2. call checkLogin(userName) to check whether user exists or not.
  3. If user exists then student.getId() will not be zero.
  4. Compare user password with database stored password using BCrypt.
  5. If userid and password is correct then show HomeFrame
  6. else show appropriate message

Complete Code for HomeFrame.java is

Showing HomeFrame after Successful Login

In LoginFrame.java if userid and password is correct then we added setVisible(true) for HomeFrame and dispose() the LoginFrame as below

new HomeFrame().setVisible(true); 
this.dispose();

HomeFrame.java is create to login.frames this frame is shown after successfully login.

Fig: User Login Example
Java Home Frame after login
Fig: Redirected to home Frame after successful login

Login form in java swing with source code. Download The Complete Project with Source code

Read More

  1. JLabel in Java Swing
  2. JComboBox in Java Swing
  3. JTable in Java Swing
  4. JTable Pagination in Java JDBC
  5. Registration Form in Java Swing
  6. Simple Calculator in Java Applet
  7. Applet Life Cycle in Java

JavaScript Multiplication Table Program with HTML CSS

JavaScript Multiplication Table is a multiplication table for any number.

Using JavaScript we program and generate multiplication table of any number.

Java multiplication table generation using for loop is developed as below

  1. Create a text box to get a number from user.
  2. Create a button to submit value
  3. on button click call the multiplicationTable() function.
  4. function will get value of text box
  5. loop to generate multiplication table.
  6. print the data in html page.
Javascript Multiplication table

Generating Multiplication table in Range

Here user will enter range to generate multiplication table if user enters 1 and 10 then multiplication table from 1 to 10 is generated.

Generate Multiplication table in Range

Read More

  1. Get multiple checkbox value in javascript
  2. Add active class onclick
  3. isPrime() Javascript : Prime number program
  4. Form Handling and Validation
  5. Try Catch and Finally in JavaScript
  6. Math Object in JavaScript

isPrime() Javascript : Prime number program in Javascript

Prime number Program

isPrime() Javascript represents a prime number checker function in Javascript.

Prime number is a number which is divisible by one and number itself.

like numbers 5,7,11,13,17, etc are prime numbers.

Prime number program in javascript using for loop

Below program uses for loop to check a number is prime or not.

Create a function isPrime() to check the number is prime or not.

  1. get the number
  2. check a number is divisible by 2 to num/2
  3. if divisible then not a prime number
  4. else it is a prime number.

In above program we have create isPrime() Javascript function.

Prime number program in JavaScript

Function is checking whether a number is prime or not.

To get text box value document.getElementById("num").value is used here document represents the html page and method getElementById is accepting a html elements id.

based on it is finding its value.

To show result in paragraph element with id result document.getElementById("result").innerHTML is used.

Read More

  1. Get multiple checkbox value in javascript
  2. Add active class onclick
  3. JavaScript Multiplication Table
  4. Form Handling and Validation
  5. Try Catch and Finally in JavaScript
  6. Math Object in JavaScript