Reputation: 251
i have made a jframe for login page,where the user have to provide the username and password press login button to log in. for the validation i have given a if condition in the starting of the code in login_btnActionPerformed method, teh codes in the method are:
private void login_btnActionPerformed(java.awt.event.ActionEvent evt) {
String sql="select * from login where name=? and password=?";
if(name_field.getText()!=null && password_field.getText()!=null){
try{
pst=conn.prepareStatement(sql);
pst.setString(1, name_field.getText());
pst.setString(2, password_field.getText());
rs=pst.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(null, "username and password is correct");
close();
NewPage n=new NewPage();
n.setVisible(true);
}
else {
JOptionPane.showMessageDialog(null, "username and password is incorrect");
name_field.setText(null);
password_field.setText(null);
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e);
}
}
else
JOptionPane.showMessageDialog(null,"Please enter the username and password");
}
Now the problem is that when i am pressing the login button by keeping the the user_field and password_field blank ,then also its showing the popup(JoptionPane) dat username and password correct and its taking me to next jframe.. help me..
Upvotes: 0
Views: 98
Reputation: 20755
The way you are using the if
condition is not correct. As @alex2410 answer, you need to change the if
condition as given by him or you can do it like this:
Replace
if(name_field.getText()!=null && password_field.getText()!=null){
By
if(name_field.getText().trim().length()==0 && password_field.getText().trim().length()==0){
Upvotes: 0
Reputation: 134
check that the login table does not contain a row with name="" and password="" (empty values for those fields)
Upvotes: 0
Reputation: 10994
Change name_field.getText()!=null && password_field.getText()!=null
to !name_field.getText().isEmpty() && !password_field.getText().isEmpty()
. That helps.
Upvotes: 3