【发布时间】:2018-06-21 15:56:03
【问题描述】:
因此,如果用户在 db 中的 admin 列 1 而不是 0 处加载不同的 fxml 文件,管理员可以在其中执行比普通用户更多的操作。
这是负责用户登录的控制器,测试密码和用户名是否在数据库中。
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 中保存管理员的值,然后在测试登录凭据以创建一个“if”条件时,该条件将决定应该加载什么 fxml。
登录验证器
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;
}
}
}
【问题讨论】:
-
“正确的做法”是制作一个描述用户(包括他是否是管理员)的DTO(数据传输对象)。登录过程必须初始化并返回此对象(如果输入数据不正确,则返回 null)。然后只需检查描述用户是否为管理员的字段。
-
我在
LoginVerifier控制器中执行此操作,在该控制器中,我使用 x 用户名和 y 密码从用户那里获取所有内容,我不确定如何获取在“admin”中找到的值“ tho,以及如何使用它。我已经粘贴了“LoginVerifier”的代码