Reputation: 45
I have a program in which I have to retrieve data from database. I have retrieved it in the form of String. This String Contains Some 100 Values. Now I have to compare these values with the values of the String given by me . If The values matches then it should execute the if condition, otherwise not.
here is my code...
String str = "9035"; Manually Given by me.
AreaCode = rs.getString("AreaCode"); Retrieved from database with 100 records..
I am using if(AreaCode.equals(str))
but it's not working....
Upvotes: 2
Views: 12885
Reputation: 5148
If your Areacode contains 100 records, Areacode is some kind of collection, I assume. You cannot compare a collection with string "str"
If Areacode is a collection, then iterate over the collection and then compare it with the 'str'
This might help you get started.
Iterator itr = Areacode.iterator();
while(itr.hasNext()){
if(AreaCode.equals(str)){
//do something
}
}
Upvotes: 2
Reputation: 2025
Instead of iterating - add another 'equal' condition to the query. It will be for sure easier and more efficient.
Upvotes: 0
Reputation: 301
The way to debug this is as follows:
String str = "9035";
while (rs.next()) {
String areaCode = rs.getString("AreaCode");
if(str.equals(areaCode)){
System.out.println("!!!!!!It matched: " + str);
break;
} else {
System.out.println("No match with: " + areaCode);
}
}
From you question it looks like you are just comparing the first record, what you need to do is iterate through each result as above and compare. Once you find the record you exit the loop with the break statement.
Of course a better solution is to do a select using a where clause that include the areaCode, if you get a record back then it is in there, if not, the you know it isn't.
Something like
select count(0) as tot from your_table_name where AreaCode = '9035'
and then with the ResultSet of that do
if(rs.getInt("AreaCode") != 0){
//found the/a match
}
Upvotes: 4