SQL Query没有结果如何告诉我的程序中的用户

时间:2013-05-20 21:23:19

标签: java sql

我正在构建一个检查用户角色的程序。按下按钮时,它使用userID查询数据库的角色/组表。如果某人不在角色/组表中,那么他们就是请求者。我有以下代码,并想知道为什么最后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");
                        }

                    }

3 个答案:

答案 0 :(得分:3)

因为你有

empty = false;

在你的内心

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

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

在你的代码中,empty总是为false,所以它永远不会输入最后一个if。也许你打算保持空=真。