Reputation: 51
I'm trying to show all the appointments that have a specific CNP that a user had used to make it.
The user is asked to reenter the same CNP that he did use when he created the appointment in order to see the appointments made using that specific CNP.
What I want to do, at the query, instead of the "?" to add the value of the CNP, that is stored in String cnp
P.S I have added commentary along the code to help you guys understand.
The code:
package LicentaApp;
public class ProgramariDBController implements Initializable {
@FXML
private TextField cpn; // The user is asked to reenter the same CNP that he did use when he created the appointment
@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();
private ObservableList<DetaliiProgramari> Info;
@Override
public void initialize(URL url, ResourceBundle ResurcesFORDAYS) {
// TODO
}
@FXML
private void AfiseazaProgramari(ActionEvent event) {
try {
ConectaredB ConectaredB=new ConectaredB();
Connection conectare=ConectaredB.logareDB();
Info = FXCollections.observableArrayList();
String CNP=cpn.getText(); // I though making it a string will help, guess it didn't
ResultSet IncDate = conectare.createStatement().executeQuery("SELECT * FROM programari where CNP=?"); // What I want to do, instead of the "?" to add the value of the CNP, that is stored in String cnp
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);
}
}
Thank you !
Future link to the delete question
Upvotes: 0
Views: 47
Reputation: 8363
You can simply concatenate the strings.
ResultSet IncDate = conectare.createStatement().executeQuery(
"SELECT * FROM programari where CNP=" + cnp);
Alternatively, use prepared statements.
String sql = "SELECT * FROM programari where CNP=?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, cnp);
ResultSet result = ps.executeQuery();
You need to be mindful of the data type of the column when using either approach. If CNP
is a string (varchar) column, you should include the quotation marks.
"SELECT * FROM programari where CNP='" + cnp + "'"
Furthermore, you can use different set...()
methods depending on the actual type if you use the second method.
Upvotes: 1