如何使用javafx从数据库中的数据获取(存储和检索)中创建全局变量?

时间:2019-10-04 14:55:54

标签: java javafx

读了那么多论坛和QnA之后,祝您有美好的一天。我仍然不清楚如何在全球范围内存储数据,并在需要时和/或在其他FXML表单上检索它们。

这就是我刚开始的内容。首先,我制作了一个公共Java类来存储和检索数据。

TestPassData.java

public class TestPassData {

    private static int PassData;

    public static int getPassData() {
        return PassData;
    }

    public static void setPassData(int PassData) {
        TestPassData.PassData = PassData;
    }

}

然后是一个控制器,我从数据库中获取数据并将其存储到TestPassData

FetchDataController.java

con = pst.prepareStatement("SELECT Emp_ID FROM Employee WHERE UserName = ? AND Password = ?");
pst.setString(1, un);
pst.setString(2, pw);
rs = pst.executeQuery();

while(rs.next()){
      TestPassData.setPassData(rs.getInt("Emp_ID");
}

然后这就是我的问题所在。我无法检索存储在变量中的数据。

RetrieveStoredDataController.java

lblPassID.setText(Integer.toString(TestPassData.getPassData())); 

我总是得到0,作为返回值。希望有人能在此分享他们的想法。

2 个答案:

答案 0 :(得分:0)

Michael Castillo Zephyr关于使用全局变量的说法非常正确。我的观点是,强大的力量带来更大的责任。我们在数据库帮助程序类中设置了一个静态变量,因为我们希望某些事情仅在另一个Controller类中发生一次

static String firstTIME = "TRUE";

您需要关心的是,firstTIME现在始终为“ TRUE”,因此您需要对其进行管理,否则它将管理您。将其设置为“”或“ FALSE”在要使用此功能强大的全局变量的新Controller类中,请确保将其导入为

import static checkbook.CreateTableUtil.firstTIME;

欢迎堆栈溢出

答案 1 :(得分:0)

我终于做了一个全局变量,我可以从任何Controller类中检索它。我只希望您对我的编程方式发表一些意见。 (使用起来不好还是很好?)

首先,我创建了一个公共界面

IUserID.java

public interface IUserID {
   int userID();
}

然后公开课 PerformLogin

public class PerformLogin implements IUserID {

private Connection con;
private PreparedStatement pst = null;
private ResultSet rs = null;

private static int userLoginID;

public PerformLogin(String un, String pw, StackPane basePane, Node rootPane) {

    try {
        this.con = bghsystems.database.DBHandler.connect();

        pst = con.prepareStatement("SELECT Emp_ID FROM User_Accnt where UA_UName = ? AND UA_Pass = ? COLLATE Latin1_General_CS_AS");
        pst.setString(1, un);
        pst.setString(2, pw);
        rs = pst.executeQuery();

        if (rs.next()) {
            PerformLogin.userLoginID = rs.getInt("Emp_ID");      // here is where I pass the data to the global variable

            try {
                Stage stage = (Stage) basePane.getScene().getWindow();
                stage.close();

                FXMLLoader loader = new FXMLLoader(getClass().getResource("/bghsystems/views/Dashboard.fxml"));
                Parent root = (Parent) loader.load();
                stage.setScene(new Scene(root));

                Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
                stage.setX(primaryScreenBounds.getMinX());
                stage.setY(primaryScreenBounds.getMinY());
                stage.setWidth(primaryScreenBounds.getWidth());
                stage.setHeight(primaryScreenBounds.getHeight());

                stage.show();

            } catch (IOException ex) {
                Logger.getLogger(PerformLogin.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
        }

    } catch (ClassNotFoundException | SQLException ex) {
        Logger.getLogger(PerformLogin.class.getName()).log(Level.SEVERE, null, ex);
    }

}

public PerformLogin() {

}

@Override
public int userID() {
    return PerformLogin.userLoginID;   //here is where I store the data
}

然后是我用来检索存储的数据的代码。

RandomControllers

 PerformLogin pd = new PerformLogin();
    lblEmpID.setText(Integer.toString(pd.userID()));