-1

can anyone let me know why the part of if statement is not working? but else work perfectly? it should be ( if there is nothing inside administrator table which is (username=varchar,, password= varchar) then let the administrator register himself.

if (click == buttonAdmin) {
    Connection con =myConnection.getConnection();
    PreparedStatement ps;
    ResultSet rs;
    try {
        ps=con.prepareStatement("SELECT * FROM `adminstrator` ");
        rs=ps.executeQuery();

        while (rs.next()) {
         String username = rs.getString(1);
         String password = rs.getString(2);           

         if ( password.equals("") && username.equals("")) {
               new AdminNewRegister();                
         }
         else {
            new AdminLogin();
            System.out.println("else");
         }           
       }  
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

 }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
dana
  • 11
  • 2
  • change `rs.getString(1);` by `rs.getString("username");` the same with the password – Youcef LAIDANI Mar 15 '18 at 07:49
  • 3
    Did you try to debug it and see what are the `password` and `username` values? – Guy Mar 15 '18 at 07:51
  • @YCF_L I think looking by index 'll work too, but the description of the table seems dodgy: (username=varchar,, password= varchar) is this a weird table, or is there one , too many? – Stultuske Mar 15 '18 at 07:53
  • 2
    You should definitely start using a debugger and some code formatting. – Ben Mar 15 '18 at 07:53
  • Also naming the table `adminstrator` seems weird. Maybe you meant `administrator`? – Ben Mar 15 '18 at 07:53
  • 1
    @Stultuske totally correct, but if the query is like this `select username, password from adminstrator` with with * I think It is weird what is the table contains more than username and password – Youcef LAIDANI Mar 15 '18 at 07:56
  • 1
    @dana have you checked the values in the meantime? – Stultuske Mar 15 '18 at 08:16
  • 1
    Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – payloc91 Mar 15 '18 at 08:29
  • SELECT * FROM `adminstrator` query will return all data from table and these all rows having some data in it,so when you are going for if condition that always fail. you just change the query to SELECT * FROM `adminstrator` where username=yourusername and password=yourpassword is that doesn't return anything that means you have to register it else you will go for login – Abhijeet Gulve Mar 15 '18 at 08:39
  • If the table is empty, you 'll never enter the while-loop, as there will be no rows in the table... – Mark Rotteveel Mar 15 '18 at 16:12

2 Answers2

0

I recommend selecting how many administrators you have in the database using the count function and then check if it's 0 or not.

M.Lamine Lalmi
  • 78
  • 1
  • 2
  • 12
0

change from

    if ( password.equals("") && username.equals("")) {
       new AdminNewRegister();                
    }

to

    if ( password.isEmpty() && username.isEmpty()) {
           new AdminNewRegister();                
    }

Hope this solves your problem. All the best:)

Chirag
  • 555
  • 1
  • 5
  • 20