从另一个类获取变量值

时间:2018-12-15 04:48:39

标签: java class variables null

因此,我有一个名为Members的类,还有另一个名为DBConnect的类,该类将连接到数据库,然后将数据库中的值存储到Members类中。然后,我想在另一个名为Members的类中使用Controller类的值。但是,我在Controller类中创建了Object的新实例,如果从那里使用方法get(),它将导致返回null

但是,如果我在DBConnect类中进行所有操作,就可以了。它会返回名字而不是null

Members

package Database;

public class Members {

    private String firstName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

DBConnect

package Database;

import java.sql.*;

public class DBConnect {
    private Connection connection;
    private Statement statement;
    private ResultSet resultSet;

    Members members = new Members();

    public DBConnect() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/finance", "root", "");
            statement = connection.createStatement();
            System.out.println("Successfully connected to DATABASE.");
        } catch (Exception ex) {
            System.out.println("ERROR: Can't connect to the Database! -> " + ex);
        }
    }

    public void getData(int id) {
        try {
            String query = "select * from Members where MembersId = '" + id + "'";
            resultSet = statement.executeQuery(query);

            while (resultSet.next()) {
                members.setId(resultSet.getInt("MembersId"));
                members.setFirstName(resultSet.getString("MembersFirstName"));
                members.setLastName(resultSet.getString("MembersLastName"));
                members.setPosition(resultSet.getString("MembersPosition"));
                members.setHourlyRate(resultSet.getDouble("MembersHourlyRate"));
                members.setGraduated(resultSet.getString("MembersGraduated"));
                members.setLanguages(resultSet.getString("MembersLanguages"));
                members.setCountry(resultSet.getString("MembersCountry"));
                members.setCity(resultSet.getString("MembersCity"));
                members.setAddress(resultSet.getString("MembersAddress"));
            }
        }catch (Exception ex) {
            System.out.println("Error: " + ex);
        }
    }
}

Controller

package sample;

import Database.DBConnect;
import Database.Members;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.ImagePattern;
import javafx.scene.shape.Circle;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ResourceBundle;


public class Controller implements Initializable {

    private DBConnect dbConnect;
    private Members members;

@FXML
    private HBox sidebarMembers;

@FXML
    private void openSidebarMembers(MouseEvent event) throws IOException {
        dbConnect = new DBConnect();
        dbConnect.getData(2);
        members = new Members();
        if (event.getSource() == sidebarMembers) {
            System.out.println(members.getFirstName());
        }
    }

我了解的是,当您创建一个新对象时,它将导致将所有变量初始化为0。除了使它们成为静态对象之外,我还应该如何访问这些值?

所以我的问题是,如何从Members类的DBConnect类访问为Controller类设置的值?

1 个答案:

答案 0 :(得分:1)

在DbConnect类中,将getData方法的返回类型更改为Member。

public Member getData(int id)

现在从控制器中您可以访问该成员

Member member = dbConnect.getData(2);