• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in servlet

 
Jonathon Stride
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so i was writing a servlet that has to do some string comparision (related to DB2 database query)

in the the dopost() method the following code is there :

String a="A";
out.println(rs.getString(3));

if(rs.getString("type1").equals(a)){

RequestDispatcher view=request.getRequestDispatcher("adminlogin.jsp");
view.forward(request, response);
}
else if("B".equals((String)rs.getString(3))){
RequestDispatcher view=request.getRequestDispatcher("customerlogin.jsp");
view.forward(request, response);
}
else if("C".equals((String)rs.getString(3))){
RequestDispatcher view=request.getRequestDispatcher("workerlogin.jsp");
view.forward(request, response);
}
else if("D".equals((String)rs.getString(3))){
RequestDispatcher view=request.getRequestDispatcher("publiclogin.jsp");
view.forward(request, response);
}
out.println(rs.getString("type1"));
out.println(a);
out.println(rs.getString("type1")==a);

the output , as seen in the browser is :

Processing.....!
A
A
A
false


somehow the "A" from the result set doesnt compare with "A" in the servlet

what is wrong here ?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


is supposed to be out.println(rs.getString("type1").equals(a));

Using == compares the objects references, not the logical equality of the objects.
 
Jonathon Stride
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
out.println(rs.getString("type1").equals(a));

still gives me output as false

when i print rs.getString("type1")
and a
separately , i get A as output
but when i compare them they are different ?

rs is a result set from a sql query on a db2 database ,type1 is a field of type VARCHAR

what exactly is the problem and is it related to the db or something
 
Paul Clapham
Sheriff
Pie
Posts: 20719
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathon Stride wrote:out.println(rs.getString("type1").equals(a));

still gives me output as false

when i print rs.getString("type1")
and a
separately , i get A as output
but when i compare them they are different ?

rs is a result set from a sql query on a db2 database ,type1 is a field of type VARCHAR

what exactly is the problem and is it related to the db or something


You didn't say how you were comparing them. Or did you mean that when your code compared them, they were different? If that's what you meant, then they were indeed different and you couldn't tell that by looking at... whatever it was you looked at. Perhaps there are leading or trailing whitespace that you didn't notice when you looked?
 
Jonathon Stride
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i printed both sides separately in firefox and they look the same

A and A
but the comaprision is giving false

as you say, if there are any trailing spaces , is there a way to remove them from the string before doing the comparision

 
Jonathon Stride
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LOL i made the table field length ( in db2 ) == 1 and the comparision is successful

you are right there were 9 whitespaces (earlier length was 10) in the result set column 3
and it wasnt comparing it successfully with the single letter "A"

thanks a lot for pointing that out


that said , is there a way i can remove the trailing spaces before comparing ?
 
Paul Clapham
Sheriff
Pie
Posts: 20719
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathon Stride wrote:that said , is there a way i can remove the trailing spaces before comparing ?


String has a trim() method.
 
krishna kanthgaru
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or store in DB , the trimmed version.

TRIM(myString)

i believe this would be better if you trailing spaces dont make sense in your application
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic