Java Swing and MySQL Registration Form Application
Java Swing and MySQL Registration Form Implementation
This project demonstrates how to create a desktop user registration form in Java using the Swing GUI toolkit and MySQL database connectivity (JDBC). It provides input handling, field validation, password confirmation, and persistent storage via prepared statements.
Complete Java Source Code
package javamysqlconnect;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class RegistrationForm {
public static void main(String[] args) {
SwingUtilities.invokeLater(CreateRegistrationForm::new);
}
}
class CreateRegistrationForm extends JFrame implements ActionListener {
JTextField userIdInput, nameInput, addressInput, phoneInput, genderInput, emailInput;
JPasswordField passwordInput, confirmPasswordInput;
JButton submitBtn, resetBtn;
Connection connection;
CreateRegistrationForm() {
try {
String dbUrl = "jdbc:mysql://localhost:3306/bca_third_sem";
String dbUsername = "root";
String dbPassword = "";
connection = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
System.out.println("Connected with the database successfully");
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Database connection failed. Check console for details.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
setTitle("Registration Form");
setLayout(null);
setSize(500, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
userIdInput = new JTextField();
nameInput = new JTextField();
addressInput = new JTextField();
phoneInput = new JTextField();
genderInput = new JTextField();
emailInput = new JTextField();
passwordInput = new JPasswordField();
confirmPasswordInput = new JPasswordField();
submitBtn = new JButton("Submit");
resetBtn = new JButton("Reset");
submitBtn.addActionListener(this);
resetBtn.addActionListener(this);
addComponent("User ID:", userIdInput, 50);
addComponent("Name:", nameInput, 100);
addComponent("Address:", addressInput, 150);
addComponent("Phone:", phoneInput, 200);
addComponent("Gender:", genderInput, 250);
addComponent("Email:", emailInput, 300);
addComponent("Password:", passwordInput, 350);
addComponent("Confirm Password:", confirmPasswordInput, 400);
add(submitBtn).setBounds(100, 450, 100, 30);
add(resetBtn).setBounds(250, 450, 100, 30);
setVisible(true);
}
private void addComponent(String label, JComponent component, int y) {
JLabel lbl = new JLabel(label);
lbl.setBounds(50, y, 150, 30);
add(lbl);
component.setBounds(200, y, 200, 30);
add(component);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submitBtn) {
handleSubmit();
} else if (e.getSource() == resetBtn) {
resetForm();
JOptionPane.showMessageDialog(this, "Form has been reset.", "Info", JOptionPane.INFORMATION_MESSAGE);
}
}
private void handleSubmit() {
String userId = userIdInput.getText().trim();
String name = nameInput.getText().trim();
String address = addressInput.getText().trim();
String phone = phoneInput.getText().trim();
String gender = genderInput.getText().trim();
String email = emailInput.getText().trim();
String password = new String(passwordInput.getPassword()).trim();
String confirmPassword = new String(confirmPasswordInput.getPassword()).trim();
if (userId.isEmpty() || name.isEmpty() || address.isEmpty() || phone.isEmpty()
|| gender.isEmpty() || email.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()) {
JOptionPane.showMessageDialog(this, "All fields are required.", "Validation Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (!password.equals(confirmPassword)) {
JOptionPane.showMessageDialog(this, "Passwords do not match.", "Validation Error", JOptionPane.ERROR_MESSAGE);
return;
}
try (PreparedStatement preparedStatement = connection.prepareStatement(
"INSERT INTO users_data (userId, name, address, phone, gender, email, password) VALUES (?, ?, ?, ?, ?, ?, ?)")) {
preparedStatement.setString(1, userId);
preparedStatement.setString(2, name);
preparedStatement.setString(3, address);
preparedStatement.setString(4, phone);
preparedStatement.setString(5, gender);
preparedStatement.setString(6, email);
preparedStatement.setString(7, password);
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
JOptionPane.showMessageDialog(this, "User added successfully.", "Success", JOptionPane.INFORMATION_MESSAGE);
resetForm();
} else {
JOptionPane.showMessageDialog(this, "User could not be added. Please try again.", "Error", JOptionPane.ERROR_MESSAGE);
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error while inserting into the database. Check console for details.", "Database Error", JOptionPane.ERROR_MESSAGE);
}
}
private void resetForm() {
userIdInput.setText("");
nameInput.setText("");
addressInput.setText("");
phoneInput.setText("");
genderInput.setText("");
emailInput.setText("");
passwordInput.setText("");
confirmPasswordInput.setText("");
}
}Database Connection Details
The form connects to a local MySQL instance using JDBC:
- Database URL:
jdbc:mysql://localhost:3306/bca_third_sem - Username:
root - Password: Empty string (default for local development)
- Target Table:
users_data(columns: userId, name, address, phone, gender, email, and password)
Event Handling and Validation
The form supports input verification and action dispatching:
- Required Fields: Ensures all text fields and password fields are populated before performing database execution.
- Password Match Validation: Compares the password with confirmation input to ensure consistency.
- SQL Insertion: Uses
PreparedStatementto safely insert parameter values and guard against SQL injection. - Reset Action: Clears all form fields through the
resetForm()method upon clicking the Reset button.