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

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

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",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("account5")));
            }
        } catch(Exception ae) {
            JOptionPane.showmessagedialog(null,ae);

            return null;                           
        }

        return list;  
    }
}

但是,此尝试返回错误 non-static variable test cannot be referenced from a static context。我的理解是,由于标签 test 不是静态的,static Connection 无法获取文本。有没有办法解决这个问题?

pingkang 回答:从外部静态连接方法引用变量

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

在第二种情况下,您没有使用对象。 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
本文链接:https://www.f2er.com/18214.html

大家都在问