开发者

Need to change ResultSet.getString(Value)---- conditionally

开发者 https://www.devze.com 2023-02-19 08:53 出处:网络
Ihave a query and a resultset I do this while (rs.next()) { String string = rs.getString(ColumnName); if (String == \"certainvalu开发者_运维知识库e\")

I have a query and a resultset

I do this

while (rs.next())
{    
    String string = rs.getString(ColumnName);

    if (String == "certainvalu开发者_运维知识库e")
    {  
        //perform action
    }else {
       //do nothing  
    }

My problem is that the if condition doesn't seem to be working.... even though I know "certainvalue" is in the result set, it never evaluates to true, and it never performs the action---- I am confused as to why that is...

is it because i am using a while loop?? or is it because resultsets are just wierd,, ,what is going on???


Java can't compare strings with ==. What you have to do is use the equals method of the String.

if (string.equals("certainvalue")) {
    perform action
}


It looks you're using Java. In that case, the == operator compares if the two objects are the same, not if they represent the same value.

Rewrite the test :

if ("certainvalue".equals(string)) { doStuff(); }

(You might consider "a".equals(b) to be equivalent to b.equals("a"), but the first form protects you from a NullPointerException if there is no value for the row in the database.)


String is an object. You can't do a comparison that way (you are trying to compare object reference).

If you are use java(aren't you?) try:

String string = rs.getString(columnName);
if (string.compareTo("anotherString") == 0){
}

You can use operator == just for primitive types (like int).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号