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
- Create User Interface (UI) Using JFrame
- Collect all data in Model sent from UI
- Validate and Process Received data
- Saving data to MySql Database
Here we have added three external Jar files
- mysql-connector-jar-5.1.14-bin.jar – To connect Java program with MySql Database
- jBCrypt-0.4.jar – To encrypt password
- 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
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.
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 | package ebhor.frame; import javax.swing.JFrame; public class RegisterFrame extends JFrame { public RegisterFrame() { } public void setBounds() { } public void addComponents() { } public static void main(String[] args) { RegisterFrame frame = new RegisterFrame(); frame.setTitle("Student Register Form"); frame.setVisible(true); frame.setBounds(500, 100, 500, 700); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(true); } } |
Output of above code is as below
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
- Components are Declare as Instance variable
- Objects of components are created inside Default Constructor
- setBounds() is used to set position ,width and height of components.
- addComponents() is used to add components in Container.
- addActionListener() is used to add action listener on registerButton.
- actionPerformed() is over ridden to handle event
Last two steps are handled later on.
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 | package ebhor.frame; import java.awt.Container; import java.awt.Font; import javax.swing.*; public class RegisterFrame extends JFrame { JLabel message; JLabel nameLabel, dobLabel, genderLabel, dobFormat; JTextField nameField; JRadioButton genderMale, genderFemale; ButtonGroup genderGroup; JLabel mailIdLabel, mobileNoLabel; JTextField mailIdField, mobileNoField; JLabel passwordLabel, rePasswordLabel; JPasswordField passwordField, rePasswordField; JLabel programLabel; JComboBox<String> programList; JLabel branchLabel, semesterLabel; JComboBox<String> branchList; JComboBox<Integer> semesterList; JButton registerButton; Container container; public RegisterFrame() { message = new JLabel("Register a new Student"); message.setFont(new Font("Courier", Font.BOLD, 20)); nameLabel = new JLabel("Name"); nameField = new JTextField(); dobLabel = new JLabel("DOB"); genderLabel = new JLabel("Gender"); genderMale = new JRadioButton("Male", true); genderFemale = new JRadioButton("Female"); genderGroup = new ButtonGroup(); genderGroup.add(genderMale); genderGroup.add(genderFemale); mailIdLabel = new JLabel("Mail Id"); mailIdField = new JTextField(); mobileNoLabel = new JLabel("Mobile No"); mobileNoField = new JTextField(); passwordLabel = new JLabel("Password"); passwordField = new JPasswordField(); rePasswordLabel = new JLabel("Re Password"); rePasswordField = new JPasswordField(); programLabel = new JLabel("Courses"); programList = new JComboBox<String>(); programList.addItem("ME/M Tect"); programList.addItem("BE/B Tect"); programList.addItem("Diploma"); branchLabel = new JLabel("Branch"); branchList = new JComboBox<String>(); branchList.addItem("Computer Science and Engineering"); branchList.addItem("Electronics and Telecommunications"); branchList.addItem("Information Technology"); branchList.addItem("Electrical Engineering"); branchList.addItem("Electrical and Electronics Engineering"); branchList.addItem("Civil Engineering"); semesterLabel = new JLabel("Semester"); semesterList = new JComboBox<>(); semesterList.addItem(1); semesterList.addItem(2); semesterList.addItem(3); semesterList.addItem(4); semesterList.addItem(5); semesterList.addItem(6); semesterList.addItem(7); semesterList.addItem(8); registerButton = new JButton("Register"); container = getContentPane(); container.setLayout(null); setBounds(); addComponents(); } public void setBounds() { message.setBounds(50, 10, 600, 30); nameLabel.setBounds(50, 60, 100, 30); nameField.setBounds(130, 60, 200, 30); dobLabel.setBounds(50, 110, 100, 30); genderLabel.setBounds(50, 160, 100, 30); genderMale.setBounds(130, 160, 100, 30); genderFemale.setBounds(240, 160, 100, 30); mailIdLabel.setBounds(50, 210, 100, 30); mailIdField.setBounds(130, 210, 200, 30); mobileNoLabel.setBounds(50, 260, 100, 30); mobileNoField.setBounds(130, 260, 200, 30); passwordLabel.setBounds(50, 310, 100, 30); passwordField.setBounds(130, 310, 200, 30); rePasswordLabel.setBounds(50, 360, 100, 30); rePasswordField.setBounds(130, 360, 200, 30); programLabel.setBounds(50, 410, 100, 30); programList.setBounds(130, 410, 200, 30); branchLabel.setBounds(50, 460, 100, 30); branchList.setBounds(130, 460, 200, 30); semesterLabel.setBounds(50, 510, 100, 30); semesterList.setBounds(130, 510, 200, 30); registerButton.setBounds(130, 550, 200, 30); } public void addComponents() { container.add(message); container.add(nameLabel); container.add(nameField); container.add(dobLabel); container.add(genderLabel); container.add(genderMale); container.add(genderFemale); container.add(mailIdLabel); container.add(mailIdField); container.add(mobileNoLabel); container.add(mobileNoField); container.add(passwordLabel); container.add(passwordField); container.add(rePasswordLabel); container.add(rePasswordField); container.add(programLabel); container.add(programList); container.add(branchLabel); container.add(branchList); container.add(semesterLabel); container.add(semesterList); container.add(registerButton); } public static void main(String[] args) { RegisterFrame frame = new RegisterFrame(); frame.setTitle("Student Register Form"); frame.setVisible(true); frame.setBounds(500, 100, 500, 700); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(true); } } |
After adding basic components it will create Frame as below

C Add JDatePicker in JFrame
First check jdatepicker-1.3.4.jar is added in project
Imports for JDatePicker
1 2 3 4 | import javax.swing.JFormattedTextField.AbstractFormatter; import org.jdatepicker.impl.JDatePanelImpl; import org.jdatepicker.impl.JDatePickerImpl; import org.jdatepicker.impl.UtilDateModel; |
Adding JDatePicker in constructor
1 2 3 4 5 6 7 8 9 10 11 12 | /*Adding JDatePicker date picker*/ UtilDateModel model = new UtilDateModel(); model.setDate(1999, 01, 02); model.setSelected(true); Properties p = new Properties(); p.put("text.today", "Today"); p.put("text.month", "Month"); p.put("text.year", "Year"); datePanel = new JDatePanelImpl(model, p); datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter()); dobFormat = new JLabel("(yyyy-mm-dd)"); /*End Date picker*/ |
complete code after adding JDatePicker
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | package ebhor.frame; import java.awt.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Properties; import javax.swing.*; /*imports for JDatePicker*/ import javax.swing.JFormattedTextField.AbstractFormatter; import org.jdatepicker.impl.JDatePanelImpl; import org.jdatepicker.impl.JDatePickerImpl; import org.jdatepicker.impl.UtilDateModel; public class RegisterFrame extends JFrame { JLabel message; JLabel nameLabel, dobLabel, genderLabel, dobFormat; JTextField nameField; JRadioButton genderMale, genderFemale; ButtonGroup genderGroup; JLabel mailIdLabel, mobileNoLabel; JTextField mailIdField, mobileNoField; JLabel passwordLabel, rePasswordLabel; JPasswordField passwordField, rePasswordField; JLabel programLabel; JComboBox<String> programList; JLabel branchLabel, semesterLabel; JComboBox<String> branchList; JComboBox<Integer> semesterList; JButton registerButton; Container container; /*JDatePicker*/ JDatePanelImpl datePanel; JDatePickerImpl datePicker; public RegisterFrame() { message = new JLabel("Register a new Student"); message.setFont(new Font("Courier", Font.BOLD, 20)); nameLabel = new JLabel("Name"); nameField = new JTextField(); dobLabel = new JLabel("DOB"); //dobField = new JTextField(); /*Adding JDatePicker date picker*/ UtilDateModel model = new UtilDateModel(); model.setDate(1999, 01, 02); model.setSelected(true); Properties p = new Properties(); p.put("text.today", "Today"); p.put("text.month", "Month"); p.put("text.year", "Year"); datePanel = new JDatePanelImpl(model, p); datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter()); dobFormat = new JLabel("(yyyy-mm-dd)"); /*End Date picker*/ genderLabel = new JLabel("Gender"); genderMale = new JRadioButton("Male", true); genderFemale = new JRadioButton("Female"); genderGroup = new ButtonGroup(); genderGroup.add(genderMale); genderGroup.add(genderFemale); mailIdLabel = new JLabel("Mail Id"); mailIdField = new JTextField(); mobileNoLabel = new JLabel("Mobile No"); mobileNoField = new JTextField(); passwordLabel = new JLabel("Password"); passwordField = new JPasswordField(); rePasswordLabel = new JLabel("Re Password"); rePasswordField = new JPasswordField(); programLabel = new JLabel("Courses"); programList = new JComboBox<String>(); programList.addItem("ME/M Tect"); programList.addItem("BE/B Tect"); programList.addItem("Diploma"); branchLabel = new JLabel("Branch"); branchList = new JComboBox<String>(); branchList.addItem("Computer Science and Engineering"); branchList.addItem("Electronics and Telecommunications"); branchList.addItem("Information Technology"); branchList.addItem("Electrical Engineering"); branchList.addItem("Electrical and Electronics Engineering"); branchList.addItem("Civil Engineering"); semesterLabel = new JLabel("Semester"); semesterList = new JComboBox<>(); semesterList.addItem(1); semesterList.addItem(2); semesterList.addItem(3); semesterList.addItem(4); semesterList.addItem(5); semesterList.addItem(6); semesterList.addItem(7); semesterList.addItem(8); registerButton = new JButton("Register"); container = getContentPane(); container.setLayout(null); setBounds(); addComponents(); } public void setBounds() { message.setBounds(50, 10, 600, 30); nameLabel.setBounds(50, 60, 100, 30); nameField.setBounds(130, 60, 200, 30); dobLabel.setBounds(50, 110, 100, 30); /*JDatePicker*/ datePicker.setBounds(130, 110, 200, 30); dobFormat.setBounds(350, 110, 200, 30); genderLabel.setBounds(50, 160, 100, 30); genderMale.setBounds(130, 160, 100, 30); genderFemale.setBounds(240, 160, 100, 30); mailIdLabel.setBounds(50, 210, 100, 30); mailIdField.setBounds(130, 210, 200, 30); mobileNoLabel.setBounds(50, 260, 100, 30); mobileNoField.setBounds(130, 260, 200, 30); passwordLabel.setBounds(50, 310, 100, 30); passwordField.setBounds(130, 310, 200, 30); rePasswordLabel.setBounds(50, 360, 100, 30); rePasswordField.setBounds(130, 360, 200, 30); programLabel.setBounds(50, 410, 100, 30); programList.setBounds(130, 410, 200, 30); branchLabel.setBounds(50, 460, 100, 30); branchList.setBounds(130, 460, 200, 30); semesterLabel.setBounds(50, 510, 100, 30); semesterList.setBounds(130, 510, 200, 30); registerButton.setBounds(130, 550, 200, 30); } public void addComponents() { container.add(message); container.add(nameLabel); container.add(nameField); container.add(dobLabel); /*JDatePicker*/ container.add(datePicker); container.add(dobFormat); container.add(genderLabel); container.add(genderMale); container.add(genderFemale); container.add(mailIdLabel); container.add(mailIdField); container.add(mobileNoLabel); container.add(mobileNoField); container.add(passwordLabel); container.add(passwordField); container.add(rePasswordLabel); container.add(rePasswordField); container.add(programLabel); container.add(programList); container.add(branchLabel); container.add(branchList); container.add(semesterLabel); container.add(semesterList); container.add(registerButton); } public static void main(String[] args) { RegisterFrame frame = new RegisterFrame(); frame.setTitle("Student Register Form"); frame.setVisible(true); frame.setBounds(500, 100, 500, 700); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(true); } /*JDate Picker drop down*/ public class DateLabelFormatter extends AbstractFormatter { private String datePattern = "yyyy-MM-dd"; private SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern); @Override public Object stringToValue(String text) throws ParseException { return dateFormatter.parseObject(text); } @Override public String valueToString(Object value) throws ParseException { if (value != null) { Calendar cal = (Calendar) value; return dateFormatter.format(cal.getTime()); } return ""; } } } |
DateLabelFormatter class is used to show date selector.

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.
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | package ebhor.model; import java.io.Serializable; import java.sql.Date; import java.sql.Timestamp; public class Student implements Serializable{ private long id; private String name; private Date dob; private String gender; private String mailId; private String mobileNo; private String password; private String rePassword; private String encPassword;//Encrypted password private String program; private String branch; private int semester; private Timestamp addDate; public Student() { } public Student(String name, Date dob, String gender, String mailId, String mobileNo, String password, String rePassword, String program, String branch, int semester) { this.name = name; this.dob = dob; this.gender = gender; this.mailId = mailId; this.mobileNo = mobileNo; this.password = password; this.rePassword = rePassword; this.program = program; this.branch = branch; this.semester = semester; } public Student(long id, String name, Date dob, String gender, String mailId, String mobileNo, String password, String rePassword, String program, String branch, int semester, Timestamp addDate) { this.id = id; this.name = name; this.dob = dob; this.gender = gender; this.mailId = mailId; this.mobileNo = mobileNo; this.password = password; this.rePassword = rePassword; this.program = program; this.branch = branch; this.semester = semester; this.addDate = addDate; } @Override public String toString() { return "Student{" + "id=" + id + ", name=" + name + ", dob=" + dob + ", gender=" + gender + ", mailId=" + mailId + ", mobileNo=" + mobileNo + ", password=" + password + ", rePassword=" + rePassword + ", program=" + program + ", branch=" + branch + ", semester=" + semester + ", addDate=" + addDate + '}'; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getDob() { return dob; } public void setDob(Date dob) { this.dob = dob; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getMailId() { return mailId; } public void setMailId(String mailId) { this.mailId = mailId; } public String getMobileNo() { return mobileNo; } public void setMobileNo(String mobileNo) { this.mobileNo = mobileNo; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getRePassword() { return rePassword; } public void setRePassword(String rePassword) { this.rePassword = rePassword; } public String getProgram() { return program; } public void setProgram(String program) { this.program = program; } public String getBranch() { return branch; } public void setBranch(String branch) { this.branch = branch; } public int getSemester() { return semester; } public void setSemester(int semester) { this.semester = semester; } public Timestamp getAddDate() { return addDate; } public void setAddDate(Timestamp addDate) { this.addDate = addDate; } public String getEncPassword() { return encPassword; } public void setEncPassword(String encPassword) { this.encPassword = encPassword; } } |
Adding following code to RegisterFrame.java
- ActionLister is added for registerButton
- 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.
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 | public void addActionListener() { registerButton.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == registerButton) { System.out.println("Register Button Clicked"); String gender = null; if (genderFemale.isSelected()) { gender = "Female"; } if (genderMale.isSelected()) { gender = "Male"; } String programName = programList.getSelectedItem().toString(); String branchName = branchList.getSelectedItem().toString(); int semesterNo = Integer.parseInt(semesterList.getSelectedItem().toString()); String dobString = datePicker.getJFormattedTextField().getText(); if (dobString.isEmpty()) { JOptionPane.showMessageDialog(null, "Date of Birth is Empty"); return; } Date dob = null; try { dob = Date.valueOf(dobString); } catch (IllegalArgumentException ex) { System.out.println("Exception " + ex); JOptionPane.showMessageDialog(null, "Date of birth format is in correct"); return; } System.out.println("name " + nameField.getText() + " dob " + dobString + " gender " + gender + " mailid " + mailIdField.getText() + " mobileNo " + mobileNoField.getText() + " password " + passwordField.getText() + " rePassword " + rePasswordField.getText() + " branch " + branchName + " semester " + semesterNo); Student student = new Student(nameField.getText(), dob, gender, mailIdField.getText(), mobileNoField.getText(), passwordField.getText(), rePasswordField.getText(), programName, branchName, semesterNo); } } |
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
- All fields are checked for Empty.
- Name length must be more than 4 and less than 20
- Validation for Mail id
- Mobile Number must be 10 digits long
- Password must be between 8 to 20 characters
- Password must contain One digit one upper case letter and special symbol
- RePassword and password must match
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 | package ebhor.frame; import ebhor.model.Student; import java.util.ArrayList; import java.util.List; public class Validation { public List<String> validateRegistration(Student student) { ArrayList<String> err = new ArrayList<String>(); if (student.getName().isEmpty()) { err.add("Name can not be empty"); } else if (student.getName().length() < 4) { err.add("Name is too short"); } else if (student.getName().length() > 50) { err.add("Name is too long"); } else if (!isString(student.getName())) { err.add("Only characters allowed in name"); } if (student.getMailId().isEmpty()) { err.add("MailId can not be empty"); } else if (!isValidEmailAddress(student.getMailId())) { err.add("MailId is not valid"); } if (student.getMobileNo().isEmpty()) { err.add("Mobile Number can not be empty"); } else if (student.getMobileNo().length() != 10) { err.add("Mobile Number must be 10 digit long"); } else if (!isDigit(student.getMobileNo())) { err.add("Mobile Numbers must have only digits"); } if (student.getPassword().isEmpty()) { err.add("Password not be empty"); } else if (student.getPassword().length() < 8) { err.add("Password is too short"); } else if (student.getPassword().length() > 20) { err.add("Password is too long"); } else if (!isStrongPassword(student.getPassword())) { err.add("Enter Strong Password"); } if (student.getRePassword().isEmpty()) { err.add("Re Password can not be empty"); } else if (student.getRePassword().length() < 8) { err.add("Re Password is too short"); } else if (student.getRePassword().length() > 20) { err.add("Re Password is too long"); } if (!student.getPassword().equals(student.getRePassword())) { err.add("Both passwords are not matching"); } return err; } public boolean isValidEmailAddress(String email) { String ePattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$"; java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern); java.util.regex.Matcher m = p.matcher(email); return m.matches(); } //https://stackoverflow.com/a/15806080/876739 public boolean isString(String name) { String ePattern = "^[\\p{L} '-]+$"; java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern); java.util.regex.Matcher m = p.matcher(name); return m.matches(); } //https://stackoverflow.com/a/3802238/876739 public boolean isStrongPassword(String text) { String ePattern = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,}$"; java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern); java.util.regex.Matcher m = p.matcher(text); return m.matches(); } //https://stackoverflow.com/a/34253764/876739 public boolean isDigit(String mobileNo) { boolean digits = mobileNo.chars().allMatch(Character::isDigit); return digits; } } |
Creating object of validation and and checking for errors if error occurs then it is shown using showMessageDialog()
.
1 2 3 4 5 6 7 8 | Student student = new Student(nameField.getText(), dob, gender, mailIdField.getText(), mobileNoField.getText(), passwordField.getText(), rePasswordField.getText(), programName, branchName, semesterNo); student.setEncPassword(BCrypt.hashpw(student.getPassword(), BCrypt.gensalt())); Validation v = new Validation(); java.util.List<String> errors = v.validateRegistration(student); if (errors.size() > 0) { JOptionPane.showMessageDialog(null, errors.toArray()); return; } |
Processing Data
Processing data is done before and after validation
- Based on male and female JRadioButton we assigned male or female to gender variable.
- Get Dob from JDatePicker as string and then converted again to java.sql.Date.
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | CREATE TABLE student ( id BIGINT(20) UNSIGNED NOT NULL auto_increment, name VARCHAR(200), dob DATE, gender VARCHAR(10), mailid VARCHAR(100), mobile_no VARCHAR(12), password VARCHAR(200), program VARCHAR(100), branch VARCHAR(100), semester INT(2), add_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP(), PRIMARY KEY (id), UNIQUE KEY (mailid), UNIQUE KEY (mobile_no) ) engine=innodb DEFAULT charset=utf8; |
ConnectionFactory.java
This file is used to connect java with database and returns the connection object.
Database name :ebhor
Username: ebhor_user
Password: 21V6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package ebhor.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class ConnectionFactory { public static Connection getConnection() { Connection c = null; try { Class.forName("com.mysql.jdbc.Driver"); c = DriverManager.getConnection("jdbc:mysql://localhost:3306/ebhor?useUnicode=true&characterEncoding=UTF-8", "ebhor_user", "21V6"); } catch (ClassNotFoundException e) { System.out.println("ClassNotFoundException " + e); } catch (SQLException e) { System.out.println("SQLException " + e); } return c; } } |
RegisterDAO.java
registerStudent() takes a student object and save each field to database using java preparedStatement();
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 | package ebhor.dao; import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException; import ebhor.model.Student; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class RegisterDAO { Connection con = null; PreparedStatement ps = null; ResultSet rs = null; int st;//status public int registerStudent(Student student) { con = ConnectionFactory.getConnection(); try { String query = "insert into " + "student " + "(name,dob,gender,mailid,mobile_no,password,program,branch,semester) " + "values(?,?,?,?,?,?,?,?,?)"; ps = con.prepareStatement(query); ps.setString(1, student.getName()); ps.setDate(2, student.getDob()); ps.setString(3, student.getGender()); ps.setString(4, student.getMailId()); ps.setString(5, student.getMobileNo()); ps.setString(6, student.getEncPassword()); ps.setString(7, student.getProgram()); ps.setString(8, student.getBranch()); ps.setInt(9, student.getSemester()); st = ps.executeUpdate(); System.out.println("Inserted student " + st); } catch (MySQLIntegrityConstraintViolationException e) { /*This exception is throws when user is already registed with same id, mobileno or mail id*/ e.printStackTrace(); st = -1; } catch (SQLException e) { /*Any SqlException occures then this will execute*/ e.printStackTrace(); st = -2; } return st; } } |
RegisterFrame.java
Receiving response from registerStudent().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | RegisterDAO dao = new RegisterDAO(); int st = dao.registerStudent(student); System.out.println(st); if (st == 1) { JOptionPane.showMessageDialog(null, "Registered Successfully"); } if (st == -1) { JOptionPane.showMessageDialog(null, "Already Registered"); } if (st == -2) { JOptionPane.showMessageDialog(null, "OOps Unable to Register"); } |
Finally Saving data on student table


How to create a registration form in java swing
The above tutorial explains about to create a registration form in java swing.
How to create a registration form in java using netbeans
Follow the above steps to develop registration form
registration form in java source code download
Read More
- JLabel in Java Swing
- JComboBox in Java Swing
- JTable in Java Swing
- JTable Pagination in Java JDBC
- Login form in Java Swing
- Simple Calculator in Java Applet
- Applet Life Cycle in Java
- Java program to print vowels in a String