我是 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;
}
}
问题是变量 name
在 pst.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
无法获取文本。有没有办法解决这个问题?