Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why do i keep getting the "Username and Password Incorrect" message dialog.

In my Employee table there are :
EID, FirstName, Surname, Username, Password , email



Java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

class frame2 extends JFrame implements ActionListener{
    JTextField username ;
    JTextField password ;
    JButton login ;
    JLabel UserLabel ;
    JLabel PassLabel ;

    Connection connection = null;
    ResultSet rs = null;
    PreparedStatement pst = null;

public frame2(){
    super("Login:");
    
   
    setDefaultCloseOperation(JFrame.EXIT_ON_­CLOSE);
    setSize(500,500);
    GridBagLayout g = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    setLayout(g);
    
    c.gridx = 0;
    c.gridy = 0;
    UserLabel = new JLabel("Username");
    add(UserLabel,c); 
    
    c.gridx = 2; //for username txtfield
    c.gridy = 0;
    username = new JTextField(15); //name of textfield 
    add(username,c); 
    
    c.gridx = 0; //for password label
    c.gridy = 1;
    PassLabel = new JLabel("Password");
    add(PassLabel,c); 
    
    c.gridx = 2; //for username txtfield
    c.gridy = 1;
    password = new JTextField(15);
    add(password,c);
    
    c.gridx = 2;//login button 
    c.gridy = 3;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(10,0,0,0);  //top padding
    login = new JButton("Login");
    add(login,c);

    
    login.addActionListener(this);
    setVisible(true);
}

public void actionPerformed(ActionEvent e) {
    if(e.getSource() == login){
 
        
        String sql = "select from Employee where Username=? and Password=?";
        try{
            pst = connection.prepareStatement(sql) ;
            pst.setString(1, username.getText());
            pst.setString(2, password.getText());
            
            rs = pst.executeQuery();
            if(rs.next()){
                 JOptionPane.showMessageDialog(null, "Username and Password Correct");
                 setVisible(false); //closes login page
            }
            
        }
        catch(Exception ex){
            setVisible(true); //frame is still visible
            JOptionPane.showMessageDialog(null, "Username and Password Incorrect");
        }
    }
   
}

    public static void main(String[] args){
        frame2 l = new frame2();
}

    
}
Posted
Updated 27-Oct-14 15:47pm
v5

Please, brief your complete issues than i can solve it.
 
Share this answer
 
Did you initialized
connection
?
From your code, I just see
connection = null;

That's why exception happened, and thus executed the line
JOptionPane.showMessageDialog(null, "Username and Password Incorrect");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900