当前位置: 首页 > 知识库问答 >
问题:

使用setter和getter分配从数据库中选取的数据[duplicate]

长孙承嗣
2023-03-14

我已经成功地从数据库中检索到数据。其中一个变量是我想在DashboardController中使用的uniqueiddb。我需要它,因为我将不得不为数据库中的单个用户查询数据。但它是在LoginController中检索的。我得把它移到仪表板控制器上。我已经尝试使用setter来设置LoginController中的值。当我在DashboardController中使用getter时,会得到一个NullPointException。表示未设置该值。我不明白为什么。请给我指出我哪里错了。我不知道是否应该使用getter和setter将这个uniqueIddb从一个类移动到另一个类。我要解决的问题是在类之间传递用户数据

LoginController.java

package Login;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import sample.databaseHandler;

import javax.swing.*;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Random;
import java.util.ResourceBundle;

public class LoginController implements Initializable {

    @FXML
    private TextField email;

    private String uniqueIddb;

    Connection con = null;

    public LoginController() {

    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

    }

    private void closeStage(){
        ((Stage) email.getScene().getWindow()).close();
    }
    @FXML
    private void loginUser(ActionEvent actionEvent) {

        PreparedStatement stmt;
        String userEmail = email.getText();
        System.out.println(userEmail);

        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Student Portal", "root", "");
            System.out.println("connection has been made");
            stmt = con.prepareStatement("SELECT Email,UniqueId FROM members WHERE Email = ? ");
            stmt.setString(1, userEmail);
            System.out.println(stmt);

            ResultSet result = stmt.executeQuery();

            while (result.next()) {
                String emaildb = result.getString("Email");
                *uniqueIddb = result.getString("UniqueId");*

                if(userEmail.equals(emaildb) ){

                    closeStage();
                    Stage stage = new Stage();
                    Parent root = null;
                    try {
                        root = FXMLLoader.load(getClass().getResource("/Dashboard/dashboard.fxml"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Scene scene = new Scene(root);
                    stage.setScene(scene);
                    stage.show();
                    **setUniqueIddb(uniqueIddb);**

                } else{
                    //pass an alert for wrong credentials
                }

            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,"Cant load Database", "Database Error", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }
}

    public void setUniqueIddb(String uniqueIddb) {
        this.uniqueIddb = uniqueIddb;
    }



    public String getUniqueId() {
        return uniqueIddb;
    }
}

DashboardController.java

package Dashboard;

import Login.LoginController;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

import javax.swing.*;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DashboardController {
    @FXML
    private Label uniqueIdDisplay;

    public DashboardController() {
        unique();
    }

    public void unique(){
        LoginController login = new LoginController();

        **String uniqueID = login.getUniqueId();**
        uniqueIdDisplay.setText(uniqueID);
    }



    @FXML
    public void openGeneral(MouseEvent mouseEvent) {


        try {
            Stage stage = new Stage();
            Parent root = FXMLLoader.load(getClass().getResource("/General/optionGeneral.fxml"));
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();



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

    @FXML
    public void openProfile(MouseEvent mouseEvent) {

        try{
            Stage stage = new Stage();
            Parent root = FXMLLoader.load(getClass().getResource("/Profile/optionProfile.fxml"));
            Scene scene =new Scene(root);
            stage.setScene(scene);
            stage.show();

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

    @FXML
    public void openPerformances(MouseEvent mouseEvent) {
        try{
            Stage stage = new Stage();
            Parent root = FXMLLoader.load(getClass().getResource("/Performances/optionPerformances.fxml"));
            Scene scene =new Scene(root);
            stage.setScene(scene);
            stage.show();

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

    @FXML
    public void openLectures(MouseEvent mouseEvent) {
        try{
            Stage stage = new Stage();
            Parent root = FXMLLoader.load(getClass().getResource("/Lectures/optionLectures.fxml"));
            Scene scene =new Scene(root);
            stage.setScene(scene);
            stage.show();

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

    @FXML
    private void enrollToCourse(MouseEvent mouseEvent) {

        Stage stage = new Stage();
        Parent root = null;
        try {
            root = FXMLLoader.load(getClass().getResource("enrollCourseDialog.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
}

共有1个答案

华景明
2023-03-14

在LoginController中:

  String emaildb = result.getString("Email");
  uniqueIddb = result.getString("UniqueId");
  // to acsses Dashboard controller
  DashboardController controller = loader. < DashboardController > getController();
  //call the methode seID defined in DashboardController  to set the variable 
  controller.setID(uniqueIddb);

在仪表板控制器中定义seID方法:

 class DashboardController {
      // Declaration of variable 
      int uniqueIddb;
      void initialize() {}
      // Methode setID
      void seID(String myID) {
      this.uniqueIddb = myID;
      }
  }
 类似资料:
  • 有什么方法可以在Kotlin数据类中创建一个私有setter和一个公共getter吗?

  • 问题内容: 我正在尝试将静态数据转换为使用数据库结果。我将使用 MySQL 和 PHP 。 示例代码: 以下是我的php / msql: 如何使用我的MySQL查询中的那些并将其实现到chartjs上的数据集?我也希望标签也可以从我的MySQL查询中生成。我应该在jQuery代码内部循环数据集吗? 这是我正在使用的插件:http : //www.chartjs.org/docs/#line-cha

  • 问题内容: 我想使用php和jquery ajax从mysql数据库中获取数据。“ process.php”是连接到数据库并获取mysql数据的php文件。当它单独运行时它可以工作,但是当使用ajax调用时它不起作用。有人可以帮忙纠正错误吗?这是我的html文件: 这是我的process.php文件 问题答案: 您的ajax调用中有两个语法错误: 请记住,jQuery的ajax需要一个对象作为参数

  • 我需要从数据库读取数据,并使用PIG分析数据。我用java编写了一个UDF,引用了下面的链接 org.apache.pig.impl.logicallayer.frontendException:错误1066:无法在org.apache.pig.pig.tools.grunt.gruntparser.openiterator(pigserver.java:892)在org.apache.pig.t

  • 一切正常,但DatabaseReference无法获取数据,这就像是忽略了我的代码运行,就像我的internet无法运行一样,请帮助我,我是这个社区的新手,下面是我的代码和图片。 以前它是工作的,但由于我只是更改了一些代码,使只有currentVersion>=vCode,这样即使数据库中的值是 firebase数据库映像 mainactivity.java manifest.xml 依赖关系

  • 问题内容: 我有一个MS-Access数据库,我正在使用JDBC(我认为是JDBC-ODBC桥)在Java中连接到该数据库。我的访问数据库有一些希伯来语值。 当我尝试使用String str = rs.getString(1)(rs是RowSet)读取这些值时,我得到的字符串只是一个问号字符串。 我还有希伯来语中的其他字符串,这些字符串是我在Java代码中使用字符串文字设置的,它们可以正常工作。所