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

Popular posts from this blog

Suppose that a data warehouse for Big-University consists of the following four dimensions: student, course, semester, and instructor, and two measures count and avg_grade. When at the lowest conceptual level (e.g., for a given student, course, semester, and instructor combination), the avg_grade measure stores the actual course grade of the student. At higher conceptual levels, avg_grade stores the average grade for the given combination. a) Draw a snowflake schema diagram for the data warehouse. b) Starting with the base cuboid [student, course, semester, instructor], what specific OLAP operations (e.g., roll-up from semester to year) should one perform in order to list the average grade of CS courses for each BigUniversity student. c) If each dimension has five levels (including all), such as “student < major < status < university < all”, how many cuboids will this cube contain (including the base and apex cuboids)?

Suppose that a data warehouse consists of the four dimensions; date, spectator, location, and game, and the two measures, count and charge, where charge is the fee that a spectator pays when watching a game on a given date. Spectators may be students, adults, or seniors, with each category having its own charge rate. a) Draw a star schema diagram for the data b) Starting with the base cuboid [date; spectator; location; game], what specific OLAP operations should perform in order to list the total charge paid by student spectators at GM Place in 2004?

Discuss classification or taxonomy of virtualization at different levels.