2 Replies - 2095 Views - Last Post: 03 May 2012 - 09:08 AM Rate Topic: -----

#1 AOM_Set  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 151
  • Joined: 18-December 10

MySQL, check for valid email and password (Using java)

Posted 02 May 2012 - 02:06 PM

Hi.
I have a method that should check the arguments that the user has given. In the database I have columns named email, and password (among others). How can I make the check if the arguments that the user has given are right? Here is what I tried, and I have a very strong feeling that this is very wrong. Can I in some way say: Select email and password from users (The table is called users)
public boolean returnValidCredentials(String emailParsed, String passwordParsed){
		try {
			rSet = statement.executeQuery("SELECT * FROM users");
			String emailRes = rSet.getString("email");
			String passwordRes = rSet.getString("password");
			
			while(rSet.next()){							
				if(!(emailRes.equalsIgnoreCase(passwordParsed) && passwordRes.equals(passwordParsed))){
					return false;
				}
			}			
		} catch (SQLException e) {
			displayMsg("Exception at method returnValidCredentials()");
		}		
		return true;
	}


I have a feeling that my code while break/return false after this first check that returns false, and it wont even check the rest. Even though, the true result might be right after it returned false

This post has been edited by AOM_Set: 02 May 2012 - 02:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: MySQL, check for valid email and password (Using java)

#2 AOM_Set  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 151
  • Joined: 18-December 10

Re: MySQL, check for valid email and password (Using java)

Posted 02 May 2012 - 02:12 PM

View PostAOM_Set, on 02 May 2012 - 02:06 PM, said:

						
if(!(emailRes.equalsIgnoreCase(passwordParsed) && passwordRes.equals(passwordParsed))){	


This should obviously say:
if(!(emailRes.equalsIgnoreCase(emailParsed) && passwordRes.equals(passwordParsed)))


Was This Post Helpful? 0
  • +
  • -

#3 IamTw_  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 238
  • Joined: 25-February 11

Re: MySQL, check for valid email and password (Using java)

Posted 03 May 2012 - 09:08 AM

Instead of returning false in the while-loop, you could make the check that return true there. That will make sure that you check all the values in the columns, and return false at the end.
Something like this:
public boolean returnValidCredentials(String emailParsed, String passwordParsed){
		try {
			rSet = statement.executeQuery("SELECT email, password FROM users");
			String emailRes = rSet.getString(1); //column 1
			String passwordRes = rSet.getString(2); //column 2
			
			while(rSet.next()){							
				if((emailRes.equalsIgnoreCase(emailParsed) && passwordRes.equals(passwordParsed))){
					return true;
				}
			}			
		} catch (SQLException e) {
			displayMsg("Exception at method returnValidCredentials()");
		}		
		return false;
	}



Was This Post Helpful? 1
  • +
  • -

Page 1 of 1