0% found this document useful (0 votes)
47 views

Import Javax

This Java code defines a class called MyLoginApp that extends JFrame and implements a simple login form interface. It contains fields for a username and password text field, a login button, and a method to authenticate the username and password against a database. When the login button is clicked, it retrieves the username and password, calls the authentication method, and displays a success or failure message accordingly.

Uploaded by

Mlondi Dlamini
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Import Javax

This Java code defines a class called MyLoginApp that extends JFrame and implements a simple login form interface. It contains fields for a username and password text field, a login button, and a method to authenticate the username and password against a database. When the login button is clicked, it retrieves the username and password, calls the authentication method, and displays a success or failure message accordingly.

Uploaded by

Mlondi Dlamini
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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.ResultSet;

import java.sql.SQLException;

public class MyLoginApp extends JFrame {

private JTextField usernameField;


private JPasswordField passwordField;

public MyLoginApp() {

super("Custom Login Form");

JPanel panel = new JPanel(new GridLayout(3, 2));

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

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

usernameField = new JTextField();

passwordField = new JPasswordField();

JButton loginButton = new JButton("Login");

loginButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

String username = usernameField.getText();

String password = String.valueOf(passwordField.getPassword());

if (authenticateUser(username, password)) {

JOptionPane.showMessageDialog(MyLoginApp.this, "Login successful!");

} else {

JOptionPane.showMessageDialog(MyLoginApp.this, "Login failed. Please check your


credentials.");

});

panel.add(usernameLabel);

panel.add(usernameField);

panel.add(passwordLabel);
panel.add(passwordField);

panel.add(new JLabel());

panel.add(loginButton);

getContentPane().add(panel, BorderLayout.CENTER);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(300, 150);

setLocationRelativeTo(null);

private boolean authenticateUser(String username, String password) {

try {

Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/Botho");

String query = "SELECT * FROM useradmin WHERE Username=? AND password=?";

PreparedStatement statement = conn.prepareStatement(query);

statement.setString(1, username);

statement.setString(2, password);

ResultSet result = statement.executeQuery();

if (result.next()) {

result.close();

statement.close();

conn.close();

return true;

} else {

result.close();

statement.close();

conn.close();

return false;

} catch (SQLException e) {
e.printStackTrace();

return false;

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

MyLoginApp loginApp = new MyLoginApp();

loginApp.setVisible(true);

});

You might also like