0

UPDATE : More information about the progress that i made and what dificulties im stubling across rn, is written at the end of the post in italic

I'm trying to show in a table view all the appointments that the current logged in user has made. I'm trying to do so by utilizing the username that the person used to login with to make the condition, because the username is unique in the DB.

PS. I wrote the details about what I tried through the code.

The first table where the appointments are saved looks like this:

This is the "Programaritest" table

ID_programare | Nume | Prenume | Data | Ora | Departament | Doctor | Nr_telefon

This is the accounts table where the username is saved as Unique

ID_account | Username | Password | Email | Nume |  Prenume | Admin 

In this I am populating the tableview with stuff from the database.

The username that I was speaking of is defined as a TextField in another class, I will post it below

package LicentaApp;

public class ProgramariDBController implements Initializable {


@FXML
private TableView<DetaliiProgramari> ProgrDB;
@FXML
private TableColumn<DetaliiProgramari, String> Nume;
@FXML
private TableColumn<DetaliiProgramari, String> Prenume;
@FXML
private TableColumn<DetaliiProgramari, String> Data;
@FXML
private TableColumn<DetaliiProgramari, String> Ora;
@FXML
private TableColumn<DetaliiProgramari, String> Departament;
@FXML
private TableColumn<DetaliiProgramari, String> Doctor;
@FXML
private TableColumn<DetaliiProgramari, String> Nr_telefon;

public LogareController Numeutilzator = new LogareController(); This is how I thought of taking the username which is found in LogareController, when somebody is tipying it in to login into the app.

private ObservableList<DetaliiProgramari> Info;

@Override
public void initialize(URL url, ResourceBundle ResurcesFORDAYS) {
    // TODO

}

@FXML
private void loadDataFromDatabase(ActionEvent event) {
    try {
      ConectaredB ConectaredB=new ConectaredB();
        Connection conectare=ConectaredB.logareDB();

        Info = FXCollections.observableArrayList();

        ResultSet IncDate = conectare.createStatement().executeQuery("SELECT *  FROM programaritest WHERE Username IN (SELECT Username FROM accounts where Username='"+Numeutilzator.getText()+" ')"); This is how I thought of making the query.
        while (IncDate.next()) {

            Info.add(new DetaliiProgramari(IncDate.getString(2), IncDate.getString(3), IncDate.getString(4), IncDate.getString(5), IncDate.getString(6), IncDate.getString(7), IncDate.getString(8)));
        }
    } catch (SQLException ex) {
        System.err.println("Error"+ex);
    }

    Nume.setCellValueFactory(new PropertyValueFactory<>("Nume"));
    Prenume.setCellValueFactory(new PropertyValueFactory<>("Prenume"));
    Data.setCellValueFactory(new PropertyValueFactory<>("Data"));
    Ora.setCellValueFactory(new PropertyValueFactory<>("Ora"));
    Departament.setCellValueFactory(new PropertyValueFactory<>("Departament"));
    Doctor.setCellValueFactory(new PropertyValueFactory<>("Doctor"));
    Nr_telefon.setCellValueFactory(new PropertyValueFactory<>("Nr_telefon"));

    ProgrDB.setItems(null);
    ProgrDB.setItems(Info);
    }
}

So here is the class where the username I'm looking to utilize is found:

 package LicentaApp;

 public class LogareController implements Initializable {
 public LoginVerifier loginVerifier = new LoginVerifier();

   @FXML
   private TextField Numeutilzator; **This is the one, "NumeUtilizator" is the username 

   @FXML
   private PasswordField Parola;

   @FXML
   private Label Stare;
 @Override
 public void initialize(URL location, ResourceBundle resources) {

  if (loginVerifier.Conexiune()) {
      Stare.setText("");
  } else {
      Stare.setText("Conexiune nereusita!");
  }
 }

 public void Autentificare (ActionEvent event) {
 try {
     if(loginVerifier.testaredate(Numeutilzator.getText(),                 Parola.getText())) {
         Stare.setText("Autentificare reusita !");
         ((Node)event.getSource()).getScene().getWindow().hide();
            Stage StagePrincipala= new Stage();
            FXMLLoader incarcator= new FXMLLoader();
            Pane parinte =     incarcator.load(getClass().getResource("/LicentaApp/Meniu.fxml").openStream());

            Scene scene = new Scene(parinte);
            scene.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
            StagePrincipala.setScene(scene);
            StagePrincipala.show();

          }
     else { 
         Stare.setText("Nume de utilizator sau parola incorect");

     }
} catch (SQLException e) {
     e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
    }
 }
     @FXML
    public void Inregistrare(ActionEvent event) {
        try {
             ((Node)event.getSource()).getScene().getWindow().hide();
            Stage PS= new Stage();
            FXMLLoader incarcator= new FXMLLoader();
            Pane parinte =     incarcator.load(getClass().getResource("/LicentaApp/InregistrareUser.fxml").open    Stream());
            Scene scena = new Scene(parinte);
            scena.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
            PS.setScene(scena);
            PS.show();
    } catch (Exception e) {

    }

public String getText() {
    TextField Numeutilizator = new TextField();
    Numeutilizator.getText();
    return null;
}

    }

}

The error I'm getting is:

The method getText() is undefined for the type LogareController

Why is this?

PS. Basically I want to show all the appointments made by the current logged in user.

This is what I tried:

public String getText() {
    TextField Numeutilizator = new TextField();
    Numeutilizator.getText();
    return null;
}

Seem to have fixed it, but I can't tell if it's how it should be, what do you think ?

Adding the username to the apointment

Basically, if somebody else is seeing this, I'm trying to get the Username(Numeutilizator) from the LogareController(username is used there to login) to be used in AddProgramareController when an apointment is made, the username will be inserted into the table so I can then use it to show all the apointments made by that user in a tableview

Error that I am getting right now :

java.sql.SQLIntegrityConstraintViolationException: Column 'Numeutilizator' cannot be null

The problem is that my attempt to move the information from Numeutilizator was unsuccesful or the way I'm trying to give that information to the query(statement) isn't quite there..

package LicentaApp;




public class AddProgramareController implements Initializable {
ObservableList Timestamp=FXCollections.observableArrayList();




@FXML
private TextField Nume;

@FXML
private TextField Prenume;

@FXML
private TextField Ora;

@FXML
private DatePicker Data;

@FXML
private TextField Departament;

@FXML
private TextField Doctor;

@FXML
private TextField Nr_telefon;













@FXML
LogareController Numeutilizator=new LogareController();

public void initialize(URL location, ResourceBundle resources) {

} 
@FXML 
private void AddProgramare(ActionEvent event) throws SQLException, IOException  {


    String Interogare1= "INSERT INTO programaritest(Nume,Prenume,Data,Ora,Departament,Doctor,Nr_telefon,Numeutilizator) VALUES(?,?,?,?,?,?,?,?)";

    String nume=Nume.getText();
    String prenume=Prenume.getText();

    LocalDate data=Data.getValue(); 

    String ora=Ora.getText();
    String departament=Departament.getText();
    String doctor=Doctor.getText();
    String nr_telefon=Nr_telefon.getText();
    String numeutilizator=Numeutilizator.getText();

    try {
        ConectaredB ConectaredB=new ConectaredB();
        Connection conexiune=ConectaredB.logareDB();
        PreparedStatement MG = conexiune.prepareStatement(Interogare1);


        MG.setString(1, nume);
        MG.setString(2, prenume);
        MG.setDate(3, Date.valueOf(data)); 
        MG.setString(4, ora);
        MG.setString(5, departament);
        MG.setString(6, doctor);
        MG.setString(7, nr_telefon);
        MG.setString(8, numeutilizator);

        MG.executeUpdate();

        // ...
   } catch (SQLException exceptie1) {
       exceptie1.printStackTrace(); 
   }


}
}
MGAMG
  • 51
  • 6
  • Obviously you didn't declare a `getText` method in `LogareController` and `Numeutilzator.getText()` yields a compile time error. Also the `LogareController` instance is never used with a fxml so it will be impossible to retrieve any inputs via that instance. Furthermore concatenating strings to create the query leads to problems, if you enter quotes in the `TextField`. You should use `PreparedStatement` to produce a query containing properly quoted sql String literals. – fabian Jun 06 '18 at 10:08
  • Hey, thanks for ur answer, the input from NumeUtilizator is actually used to verify if the login credentials are good or not, I have posted the code for the LoginVerifier via Edit, you can take a look if that helps in any way, it is used in a FXML. One more thing, i can't really make much of the duplicate that you linked, not really the same thing, maybe it is for you beacause you do understand these things obviously better then I do. Thank you again. – MGAMG Jun 06 '18 at 12:03
  • The linked question asks why the `getCost()` method results in *"X method is undefined for type Y"* and the issue is that type `Y` does not contain a method named `X` (`X` = `getCost`, `Y` = `ShopCLI`). Your issue is the same, only with `X` = `getText` and `Y` = `LogareController`. The answer is also applicable (you need to access the `getText` method via `TextField`, not via `LogareController`; alternatively declare a method with this name in `LogareController`). The `LoginVerifier` is unrelated to the issue; your issue is in the `ProgramariDBController` class. – fabian Jun 06 '18 at 12:42
  • Added the method, edited the post with what I did, can you take a look ? – MGAMG Jun 06 '18 at 14:32
  • Take a look at the *Get Controller* and *Set Controller* approaches here: https://stackoverflow.com/a/14190310/2991525 (Even though the question is about passing values, you can also use the controller instance to retrieve information from injected Nodes). – fabian Jun 06 '18 at 15:11
  • This is actually amazing, but after i retrive the data from of the caller, how do I use it, as in, for the current caller(user?) to show the apointments created by him ?Thankyou again btw – MGAMG Jun 06 '18 at 15:26
  • More pressing matters tho, after I got rid of the method error, it's giving me this: " Errorjava.sql.SQLSyntaxErrorException: Unknown column 'Username' in 'IN/ALL/ANY subquery' " The query that does that is : ("SELECT * FROM programaritest WHERE Username IN (SELECT Username FROM accounts where Username='"+Numeutilzator.getText()+" ')") – MGAMG Jun 06 '18 at 15:40
  • Is the `username` column defined in your `programaritest` table? – fabian Jun 06 '18 at 18:16
  • It wasn't, i have added it, but now, I'm trying to think of a way using the Username that I got via the gettext method that i made, to insert it into the db when a new appointment is made, I wrote some code but it returns that the username column can not be null, I'll paste it in the edit, if u wanna take a look, sorry for the late reply, had a poweroutage situation.. – MGAMG Jun 07 '18 at 08:02

0 Answers0