【问题标题】:Select Specific value from multiple rows returned by Oracle database从 Oracle 数据库返回的多行中选择特定值
【发布时间】:2018-01-04 00:20:45
【问题描述】:

我正在尝试从数据库返回的值中比较特定值。
我从STORESTAFF 得到多行,但我正在尝试比较从表返回的值之一..

contains() and 
equals() 

不工作。

String qry = "Select all employe_id from storestaff where post='Admin'";
pstmnt = conn.prepareStatement(qry);
ResultSet rs2 = pstmnt.executeQuery(qry);
if (rs2.next()) {
    String aa = rs2.getString("employe_id");

    if (aa.contains(UN.getText())) {
        this.aa = aa;
        JOptionPane.showMessageDialog(null, "Exists");
    } else {
        JOptionPane.showMessageDialog(null, "Username doesn't Exist");
    }
}

谁能建议我更好的解决方案,或者可以告诉我更好的解决方案?

【问题讨论】:

  • 是的,我的prblm已经解决了

标签: java database jdbc netbeans oracle11g


【解决方案1】:

如果您使用if,它将返回第一个值,在您的情况下,您必须使用while,该循环会抛出所有结果,因此您可以检查多个结果,而不是您可以使用:

boolean check = false;
while (rs2.next()) {
    String aa = rs2.getString("employe_id");
    if (aa.contains(UN.getText())) {
        this.aa = aa;
        check = true;//if the value exist change the variable to true
        break;       //and break the loop
    }
}

//when you end you have to check the value, if true then the user exist else no
if (check) {
    JOptionPane.showMessageDialog(null, "Exists");
} else {
    JOptionPane.showMessageDialog(null, "Username doesn't Exist");
}

【讨论】:

    【解决方案2】:

    if(rs2.next()) 更改为 while(rs2.next()) 您正在获取批量数据,因此您需要对其进行迭代以比较您想要的值之一

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多