Write a java program to create login with user id, password, ok button, and cancel button. Handle key events such that pressing 'l' performs login and pressing 'c' clears text boxes and puts focus on user id text box. Assume user table having fields Uid and Password in the database named account. (10)
LoginFrame.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
/**
* JFrame to handle key events such that pressing 'l' performs login,
* ... and pressing 'c' clears text fields and puts focus on user ID
* ... text field.
*/
public class LoginFrame extends JFrame {
JLabel userIdLabel, passwordLabel;
JTextField userIdTextField;
JPasswordField passwordField;
JButton okBtn, cancelBtn;
JFrame self;
public LoginFrame() {
self = this;
userIdLabel = new JLabel("User ID");
passwordLabel = new JLabel("Password");
userIdTextField = new JTextField(20);
passwordField = new JPasswordField(20);
okBtn = new JButton("OK");
cancelBtn = new JButton("Cancel");
okBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new JFrame() {
{
JLabel messageLabel = new JLabel();
LoginService loginService = new LoginService();
String user = userIdTextField.getText();
String password = new String(passwordField.getPassword());
try {
if (loginService.isAuthenticated(user, password)) {
messageLabel.setText("Login Success!");
} else {
messageLabel.setText("Invalid credentials!");
}
} catch (SQLException ex) {
ex.printStackTrace();
messageLabel.setText(ex.getMessage());
} finally {
add(messageLabel);
setSize(200, 100);
setVisible(true);
}
}
};
}
});
/**
* Bring the focus back to the JFrame when a mouse click is registered
*/
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
self.requestFocus();
}
});
this.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == 'l') {
okBtn.doClick();
} else if (e.getKeyChar() == 'c') {
userIdTextField.setText("");
passwordField.setText("");
userIdTextField.grabFocus();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
this.setFocusable(true);
this.setSize(300,500);
this.setLayout(new FlowLayout(FlowLayout.LEFT));
this.add(userIdLabel);
this.add(userIdTextField);
this.add(passwordLabel);
this.add(passwordField);
this.add(okBtn);
this.add(cancelBtn);
this.setVisible(true);
}
public static void main(String[] args) {
new LoginFrame();
}
}
LoginService.java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class LoginService {
/**
* Retrieve a Connection instance after initializing the database connection
*/
Connection connection;
public LoginService() {
// Database initialization
// ...
// ...
}
/**
*
* @shikha user: user ID entered by the user from the login form
* @return true if user exists in database; otherwise false
*/
public boolean isValidUser(String user) throws SQLException {
String query = "SELECT Uid from user WHERE Uid = ?";
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, user);
ResultSet rs = ps.executeQuery();
return rs.next();
}
/**
*
* @shikha user: user ID entered by the user from the login form
* @shikha password: password entered by the user from the login form
* @return true if user ID and password combination exist as a record; otherwise false
* @throws SQLException
*/
public boolean isAuthenticated(String user, String password) throws SQLException {
if (!isValidUser(user)) return false;
/**
* TODO: Logic to hash password
*/
String hashedPassword = "";
String query = "SELECT Uid from user WHERE Uid = ? AND Password = ?";
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, user);
ps.setString(2, hashedPassword);
ResultSet rs = ps.executeQuery();
return rs.next();
}
}
Comments
Post a Comment