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