0

I have this variable that I passed from one controller to another it's the username and the password and then I wanted to use them to make a SQL query but it's not working.

I am really really new with javafx and I have no idea how to correct this and I need it now for a project.

The variables t and tot contain the username and password that I passed from the first controller.

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 

This is the code of the controller that displays the students table. What I did now is that I put the two variables "username" and "password" in the labels "label" and "labele" and I don't know why I can't insert the value of these labels in the SQL query.

    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();
    }
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
sarah8909
  • 13
  • 3
  • [edit] your question to add the code in the picture and remove the pictures. Divide and conquer. To solve a problem break it into smaller problems. What do you get when you print out `label.getText()` and `labele.getText()` (not the best choice of names) in `ViewStudent` ? – c0der May 10 '20 at 04:46
  • Do you initialize the text of the labels via fxml or use a controller factory to set those values? Otherwise those values are not set by the time `initialize` is called. – fabian May 10 '20 at 08:33
  • Thank you all for replying, @c0der i tried printing out label.getText() and I actually got Label instead of the value of the username that I find in it in the scene, same for labele.getText() (I'm going to change their names,thanks). – sarah8909 May 10 '20 at 09:11
  • @fabian thank you for replying, I do think the values are not set by the time initialize is called. I am not sure how to initialize the labels via a controller factory , I would very much like some help with that. i'm adding some code of where did i get the labels from. – sarah8909 May 10 '20 at 09:13
  • **"**_I actually got Label_**"** I am not sure what that means. Consider adding the printout to the code and add the output as comment. If the printout is not what you expect, than the sql part of the question is not relevant and can be removed. – c0der May 10 '20 at 09:14
  • @c0der the ouput that i get is <<<<< – sarah8909 May 10 '20 at 09:25
  • @c0der Yes the second print out is not what I expected. That's true, how can i solve this issue – sarah8909 May 10 '20 at 09:26
  • 1
    Please post [mcve]. Many chunks of this code are not essential to demonstrate the problem. On the other hand it leave us guessing missing information. I find [SSCCE](http://www.sscce.org/) or [mre] very useful techniques. Not only it makes helping much easier, it is a powerful debugging tool. It many case, while preparing one, you are likely to find the problem. – c0der May 10 '20 at 09:54
  • @c0der Thank you for your note. I hope it's better now. – sarah8909 May 10 '20 at 10:03
  • Does this answer your question? [Passing Parameters JavaFX FXML](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) – kleopatra May 10 '20 at 10:14
  • @kleopatra thank your for your reply in the other question, the response was to use a controller. But I already used it to pass in the value of the labels i couldn't find another way to use it besides this one. I'm not sure i fully understood the answer as I am really new with javafx – sarah8909 May 10 '20 at 10:51
  • I am sorry but the posted code is not [mre] but some snippets from which I can't understand much. I am guessing that your problem is using text of two Labels, defined in an fxml file, in a controller of another fxml file. Please post short (**M**) code that shows only that. Remove all unrelated code such as the sql part. Make it complete (**R**) so we can copy-paste run. – c0der May 10 '20 at 11:13
  • Some additional comments: `initialize` is invoked **before** `myFunctione` and `ymyFunctione`. Also it is not clear why you split `text.split("/")` and why do you expect `text` to contain a `/` – c0der May 10 '20 at 11:17
  • @c0der i hope it's clearer now. Yes that's my problem. i see and is there a way to make this code work even if initialize is invoked before the two functions. the text.split was just a detail. I removed it because I don't really need it. I genuinely hope there is a way for this code to work because I need it for a school project. – sarah8909 May 10 '20 at 11:35
  • **"**_I removed it because I don't really need it_**"** that is a good example why mre is needed to avoid waste our time and yours. Please follow the advice you got. – c0der May 10 '20 at 11:38

1 Answers1

0

The following is an mre of setting values of 2 labels to another controller:

ViewStudents.fxml has only a label. The label should show the two strings passed to the controller:

<?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>

Its controller that can receive two Strings and set them to the label:

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);
    }
}

Simple application to test it:

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);
    }
}

After you have set the content of the two labels you can use it to prepare an sql statement, for example:

   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();
        }
    }
c0der
  • 18,467
  • 6
  • 33
  • 65
  • Thank you so much for your help I really appreciate it. You helped me to realize the main problem in my code. I apologize if my question wasn't clear enough. I managed to find a solution maybe it's not the perfect one. but it worked, I set a global variable in wich i did put the values that I got from the first two labels and i used the two global values in mysql query. Thank you so much again – sarah8909 May 10 '20 at 13:24