Question: IN java, I have to create a login (login.javafile) the following are. I need help in creating the login. starter file which authenticates users both
IN java, I have to create a login (login.javafile) the following are. I need help in creating the login.
starter file which authenticates users both admin and regular users.
For the first time you use the app, please enter in the hard coded admin credentials in the file namely admin and admin1 respectively for the user name and password. After the admin is authenticated the ticketsGUI window appears and any of your tables are created if necessary.
Further check out the conditional logic for the loginButton eventHandler which nicely checks over credentials and passes the user name of a regular user (verified against the user table using prepared statements) or denotes a user as Admin in the ticketsGui.java window that triggers on a successful login. Any unsuccessful login fires up a pop up message.
Of course once you know your users table has been created or your testing the sample users table out, feel free to login with the credentials listed in the userlist file or just
connect to the server via MySQLWorkbench to gain access to table record data.
II. Create Java files for the UI/UX
Create Login screen (user vs admin password)
Create ticket screen
Include menu items and other GUI elements as optionsfor user vs adminDecide on the functionality of your program-What does the user need to do?-What does the admin need to do?
The following is some of the code that has been done:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.mysql.jdbc.PreparedStatement;
public class Login {
// create instance fields for class
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JLabel namelabel;
private JLabel passwordLabel;
private JTextField userText;
private JPasswordField passwordText;
private JButton loginButton;
private JPanel controlPanel;
public Login() {
prepareGUI();
showTextFields();
}
private void prepareGUI() {
// instantiate objects
mainFrame = new JFrame("Login"); // title of window form
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("", JLabel.CENTER);
controlPanel = new JPanel();
// window frame settings
mainFrame.setSize(400, 400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.getContentPane().setBackground(Color.red);
mainFrame.setLocationRelativeTo(null);
// frame object settings
headerLabel.setText("Account Access");
statusLabel.setSize(350, 100);
// add frame objects to mainframe
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent wE) { // define a window close
// operation
System.exit(0);
}
});
}
private void showTextFields() {
// instantiate controls
namelabel = new JLabel("User ID: ", JLabel.RIGHT);
passwordLabel = new JLabel("Password: ", JLabel.CENTER);
userText = new JTextField(6);
passwordText = new JPasswordField(6);
loginButton = new JButton("Login");
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*
* Check credentials for various users
*
* Administrator is a super user to the ticket system
* Code below uses a default (temporary) hard coded admin user
* name/password for verification. You can change this setting
* if you like.
*/
// Create user friendly variable name to store user name from text box
String userName = userText.getText();
// convert characters from password field to string for input validation
String password = new String(passwordText.getPassword());
boolean adminFlag = false;
if (userName.equals("admin") && password.equals("admin1")) {
adminFlag = true;
// close of Login window
mainFrame.dispose();
// open up ticketsGUI file upon successful login
new ticketsGUI("Admin"); // establish role as admin via constructor call
}
/*
* match credentials from text fields with users table for a
* match for regular users
*/
else if (!adminFlag) {
Connection connect = Dao.getConnection();
String queryString = "SELECT uname, upass FROM jpapa_users where uname=? and upass=?";
PreparedStatement ps;
ResultSet results = null;
try {
// set up prepared statements to execute query string cleanly and safely
ps = (PreparedStatement) connect.prepareStatement(queryString);
ps.setString(1, userName);
ps.setString(2, password);
results = ps.executeQuery();
if (results.next()) { // verify if a record match exists
// in table
JOptionPane.showMessageDialog(null, "Username and Password exist");
// close of Login window
mainFrame.dispose();
// open up ticketsGUI file upon successful login
new ticketsGUI(userName); // establish role as
// regular user via
// constructor call
} else {
JOptionPane.showMessageDialog(null, "Please Check Username and Password ");
}
Please and thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
