Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ResultSet Problem

 
Udegbunam Morah
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Statement stat = con.createStatement("select * from Employees where employee_id = 106");
ResultSet res = stat.excuteQuery();
String s = res.getString(1); // Returns a String object that encapsulates the String literal "Robert".
String a = "Robert";
if ( a.equals(s) )
{
// some code executes
}
else
{
// some code executes
}
Even though 's' is a reference to a String object that contains the value "Robert", when it is compared with the 'a' (which is also String variable that holds a reference to a String object which encapsulates a String literal 'Robert').
Why does the boolean expression in the 'If statement' evaluate to false???
Thanks in advance?
[ February 16, 2004: Message edited by: Udegbunam Morah ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the "if" is evaluating to false, the strings cannot both contain the exact value "Robert". Are they cased differently? Are there trailing spaces in one of them?
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear has hit the problem on the head. The field being returned from the database is padded with spaces to the width of the text field. Trim that, and everything should work fine.
Tom Blough
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic