【问题标题】:Compare field from previous row in JDBC ResultSet比较 JDBC ResultSet 中上一行的字段
【发布时间】:2014-01-30 03:18:45
【问题描述】:

我研究过找不到任何相关信息。我有一个结果集,可以返回不同的 tagId,它们可以是同一个 accountId 的多个 tagId。

while(result_set.next()){
   String tagId = result_set.getString("tagId");
   String accountId = result_set.getString("accoundId");
   // plenty of other fields being store locally
}

我需要存储第一个 accoundId(正在完成)& 每次后续迭代都将其与前一个 Id 进行比较以检查是否相等(如果是相同的帐户)。

我试过这个,但它失败了,在第一次迭代之后它们会一直相等,我必须是 DUMB bc i 虽然只要我在分配之前比较它们 global guy(previousId) 应该保持先前的值。

String previousId = null;  
while(result_set.next()){
   String tagId = result_set.getString("tagId");
   String accountId = result_set.getString("accoundId");
   previousId = accountId;
}

无论如何,我希望我的工作流程如下:

 while(result_set.next()){
     if (previousId = null) {
         // this would be the first iteration
     }
     else if (previousId.equals(accountId) {
        // go here
     } else {
      // go here
 }

}

【问题讨论】:

    标签: java jdbc resultset


    【解决方案1】:

    如果我理解你的话,这应该可以工作..

    String previousId = null;  
    while(result_set.next()){
       String tagId = result_set.getString("tagId");
       String accountId = result_set.getString("accoundId");
       if (previousId == null) {
         // this would be the first iteration
       } else if (previousId.equals(accountId) {
        // go here
       } else {
         // go here
       }
       previousId = accountId;
    }
    

    【讨论】:

    • 检查if (previousId = null) {会编译
    • @MGorgon +1 为您解答
    猜你喜欢
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多