为我的应用程序创建管理员

时间:2018-06-21 15:56:04

标签: java mysql sql javafx

因此,如果用户在数据库中的admin 1列(而不是0列)中加载了另一个fxml文件,则admin可以比常规用户执行更多的工作。

这是负责用户登录的控制器,测试密码和用户名是否在数据库中。

PS在下面,我将详细介绍如何尝试创建方法。

public class LogareController implements Initializable {

LoginVerifier loginVerifier = new LoginVerifier();


   @FXML
   private TextField Numeutilzator;

   @FXML
   private PasswordField Parola;

   @FXML
   private Label Stare;






@Override
 public void initialize(URL location, ResourceBundle resources) {


  if (loginVerifier.Conexiune()) {
  Stare.setText("");
  } else {

  Stare.setText("Conexiune nereusita!");

  }

 }

 public void Autentificare (ActionEvent event) {
     try {
         if(loginVerifier.testaredate(Numeutilzator.getText(),         Parola.getText())) {
             Stare.setText("Autentificare reusita !");
             ((Node)event.getSource()).getScene().getWindow().hide();
                Stage st= new Stage();
            FXMLLoader loader= new FXMLLoader();
            Pane Pane =     loader.load(getClass().getResource("/LicentaApp/Meniu.fxml").openStream());

            Scene scene = new Scene(Pane);
            scene.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
            st.setScene(scene);
            st.show();

          }
     else { 
         Stare.setText("Nume de utilizator sau parola incorect");

     }




} catch (SQLException e) {





     e.printStackTrace();
} catch (IOException e) {

    e.printStackTrace();
    }
         }
     @FXML
    public void Inregistrare(ActionEvent event) {
        try {
             ((Node)event.getSource()).getScene().getWindow().hide();
                    Stage PS= new Stage();
                FXMLLoader loader= new FXMLLoader();
                Pane Pane1 =     loader.load(getClass().getResource("/LicentaApp/InregistrareUser.fxml").openStream());
                Scene scena = new Scene(Pane1);
                scena.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
            PS.setScene(scena);
            PS.show();
    } catch (Exception e) {

    }



    }
// This is what I came up with, I know its bad, but I can't think of anything else, in this method I am trying to save the value of "Admin" from the table in a variable, which I can you to determen if the user has 0 or 1 at the admin, If he has 1 then a new fxml will be loaded for him, if he has 0 he is a 
regular user

public boolean admin(int admin) throws SQLException {
      ConectaredB ConectaredB=new ConectaredB();
       Connection conectare=ConectaredB.logareDB();
      PreparedStatement PSMG= null;
     ResultSet RSMG = null;
      String Interogare = "SELECT Admin FROM accounts where Admin='1'";
      try {
          PSMG = conectare.prepareStatement(Interogare);
              PSMG.setLong(1, admin);


          LogareController Adminstatus = new LogareController();

    String Adminstatus = admin.getBytes() //IT only lets me to use getBytes(), i wanted to get the value from admin, after the query executed, this causes a confict with the primitive type "int".

        } catch (Exception exceptie2) {
        return true;

        }
    return false;
         }


}
    }

基本上,我该如何制作一种方法来保存sql中admin的值,然后在测试登录凭据以形成“ if”条件(这将确定应加载的fxml)时,就会如此。

LoginVerifier

public class LoginVerifier {



  public LoginVerifier () {


      ConectaredB ConectaredB=new ConectaredB();
        Connection conectare=ConectaredB.logareDB();



   if (conectare == null) {

   System.out.println("Conectare nereusita!");
    System.exit(1);}
  }

  public boolean Conexiune() {
      ConectaredB ConectaredB=new ConectaredB();
        Connection conectare=ConectaredB.logareDB();
   try {
  return !conectare.isClosed();
 } catch (SQLException e) {

  e.printStackTrace();
  return false;
 }

}
  public boolean testaredate(String numeutil, String parola) throws SQLException {
      ConectaredB ConectaredB=new ConectaredB();
        Connection conectare=ConectaredB.logareDB();
      PreparedStatement PSMG= null;
     ResultSet RSMG = null;
      String Interogare = "SELECT * FROM accounts where Username=? and Password=?";
      try {
          PSMG = conectare.prepareStatement(Interogare);
          PSMG.setString(1, numeutil);
          PSMG.setString(2, parola);

          RSMG = PSMG.executeQuery();
            if(RSMG.next()){
                return true;
            }
            else {
                return false;
            }

        } catch (Exception exceptie2) {
            return false;

        }
    }
  }

1 个答案:

答案 0 :(得分:0)

这是如何实现它的粗略示例。

User类是描述用户的模型。我只是把它们与其他fx组件兼容。

public class User {
    private IntegerProperty id = new SimpleIntegerProperty();
    private StringProperty name = new SimpleStringProperty();
    private StringProperty password = new SimpleStringProperty();
    private BooleanProperty admin = new SimpleBooleanProperty();

    public int getId() {
        return id.get();
    }

    public IntegerProperty idProperty() {
        return id;
    }

    public void setId(int id) {
        this.id.set(id);
    }

    public String getName() {
        return name.get();
    }

    public StringProperty nameProperty() {
        return name;
    }

    public void setName(String name) {
        this.name.set(name);
    }

    public String getPassword() {
        return password.get();
    }

    public StringProperty passwordProperty() {
        return password;
    }

    public void setPassword(String password) {
        this.password.set(password);
    }

    public boolean isAdmin() {
        return admin.get();
    }

    public BooleanProperty adminProperty() {
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin.set(admin);
    }
}

User类对象的创建和初始化是在LoginVerifier#login函数中完成的

public class LoginVerifier {

    public Optional<User> login(String username, String password) throws SQLException {
        ConectaredB ConectaredB = new ConectaredB();
        Connection conectare = ConectaredB.logareDB();

        PreparedStatement PSMG = null;
        ResultSet RSMG = null;
        String Interogare = "SELECT * FROM accounts where Username = ? and Password = ?";

        PSMG = conectare.prepareStatement(Interogare);
        PSMG.setString(1, username);
        PSMG.setString(2, password);

        RSMG = PSMG.executeQuery();
        if(RSMG.next()) {
            User user = new User();
            user.setName(username);
            user.setPassword(password);
            user.setId(RSMG.getInt("id"));
            user.setAdmin(RSMG.getInt("Admin") == 1);

            return Optional.of(user);
        }

        return Optional.empty();
    }
}

通过检查Optional是否包含对象并通过检查User#isAdmin属性进行授权来完成认证

public void Autentificare(ActionEvent event) {
    try {
        Optional<User> optional = loginVerifier.login(Numeutilzator.getText(), Parola.getText());

        if(optional.isPresent()) {
            String fxmlFile = optional.get().isAdmin() ? "/LicentaApp/Meniu_Admin.fxml" : "/LicentaApp/Meniu.fxml";

            Stare.setText("Autentificare reusita !");
            ((Node)event.getSource()).getScene().getWindow().hide();

            Stage st = new Stage();
            FXMLLoader loader = new FXMLLoader();
            Pane Pane = loader.load(getClass().getResource(fxmlFile).openStream());

            Scene scene = new Scene(Pane);
            scene.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
            st.setScene(scene);
            st.show();
        }
        else {
            Stare.setText("Nume de utilizator sau parola incorect");
        }
    }
    catch (SQLException | IOException e) {
        e.printStackTrace();
    }
}
相关问题