如何在SQL查询javafx中使用从一个控制器传递到另一个控制器的变量



我有一个从一个控制器传递到另一个控制器的变量,它是用户名和密码,然后我想用它们来进行SQL查询,但它不起作用。

我对javafx真的很陌生,我不知道如何纠正这一点,我现在需要它来做一个项目。

变量ttot包含我从第一个控制器传递的用户名和密码。

private String[] T = new String[2];
public void myFunctione(String text){
label.setText(text);
}
public void yFunctione(String text){
labele.setText(text);
}

@Override
public void initialize(URL location, ResourceBundle resources) {
System.out.print(label.getText());
System.out.print(labele.getText());

//(R) sql query in which I used label and labele 

这是显示学生表的控制器的代码。我现在所做的是,我把两个变量";用户名";以及";密码";在标签";标签";以及";标签";我不知道为什么我不能在SQL查询中插入这些标签的值。

public void viewStudents(ActionEvent e) throws IOException{

Stage primaryStage=new Stage();
FXMLLoader Loader= new FXMLLoader(getClass().getResource("ViewStudents.fxml"));
Parent root = Loader.load();
ViewStudents con = Loader.getController();
con.myFunctione(label1.getText());
con.yFunctione(label2.getText());
Scene scene = new Scene(root,808,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

primaryStage.setScene(scene);
primaryStage.show();
}

以下是对另一个控制器设置2个标签值的mre:

ViewStudents.fxml只有一个标签。标签应显示传递给控制器的两个字符串:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>

<HBox xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fx_tests.ViewStudents">
<children>
<Label fx:id="sqlText" prefHeight="17.0" prefWidth="224.0" text="Press button to show sql text" HBox.hgrow="ALWAYS" />
</children>
</HBox>

它的控制器可以接收两个字符串并将它们设置为标签:

import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class ViewStudents {
@FXML private Label sqlText;
void setStudentDetails(String name, String password){
sqlText.setText("name: "+ name + " password: " + password);
}
}

测试它的简单应用程序:

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class FxmlTest extends Application {
private Button updateStudentsView;
private Label name, password;
private ViewStudents con;
@Override
public void start(Stage primaryStage) {
Pane viewStudents = null;
try {
FXMLLoader Loader= new FXMLLoader(getClass().getResource("ViewStudents.fxml"));
viewStudents = Loader.load();
con = Loader.getController();
} catch (IOException ex) {
ex.printStackTrace();
}
name = new Label("sarah");
password = new Label("89");
updateStudentsView = new Button("Update Students View");
updateStudentsView.setOnAction(e->viewStudents());
HBox studentsDetails = new HBox(20,new Text("Strudent details: "), name,password, updateStudentsView);
Pane root = new VBox(20, studentsDetails, viewStudents);
VBox.setMargin(studentsDetails, new Insets(20));
VBox.setMargin(viewStudents, new Insets(20));
Scene scene = new Scene(root, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
private void viewStudents() {
con.setStudentDetails(name.getText(), password.getText());
updateStudentsView.setDisable(true);
}
public static void main(String[] args) {
launch(null);
}
}

设置好两个标签的内容后,可以使用它来准备sql语句,例如:

void setStudentDetails(String name, String password){
try {
String sql= "SELECT s.id , s.nom , s.prenom , s.email , s.filiere , s.date  FROM `students` as s JOIN `profs` as p ON p.id=s.idfk WHERE p.username=? AND p.password=?"; 
Connection con=AdminsDB.getConnection();
PreparedStatement preparedStatement=(PreparedStatement)con.prepareStatement(sql);
preparedStatement.setString(1,name);
preparedStatement.setString(2,password);
ResultSet rs=preparedStatement.executeQuery();
while(rs.next()){
data.add(new Students(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6)));
}
con.close();
}catch (SQLException e) {
e.printStackTrace();
}
}

最新更新