从外部静态连接方法引用变量

Referring to a variable from outside static Connection method

我是 java 的新手,开始做一个 javaFX 项目。在这个项目中,我从前一帧接收到一个变量,并使用它来执行 SQL 查询,以便根据该特定变量呈现 table。 这是我的代码:

package financials;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

import javax.swing.JOptionPane;

/**
 * FXML Controller class
 *
 * @author param
 */
public class theControl implements Initializable {
    @FXML
    private Label test;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        Statement st;
        Connection con = null;    
    }

    /**
     *
     * @param name
     */
    public void previous(String name) {
        System.out.println(name);
    }
       
    public static Connection ConnectDB() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/database", "root", "Password");
            
            return con;
        } catch(Exception ae) {
            JOptionPane.showMessageDialog(null, ae);

            return null;
        }
    }

    public static ObservableList<RenderNow> getListAccount() {
        Connection con = ConnectDB();
        ObservableList<RenderNow> list = FXCollections.observableArrayList();
        try {
            PreparedStatement pst = con.prepareStatement("SELECT * FROM lines WHERE Code=? ");
            pst.setString(1, name); //This is where I am having trouble
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                list.add(new SBRender(rs.getString("Account1"), rs.getString("Account2"), rs.getString("Account3"), rs.getString("Account4"), rs.getString("Account5")));
            }         
        } catch(Exception ae) {
            JOptionPane.showMessageDialog(null, ae);

            return null;                          
        }

        return list;  
    }
}

问题是变量 namepst.setString 行中没有被识别。我收到的错误是 variable 'name' is not found。我尝试了一种不同的方法,我使用 name 设置 Label test 的文本,然后尝试在 public static Connection ConnectDB() 方法中获取变量。

类似于:

public class theControl implements Initializable {
    @FXML
    private Label test;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        Statement st;
        Connection con = null;
    }

    /**
     *
     * @param name
     */
    public void previous(String name) {
        System.out.println(name);
        test.setText(name); //Where i set the text of label 'text'
    }
   
    public static Connection ConnectDB() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/database", "root", "Password");
            
            return con;
        } catch(Exception ae) {
            JOptionPane.showMessageDialog(null, ae);

            return null;
        }
    }

    public static ObservableList<RenderNow> getListAccount() {
        String name2 = test.getText(); //Where I try and get the text from label 'text'
        Connection con = ConnectDB();
        ObservableList<RenderNow> list = FXCollections.observableArrayList();
        try {
            PreparedStatement pst = con.prepareStatement("SELECT * FROM lines WHERE Code=? ");
            pst.setString(1, name2); //This is where I am having trouble
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                list.add(new SBRender(rs.getString("Account1"), rs.getString("Account2"), rs.getString("Account3"), rs.getString("Account4"), rs.getString("Account5")));
            }
        } catch(Exception ae) {
            JOptionPane.showMessageDialog(null, ae);

            return null;                           
        }

        return list;  
    }
}

但是,这种尝试returns出错了non-static variable test cannot be referenced from a static context。我的理解是,由于标签 test 不是静态的,所以 static Connection 无法获取文本。有什么解决办法吗?

在你的第一种情况下,变量没有设置。它仅适用于您拥有打印方法的方法。

在第二种情况下,您没有使用该对象。 test 变量在一个对象中,因此不能通过不依赖于对象的静态方法访问。

我建议您将新参数添加到您的静态方法中,并像这样使用:

// create new static method which require the name in parameters
public static ObservableList<RenderNow> getListAccountWithName(String name) {
    Connection con = ConnectDB(); // get DB thanks to static method
    ObservableList<RenderNow> list = FXCollections.observableArrayList();
    try {
        PreparedStatement pst = con.prepareStatement("SELECT * FROM lines WHERE Code = '?'");
        pst.setString(1, name); // now you can use name value
        ResultSet rs = pst.executeQuery();
        while (rs.next()) {
            list.add(new SBRender(rs.getString("Account1"), rs.getString("Account2"), rs.getString("Account3"), rs.getString("Account4"), rs.getString("Account5")));
        }
    } catch(Exception ae) {
        JOptionPane.showMessageDialog(null, ae);
        return null;                           
    }

    return list;  
}

现在,您可以从对象调用它:

ObservableList<RenderNow> accounts = getListAccountWithName(test.getText());
// now what you have what you are looking for