Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginScreen extends JFrame implements Observer {
- private JLabel lblTitle, lblUsername, lblPassword;
- private JTextField txtVanReg;
- private JPasswordField txtPassword;
- private JPanel pnlCenter, pnlNorth;
- private JButton btnLogin, btnCancel;
- private final Font fntOther = new Font("Verdana", Font.PLAIN, 16);
- private final Font fntTitle = new Font("Verdana", Font.PLAIN, 20);
- private LoginController controller;
- public LoginScreen() {
- this.controller = new LoginController(this);
- this.setTitle("Login");
- this.setLayout(new BorderLayout());
- setUpComponents();
- }
- private void setUpComponents() {
- lblTitle = new JLabel("Burrito Business");
- lblUsername = new JLabel("Van reg: ");
- lblPassword = new JLabel("Password: ");
- txtVanReg = new JTextField();
- txtPassword = new JPasswordField();
- btnLogin = new JButton("Login");
- btnCancel = new JButton("Cancel");
- btnLogin.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- controller.loginRequested(txtVanReg.getText(), txtPassword.getPassword());
- }
- });
- btnCancel.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- controller.loginCancelled();
- }
- });
- pnlCenter = new JPanel(new GridLayout(3, 3));
- pnlNorth = new JPanel(new BorderLayout());
- pnlCenter.add(lblUsername);
- pnlCenter.add(txtVanReg);
- pnlCenter.add(lblPassword);
- pnlCenter.add(txtPassword);
- pnlCenter.add(btnLogin);
- pnlCenter.add(btnCancel);
- pnlNorth.add(lblTitle);
- setFonts();
- this.add(pnlNorth, BorderLayout.NORTH);
- this.add(pnlCenter, BorderLayout.CENTER);
- this.setMinimumSize(new Dimension(400, 150));
- this.setResizable(false);
- }
- private void setFonts() {
- lblTitle.setFont(fntTitle);
- lblUsername.setFont(fntOther);
- lblPassword.setFont(fntOther);
- txtVanReg.setFont(fntOther);
- txtPassword.setFont(fntOther);
- btnLogin.setFont(fntOther);
- btnCancel.setFont(fntOther);
- }
- /*
- * (non-Javadoc)
- *
- * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
- */
- public void update(Observable o, Object arg) {
- if (o instanceof DBUserAuth && ((Boolean) arg).booleanValue() == true) {
- JOptionPane.showMessageDialog(new JFrame(), "Login successful");
- } else {
- JOptionPane.showMessageDialog(new JFrame(), "Login failed");
- }
- }
- public char[] getPassword() {
- return this.txtPassword.getPassword();
- }
- public String getVanReg() {
- return this.txtVanReg.getText();
- }
- public void close() {
- this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
- }
- public class LoginController {
- private DBUserAuth dbConnection;
- private LoginScreen view;
- public LoginController(LoginScreen view) {
- dbConnection = new DBUserAuth();
- this.view = view;
- dbConnection.addObserver(view);
- }
- public void loginRequested(String van_reg, char[] password) {
- String pass = "";
- for (char a : password) {
- pass += a;
- }
- if (dbConnection.checkUser(van_reg, pass)) {
- StaffMenu aMenu = new StaffMenu(van_reg);
- aMenu.setVisible(true);
- view.close();
- }
- }
- public void loginCancelled() {
- view.close();
- }
- public class DBUserAuth extends Observable {
- private Properties props;
- private Connection con;
- private String url, password, username;
- private PreparedStatement loginAuthStatement, addTruckStatement;
- private ResultSet results;
- /**
- * Loads the properties file and gets the url, username and the password
- * from the file. Then goes on to open a connection to the database.
- */
- public DBUserAuth() {
- // 1. Load properties in
- props = new Properties();
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- InputStream stream = loader.getResourceAsStream("prop.properties");
- try {
- props.load(stream);
- url = props.getProperty("db.url");
- username = props.getProperty("db.username");
- password = props.getProperty("db.password");
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("Failed to load properties");
- }
- }
- /**
- * Used to connect to the DB
- *
- * @return True if success, false otherwise
- */
- private boolean openConnection() {
- try {
- con = DriverManager.getConnection(url, username, password);
- // connect to the db
- System.out.println("Connected to the DB");
- return true;
- } catch (SQLException e) {
- e.printStackTrace();
- System.out.println("Failed to connect to the DB");
- return false;
- }
- }
- /**
- * Used to close connection to the DB
- *
- * @return True if success, false otherwise
- */
- private boolean closeConnection() {
- try {
- con.close();
- return true;
- } catch (SQLException e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- *
- * @param password
- * the proposed password i.e. the password to check
- * @return
- */
- public boolean checkUser(String username, String password) {
- long time13 = System.currentTimeMillis();
- long time14;
- try {
- openConnection();
- loginAuthStatement = con.prepareStatement(
- "SELECT Van_reg_no, Hashed_Password FROM Food_Truck WHERE Van_reg_no LIKE ? ORDER BY Van_reg_no ASC");
- loginAuthStatement.setString(1, "%" + username + "%");
- results = loginAuthStatement.executeQuery();
- results.beforeFirst();
- while (results.next()) {
- String reg = results.getString(1);
- String hashPassword = results.getString(2);
- if (reg.equals(username) && BCrypt.checkpw(password, hashPassword)) {
- System.out.println("Login passed");
- time14 = System.currentTimeMillis();
- System.out.println("Total login time: " + (time14 - time13) + " miliseconds.");
- setChanged();
- notifyObservers(new Boolean(true));
- return true;
- }
- }
- } catch (SQLException e) {
- System.out.println("SQL for user auth. failed");
- e.printStackTrace();
- } finally {
- closeConnection();
- }
- setChanged();
- notifyObservers(new Boolean(false));
- time14 = System.currentTimeMillis();
- System.out.println("Total login time: " + (time14 - time13) + " miliseconds.");
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement