0

I have a FXML file, on which is ListView and next to it TextFields. How can I synchronize ID's from table on ListView with rest records in table?

For example when I click ID "1" on ListView then NAME and LASTNAME from my database (which are rest records with primary key ID "1") appears in TextField1 and TextField2)

@Override
public void initialize(URL location, ResourceBundle resources) {

db.getConnect();
con = db.getConnect();

try {


    pst1 = con.prepareStatement("SELECT idpatient FROM patient");
    rs1 = pst1.executeQuery();

    while(rs1.next())
    {
    ObservableList<String> ids = FXCollections.observableArrayList(rs1.getString("idpatient"));
    patientList.setItems(ids);
    }

This code displays only last row from column "idpatient", but I want to display whole column... how can I do it?

1 Answer 1

2

You are calling patientList.setItems(ids) for each row returned by the SELECT, so each row throws away the results from the previous row.

You need to accumulate the data from the rows in one list and then call patientList.setItems(ids) after the end of the while(rs1.next()) loop.

Perhaps something like:

List<String> results = new ArrayList<>();

while(rs1.next())
 {
   results.add(rs1.getString("idpatient"));
 }

ObservableList<String> ids = FXCollections.observableArrayList(results);
patientList.setItems(ids);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.