This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JDBC Suggestion Needed  RSS feed

 
Koh Khai Huat
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys i need sugestion for this problem!!! Ok I have this few lines of code :

first i retrive all the data from my database then
i place all the data from the Colume "Used" in to a Array of string Name "Result[]"

The data in the Used Colume only have the Text of "Yes" And "No"

try{rs = ConnectionDB.st.executeQuery("Select * from LockerInfo");
while (rs.next())
{

result[l] = rs.getString("Used");
l ++;
}
}

Next i compare the data in the result array with the string "Yes".

But the program Keep returning me with a false answer.

if (result[i] == "Yes")
{
buttons[i].setBackground(Color.yellow);
}
else
{
System.out.println(result[i]);
buttons[i].setBackground(Color.red);
}

I dun know wat happen to this program which keep returning me a false Please give suggestion to slove this problem thanks alot.

regards
 
Abhinav Srivastava
Ranch Hand
Posts: 354
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use result[i].equals("Yes")
 
Koh Khai Huat
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks that really help

erm... if u dun mind can explain why this kind of problem will occur???
 
Abhinav Srivastava
Ranch Hand
Posts: 354
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A==B is checking whether A and B both point to the same object.
A.equals(B) depends on how the equals() method is implemented for the Object.
e.g. you might have two different String objects each having the value "Yes"
which is basically the problem you faced. So == returns false, however
.equals() works on values hence true.

For better understanding, read javadoc for Object.equals() and Object.hashCode()
 
Koh Khai Huat
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot for the explaination
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!