0% found this document useful (0 votes)
38 views3 pages

Untitled Document

Good problem

Uploaded by

officialgagan001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views3 pages

Untitled Document

Good problem

Uploaded by

officialgagan001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

package lab;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class LoginForm extends JFrame {

private static final String DB_URL = "jdbc:mysql://localhost:3306/user_db";


private static final String USER = "root";
private static final String PASS = "root";

public LoginForm() {
setTitle("Login Form");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

// Create panels
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(7, 2));

// Create labels and text fields


JLabel firstNameLabel = new JLabel("First Name:");
JTextField firstNameField = new JTextField();

JLabel lastNameLabel = new JLabel("Last Name:");


JTextField lastNameField = new JTextField();

JLabel emailLabel = new JLabel("Email:");


JTextField emailField = new JTextField();

JLabel usernameLabel = new JLabel("Username:");


JTextField usernameField = new JTextField();

JLabel mobileLabel = new JLabel("Mobile:");


JTextField mobileField = new JTextField();

JLabel passwordLabel = new JLabel("Password:");


JPasswordField passwordField = new JPasswordField();
// Create a submit button
JButton submitButton = new JButton("Submit");

// Add action listener for the button


submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String firstName = firstNameField.getText();
String lastName = lastNameField.getText();
String email = emailField.getText();
String username = usernameField.getText();
String mobile = mobileField.getText();
String password = new String(passwordField.getPassword());

// Insert data into the database


insertUser(firstName, lastName, email, username, mobile, password);
}
});

// Add components to panel


panel.add(firstNameLabel);
panel.add(firstNameField);
panel.add(lastNameLabel);
panel.add(lastNameField);
panel.add(emailLabel);
panel.add(emailField);
panel.add(usernameLabel);
panel.add(usernameField);
panel.add(mobileLabel);
panel.add(mobileField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(new JLabel("")); // Empty label for spacing
panel.add(submitButton);

// Add panel to frame


add(panel);
}

private void insertUser(String firstName, String lastName, String email, String username,
String mobile, String password) {
String query = "INSERT INTO users (first_name, last_name, email, username, mobile,
password) VALUES (?, ?, ?, ?, ?, ?)";
try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement pstmt = conn.prepareStatement(query)) {

pstmt.setString(1, firstName);
pstmt.setString(2, lastName);
pstmt.setString(3, email);
pstmt.setString(4, username);
pstmt.setString(5, mobile);
pstmt.setString(6, password);

int rowsAffected = pstmt.executeUpdate();


if (rowsAffected > 0) {
JOptionPane.showMessageDialog(this, "User registered successfully!");
} else {
JOptionPane.showMessageDialog(this, "Registration failed.");
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Database error: " + e.getMessage());
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
LoginForm loginForm = new LoginForm();
loginForm.setVisible(true);
});
}
}

You might also like