简体   繁体   中英

How to compare Resultset.getString() to String in Java?

I'm trying to compare a resultset to a string, but can't seem to get it to work. This is is what I'm trying:

if(resultset.getString("name").equals("George"))

When I printout resultset.getString("name") I get George. So they should be equal?

I also have a comparator using resultset.getInt("Age") that works, so I thought maybe I could be clever and use compareTo instead of equeals , but when I try,

if(resultset.getString("name").comparesTo("George"))

I was expecting to get 0, but I get 93???

What am I missing?

If .equals does not show them to be equal, then there is a difference. Could there be a space after one of them, or perhaps some other extra character?

Try printing both of them surrounded by a known character, maybe something like:

System.out.println("*" + resultset.getString("name") + "*");

Also, as a dummy check, make sure getString is actually returning type String. If this is a method you've defined, it could be returning something strange.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM