0

I have this query( the parameters of the query are given by input: both table and name and password are string given by text field.

//...
ResultSet rs = st.executeQuery("select * " + 
                               "from `"+ table + "` " +
                               "where Name='" + name + "' " + 
                               "  and Password='" + password + "'");

and after this i don't understand why i can't enter in this if:

else if (table=="products"){
    // ...
} 

and therefore I can not go along with the program

8
  • Are you encountering any error or exception? Commented Jan 18, 2014 at 18:44
  • Any exceptions been thrown that you may want to share? Commented Jan 18, 2014 at 18:44
  • 1
    where starts the if statement? Commented Jan 18, 2014 at 18:44
  • While you're at it, please read up on prepared statements, putting user strings into SQL is something you need to know to never do. Commented Jan 18, 2014 at 18:44
  • 2
    Use String.equals to compare strings Commented Jan 18, 2014 at 18:45

3 Answers 3

0

The == operator compares hash values of the two objects. Only if they both have the same hash value will it return true. Always use the str1.equals(str2) method to compare Strings.

Also using prepared statements will help prevent SQL injection, a dangerous form of hacking. See here for an example of why this is dangerous.

0

your ans:

 you need to use String.equals

Suggestion:

 please use PREPARED STATEMENT. Simple statement is not secured as well as for other reason its not good to use.
0

When you do if(table == "products"), Java will do a deep comparison of the two strings and since the two string objects are different (even though they have the same text), the code section will not execute as the condition evaluates to false. You could try table.equals("products"). This will just compare the text of the two strings.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.