Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- import java.sql.*;
- public class AdvAccessTry extends JFrame implements ActionListener
- {
- Container cp;
- JTextField txtUsername,txtPassword,txtChangePass;
- JLabel lblTries,lblWelcome;
- JButton cmdLogin,cmdChangePass,cmdLogout;
- Connection con;
- int check = 3;
- String uName = "Guest";
- public AdvAccessTry()
- {
- super("Login");
- cp = getContentPane();
- cp.setLayout(new FlowLayout());
- lblWelcome = new JLabel("Welcome "+uName);
- cp.add(lblWelcome);
- txtUsername = new JTextField(20);
- txtPassword = new JTextField(20);
- cp.add(txtUsername);
- cp.add(txtPassword);
- lblTries = new JLabel("Remaining Tries: 3");
- cp.add(lblTries);
- cmdLogin = new JButton("Login");
- cp.add(cmdLogin);
- cmdLogin.addActionListener(this);
- cmdLogout = new JButton("Logout");
- cp.add(cmdLogout);
- cmdLogout.addActionListener(this);
- cmdLogout.setEnabled(false);
- txtChangePass = new JTextField(20);
- cp.add(txtChangePass);
- txtChangePass.setEnabled(false);
- cmdChangePass = new JButton("Change Password");
- cp.add(cmdChangePass);
- cmdChangePass.addActionListener(this);
- cmdChangePass.setEnabled(false);
- try
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- con = DriverManager.getConnection("jdbc:odbc:Trying");
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(this,"Error");
- System.out.println(e+"");
- }
- }
- public void actionPerformed(ActionEvent ae)
- {
- if(ae.getSource() == cmdLogin)
- {
- try
- {
- if(check>0)
- {
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM Access");
- int i = 0;
- while(rs.next())
- {
- if(txtUsername.getText().equals(rs.getString("Username")) && txtPassword.getText().equals(rs.getString("Password")))
- {
- i=1;
- uName = txtUsername.getText();
- }
- }
- if(i>0)
- {
- JOptionPane.showMessageDialog(this,"Access Granted");
- check = 3;
- txtChangePass.setEnabled(true);
- cmdChangePass.setEnabled(true);
- lblWelcome.setText("Welcome "+uName);
- txtUsername.setEnabled(false);
- txtPassword.setEnabled(false);
- cmdLogin.setEnabled(false);
- cmdLogout.setEnabled(true);
- lblTries.setEnabled(false);
- }
- else
- {
- JOptionPane.showMessageDialog(this,"Access Denied");
- check = check-1;
- txtChangePass.setEnabled(false);
- cmdChangePass.setEnabled(false);
- uName = "Guest";
- lblWelcome.setText("Welcome "+uName);
- lblTries.setEnabled(true);
- if(check == 0)
- {
- JOptionPane.showMessageDialog(this,"You used all three tries");
- txtUsername.setEnabled(false);
- txtPassword.setEnabled(false);
- cmdLogin.setEnabled(false);
- lblTries.setText("Tries Remaining: "+check);
- lblTries.setEnabled(false);
- }
- }
- lblTries.setText("Tries Remaining: "+check);
- }
- }
- catch(Exception e)
- {
- System.out.println(e.toString());
- JOptionPane.showMessageDialog(this,"Error");
- }
- }
- if(ae.getSource() == cmdLogout)
- {
- JOptionPane.showMessageDialog(this,"Logged out from "+uName);
- uName = "Guest";
- lblWelcome.setText("Welcome "+uName);
- txtUsername.setEnabled(true);
- txtPassword.setEnabled(true);
- cmdLogin.setEnabled(true);
- cmdLogout.setEnabled(false);
- txtChangePass.setText("");
- txtUsername.setText("");
- txtPassword.setText("");
- txtChangePass.setEnabled(false);
- cmdChangePass.setEnabled(false);
- lblTries.setEnabled(true);
- }
- if(ae.getSource() == cmdChangePass)
- {
- try
- {
- if((txtChangePass.getText()).length()>0)
- {
- Statement stmt= con.createStatement();
- int i = stmt.executeUpdate("UPDATE Access SET Password = '"+txtChangePass.getText()+"' WHERE Username = '"+uName+"'");
- JOptionPane.showMessageDialog(this,"Password Changed");
- txtPassword.setText(txtChangePass.getText());
- txtChangePass.setText("");
- }
- else
- JOptionPane.showMessageDialog(this,"Please enter a Password");
- }
- catch(Exception e)
- {
- JOptionPane.showMessageDialog(this,"Error Changing Password");
- System.out.println(e+"");
- }
- }
- }
- public static void main(String args[])
- {
- AdvAccessTry at = new AdvAccessTry();
- at.setSize(300,300);
- at.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement