954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

always return false??

would anyone please take a quick look? this always return false for some reason... the database connection is fine and the query is fine... it actually returned the right password from the database. please see below

public boolean passCheck(String login, String pass)
	{
		String realPass="";
		
		try
		{
			String query ="SELECT password FROM admin_user where login_name='"+login+"'";
			resultSet = statement.executeQuery(query);
            while ( resultSet.next() ) 
            {
                realPass = resultSet.getString("password");
                System.out.println(realPass);
            }
			
		}
		catch(SQLException sqlex)
		{
			sqlex.printStackTrace();
		}
                // the returned password is correct
		System.out.println("from db pass is "+realPass);
		// this matches the password from above
                System.out.println("entered pass is "+pass);
		if(pass == realPass)
		{
			return true;
		}
		
		else
		{
			return false;
		}
	}


please point out why this function always return "false" ... thanks very much

k2k
Posting Whiz
352 posts since Nov 2007
Reputation Points: 15
Solved Threads: 1
 

always forgot string has to be compared by .equals() method....

k2k
Posting Whiz
352 posts since Nov 2007
Reputation Points: 15
Solved Threads: 1
 

Try this and let me now:

public boolean passCheck(String login, String pass) {
        boolean found=false;
        try {
            String query = "SELECT password FROM admin_user where login_name='" + login + "'";
            // resultSet = statement.executeQuery(query);
            while (resultSet.next()) {
                realPass = resultSet.getString("password");
                System.out.println(realPass);


                if (pass == realPass) {
                    found =true;
                    break;
                } 
                
            }

        } catch (SQLException sqlex) {
            sqlex.printStackTrace();
        }
        // the returned password is correct
        System.out.println("from db pass is " + realPass);
        // this matches the password from above
        System.out.println("entered pass is " + pass);
        /*if (pass == realPass) {
        return true;
        } else {
        return false;
        }*/
        return found;
    }
moutanna
Posting Whiz
387 posts since Oct 2009
Reputation Points: 16
Solved Threads: 58
 

Try this and let me now:

...
                if (pass == realPass) {
                    found =true;
                    break;
                } 
   ...

hmmm... why this answer? not only did k2k found (and posted) the right answer himself, but your answer is wrong.
if you use Java on a regulary basis, you should know by now that it is an Object Oriented language, that a String is an Object and that Objects are compared not by '==' but by the .equals(Object b) method.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You