我正在努力让用户登录& JavaFX中的注册申请& Sqlite。
我成功将数据插入到注册窗口的数据库中,并在登录窗口中从数据库中选择数据。
但我需要在新窗口中显示用户的所有数据(数据库中的哪条记录)。我为它输入了一些代码,但每次登录后它只显示用户窗口中数据库的最后一个数据。
登录代码:
@FXML
public Label errMesage;
@FXML
public TextField userNameTF;
@FXML
public TextField passwordTF;
@FXML
public Button loginBtn;
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
}
public void handleSecoundaryRegestrationLink(ActionEvent event) throws IOException{
Parent switchAlterRegestrationPage = FXMLLoader.load(getClass().getResource("/gui/gui1.fxml"));
Scene sceneAlterRegestrationPage = new Scene(switchAlterRegestrationPage);
Stage stageAlterRegestrationPage = (Stage)((Node)event.getSource()).getScene().getWindow();
stageAlterRegestrationPage.hide();
stageAlterRegestrationPage.setScene(sceneAlterRegestrationPage);
stageAlterRegestrationPage.show();
}
public void handleLoginBtn(ActionEvent event) throws IOException{
Parent login = FXMLLoader.load(getClass().getResource("/gui/user.fxml"));
Scene sceneLogin = new Scene(login);
Stage stageLogin = (Stage)((Node)event.getSource()).getScene().getWindow();
if(isValidLoginCOndition()){
stageLogin.hide();
stageLogin.setScene(sceneLogin);
stageLogin.show();
}
else{
errMesage.setText("UserName or Password not Matched!");
}
}
public boolean isValidLoginCOndition() {
boolean let_in = false;
System.out.println("SELECT * FROM user WHERE usrName = "+"'" + userNameTF.getText() +"'"
+ "AND PASSWORD "+ "'" + passwordTF.getText()+"'");
Connection c = null;
Statement stmt = null;
try {
c = DriverManager.getConnection("jdbc:sqlite:regestration.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT * FROM user WHERE USERNAME= " + "'" + userNameTF.getText() + "'"
+ " AND PASSWORD= " + "'" + passwordTF.getText() + "'");
while ( rs.next() ) {
if (rs.getString("USERNAME") != null && rs.getString("PASSWORD") != null) {
String username = rs.getString("USERNAME");
System.out.println( "USERNAME = " + username );
String password = rs.getString("PASSWORD");
System.out.println("PASSWORD = " + password);
let_in = true;
}
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
return let_in;
}
}
用户表单代码:
@FXML
public Label loginFullName;
@FXML
public Label emailLB;
@FXML
public Label loginFullName2;
@FXML
public AnchorPane usrAP;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
public void handleShowDetails(){
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:regestration.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT * FROM user");
while ( rs.next()) {
String fullname = rs.getString("FULLNAME");
String email = rs.getString("EMAIL");
loginFullName.setText(fullname);
emailLB.setText(email);
loginFullName2.setText(fullname);
System.out.println( "FULLNAME = " + fullname );
System.out.println( "EMAIL = " + email );
System.out.println();
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
}
public void handleLogOut(ActionEvent event) throws IOException{
Parent logout = FXMLLoader.load(getClass().getResource("/gui/login.fxml"));
Scene sceneLogOut = new Scene(logout);
Stage stageLogOut = (Stage) ((Node)event.getSource()).getScene().getWindow();
stageLogOut.hide();
stageLogOut.setScene(sceneLogOut);
stageLogOut.show();
}
}
答案 0 :(得分:0)
如果我理解你想要的行为,你想通过调用handleShowDetails函数在额外的Form中显示用户详细信息。 如果是这样sql调用
Select * from user
将从数据库中获取所有用户。然后循环遍历数据并设置标签登录fullname,它始终只显示while循环的最后一项。
因此,如果您只想显示当前登录用户的数据,则必须将这些额外参数传递给您的sql调用,如
Select * from user where usrName=...
正如您在登录表单中所做的那样。
要优化您应该更好地将用户数据存储在User对象的登录过程中,以避免在会话期间可以在db上进行多次调用。