1

i was trying to validate my login using this code but it always shows invalid and i tried printing the value of rs variable which is true and there is no error or exception,Here is my code

String sql="SELECT * FROM user_info WHERE password=? and  email_id=?";
try
{
    ResultSet rs=null;
    PreparedStatement ps=mycon.prepareStatement(sql);
    ps.setString(1,text_eid.getText());
    ps.setString(2,passwordField.getText());
    rs=ps.executeQuery();


    System.out.println("rs="+rs.next());
    boolean b=rs.next();
    if(b==true)
    {
        JOptionPane.showMessageDialog(null, "suc");
    }
    else
    {
        JOptionPane.showMessageDialog(null, "invalid");

    }
}

catch(Exception er)
{
    er.printStackTrace();
}
1
  • 1
    WARNING: Never store plain-text passwords. Always use a password-specific hash like Bcrypt to store them. Commented Nov 29, 2019 at 16:59

3 Answers 3

1

The problem is you are trying to print rs.next() and there may be only one result. In this case it will print true but actually assign false in variable. Remove that sysout and log the assigned variable. Except this there is nothing wrong.

Sign up to request clarification or add additional context in comments.

Comments

1

As I see it, there are two problems with your code:

  1. Your SQL string uses parameter 1 for the password and parameter 2 for the email ID:

    String sql="SELECT * FROM user_info WHERE password=? and  email_id=?";
    //                                        param 1--^      param 2--^
    

    However, it seems you then proceed to pass the parameter values the other way around:

        ps.setString(1,text_eid.getText());
        ps.setString(2,passwordField.getText());
    
  2. As pointed out elsewhere, you are calling rs.next() twice. Each time you call this method, the result set attempts to move forward one row, and the method returns whether a row was read. So the following line tries to move the result set to the first row and prints whether there was a first row:

    System.out.println("rs="+rs.next());
    

    However the next line tries to move the result set forward another row, and sets b to whether the result set has a second row:

    boolean b=rs.next();
    

    I'm guessing that in your case the result set returned one row, so you got true printed out and b was set to false.

    A better way of writing this is as follows:

    boolean b=rs.next();
    System.out.println("rs="+b);
    

    In this case we only call rs.next() once. Once we've got the return value of this method in b, we can then just print out b without needing to call rs.next() again.

Comments

0

Try this:

int result = st.executeUpdate()

if (result == 0) {
        JOptionPane.showMessageDialog(null, "invalid");

} else {
        JOptionPane.showMessageDialog(null, "suc");

}

or you can create a User class to set his properties like this:

User u = new User();    
while (rs.next()) {
                    u.setId(rs.getString("email_id=?"));

                }


 if (u.getId() == null) {
            JOptionPane.showMessageDialog(null, "invalid");

    } else {
            JOptionPane.showMessageDialog(null, "suc");

    }

2 Comments

You can't use executeUpdate with a select, and using rs.getString("email_id=?") won't work because that is not a proper column name.
it was a idea of how it should work... only have to change the names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.