1

i want to create this task, when administrator uses this program for first time he has to register but for the second time he doesn't have to register because he already did and he has to login .. I tried to retrieve the username and password from database then compare result Set if the result set is empty then let the register class to be open else let the login form to be open ... but I didn't work. what is the problem ???

@Override
public void mouseClicked(MouseEvent me) {
    Object click = me.getSource();
    if (click == buttonAcoutUS) {
        new AboutUs();
        dispose();
    } else if (click == buttonAdmin) {
        ifIDExit();
        dispose();
    } 

}

 public void ifIDExit(){

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

        rs=ps.executeQuery();
        rs.next();
        if (rs.equals("")) {
               new newRegister();

        }
        else {
            new AdminLogin();
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

}

}
dana
  • 11
  • 2

1 Answers1

1

rs.next(); just moves the cursor forward one row from its current position (in this case to the first because, initially the cursor is positioned before the first row).

For retrieving strings from the result set use ResultSet.getString(int columnIndex). Therefore you should not use SELECT * but name all columns in your query.

Depending on your DB model this could work (or at least give you the right idea):

 ps = con.prepareStatement("SELECT username, password FROM adminstrator");

 rs = ps.executeQuery();
 while (rs.next()) { //loop through results
    String un = rs.getString(1); //first column is 1
    String pw = rs.getString(2);
    //... compare strings to something
 }

See here for details about ResultSets.

Würgspaß
  • 4,660
  • 2
  • 25
  • 41