【问题标题】:SQL Query has no results how do I tell that to a user in my programSQL 查询没有结果我如何告诉我程序中的用户
【发布时间】:2013-05-20 21:23:18
【问题描述】:

我正在构建一个检查用户角色的程序。当一个按钮被按下时,它使用用户 ID 来查询我们数据库的角色/组表。如果一个人不在角色/组表中,那么他们就是请求者。我有以下代码,想知道为什么最后一个 else if 语句不起作用。

当我们将一个人设置为请求者时,他们实际上会从角色/组表中删除。所以resultset 应该是空的。对吧?

ResultSet rs = stmt.executeQuery(getRolesQuery2.toString());
                try
                {
                    boolean empty = true;
                    while (rs.next())
                    {
                        empty = false;
                        userRole = rs.getInt(1);
                        userGroup = rs.getInt(2);
                        System.out.println("The Users Current Role/Group is: " +userRole+ "/" +userGroup);
                        if(userRole == 4 && userGroup == 1)
                        {
                            userRoleLbl.setText("The User Role/Group is: " +userRole+ "/" +userGroup+ " NDS Administrator");
                        }
                        else if(userRole == 1 && userGroup == 3)
                        {
                            userRoleLbl.setText("The User Role/Group is: " +userRole+ "/" +userGroup+ " Privacy Administrator");
                        }
                        else if(userRole == 1 && userGroup == 5)
                        {
                            userRoleLbl.setText("The User Role/Group is: " +userRole+ "/" +userGroup+ " Security Administrator");
                        }
                        else if(userRole == 1 && userGroup == 7)
                        {
                            userRoleLbl.setText("The User Role/Group is: " +userRole+ "/" +userGroup+ " ORD Administrator");
                        }
                        else if(userRole == 1 && userGroup == 9)
                        {
                            userRoleLbl.setText("The User Role/Group is: " +userRole+ "/" +userGroup+ " OEF-OIF Administrator");
                        }
                        else if(userRole == 1 && userGroup == 11)
                        {
                            userRoleLbl.setText("The User Role/Group is: " +userRole+ "/" +userGroup+ " Surgery Administrator");
                        }
                        else if(userRole == 1 && userGroup == 13)
                        {
                            userRoleLbl.setText("The User Role/Group is: " +userRole+ "/" +userGroup+ " Capri Administrator");
                        }
                        else if(userRole == 3 && userGroup == 15)
                        {
                            userRoleLbl.setText("The User Role/Group is: " +userRole+ "/" +userGroup+ " DART Super User");
                        }
                        //TODO: Add some handling for if the role/group is not present which means they are a requester
                        else if(empty)
                        {
                            userRoleLbl.setText("The User is a Requester");
                        }

                    }

【问题讨论】:

  • empty 在进入 while 循环时已设置为 false,并且不再设置,因此 if(empty) 永远不会为 true。
  • 如果没有结果,会不会完全进入while循环?
  • 我按照此处接受的答案中的步骤操作:stackoverflow.com/questions/2938812/…

标签: java sql


【解决方案1】:

因为你有

empty = false;

在你的时间里

你应该在 while 之外检查这个值,这样如果你的结果集是空的,它就是 true

         try
            {
                boolean empty = true;
                while (rs.next())
                {  
                  //your if else
                }

           if(empty)
              {
                userRoleLbl.setText("The User is a Requester");
              }
           }

【讨论】:

  • 删除 empty = false; 仍然没有任何结果。
  • 您必须将该检查放在 while 之外,因为如果您的结果集为空,它将不会进入 while 循环
【解决方案2】:

如果 ResultSet 没有结果,则根本不会进入 while() 循环。在进入循环之前,您应该检查是否没有结果。

【讨论】:

    【解决方案3】:

    在你的代码中空总是假的,所以它永远不会输入最后一个 if。也许您的意思是保留 empty = true。

    【讨论】:

      猜你喜欢
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-25
      相关资源
      最近更新 更多