java 如何将从数据库中检索到的字符串值与程序中定义的字符串值进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12823153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:21:40  来源:igfitidea点击:

how to compare String values retrieved from database to the String value Defined in Program

javamysqlstringcomparison

提问by Ram

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.

我有一个程序,我必须在其中从数据库中检索数据。我以字符串的形式检索它。此字符串包含大约 100 个值。现在我必须将这些值与我给出的 String 的值进行比较。如果值匹配,则它应该执行 if 条件,否则不执行。

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....

我正在使用,if(AreaCode.equals(str))但它不起作用......

回答by Andy Hedges

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.

从您的问题来看,您似乎只是在比较第一条记录,您需要做的是迭代上述每个结果并进行比较。找到记录后,您可以使用 break 语句退出循环。

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.

当然,更好的解决方案是使用包含 areaCode 的 where 子句进行选择,如果你得到一条记录,那么它就在那里,如果没有,你就知道它不是。

Something like

就像是

select count(0) as tot from your_table_name where AreaCode = '9035'

and then with the ResultSet of that do

然后用那个做的 ResultSet

if(rs.getInt("AreaCode") != 0){
  //found the/a match
}

回答by Susie

If your Areacode contains 100 records, Areacode is some kind of collection, I assume. You cannot compare a collection with string "str"

如果您的区号包含 100 条记录,我假设区号是某种集合。您不能将集合与字符串“str”进行比较

If Areacode is a collection, then iterate over the collection and then compare it with the 'str'

如果 Areacode 是一个集合,则遍历该集合,然后将其与“str”进行比较

This might help you get started.

这可能会帮助您入门。

Iterator itr = Areacode.iterator();
while(itr.hasNext()){
   if(AreaCode.equals(str)){
   //do something
   }
}

回答by urir

Instead of iterating - add another 'equal' condition to the query. It will be for sure easier and more efficient.

而不是迭代 - 向查询添加另一个“相等”条件。这肯定会更容易和更有效率。