1

I am building a program which checks the roles of users. When a button gets pressed it uses the userID to query the role/group table of our database. If a person is not in the role/group tables then they are a requester. I have the following code, and was wondering why the last else if statement is not working.

When we set a person to be a requester, they are literally removed from the role/group tables. So the resultset should be empty. Right?

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");
                        }

                    }
DarthOpto
  • 1,640
  • 7
  • 32
  • 59

3 Answers3

3

Because you have

empty = false;

inside your while

You should check this value outside the while so that if your result set is empty it will be true

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

           if(empty)
              {
                userRoleLbl.setText("The User is a Requester");
              }
           }
NullPointerException
  • 3,732
  • 5
  • 28
  • 62
0

If the ResultSet has no results, it will not enter the while() loop at all. You should check for no results, before entering the loop.

The other other Alan
  • 1,868
  • 12
  • 21
0

In your code empty is always false, so it will never enter the last if. Maybe you meant to keep the empty = true.

docDevil
  • 72
  • 4