如何在JavaFX

时间:2018-01-25 12:23:15

标签: mysql sql javafx fxml

我有一个测试数据库employees和一个表employee_db 我希望获得列表中的所有员工,我以某种方式管理的内容,列表显示employees。现在我需要的是,当我在该列表中选择员工时,他的name, age, address, salary显示在文本字段中

public class employeeController {

@FXML
private TextField name;
@FXML
private TextField age;
@FXML
private TextField address;
@FXML
private TextField salary;
@FXML
private TextField delete;
@FXML
private TextField selUp;
@FXML
private Button updateBtn;
@FXML
private ToggleButton eId;
@FXML
private ToggleButton eName;
@FXML
private ToggleButton select;
@FXML
private ToggleButton update;
@FXML
private ChoiceBox employeeList;
@FXML
private ChoiceBox updateList;
@FXML
private TextArea infoArea;
@FXML
private TextField newValue;

List<Employees> emp_collection;
ResultSet rs;
ObservableList<String> empCol = FXCollections.<String>observableArrayList("ID", "Name", "Age", "Address", "Salary");
Employees emp;

public employeeController() {

}


@FXML
private void update() throws SQLException {


  if(!update.isSelected()){
      updateList.setVisible(false);
      updateBtn.setVisible(false);

  }
  else if(update.isSelected()){
      updateList.setVisible(true);
      updateBtn.setVisible(true);
       try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/employees", "root", "ljubisa97");) {
        Employees emp = null;
        emp_collection = new ArrayList();
        Statement st = conn.createStatement();
        st.executeQuery("SELECT * FROM employees.employee_db where emp_id = 3;");
        ResultSet rs = st.getResultSet();
        while (rs.next()) {

            emp = new Employees(rs.getInt("emp_id"), rs.getString("name"), rs.getInt("age"), rs.getString("address"), rs.getInt("salary"));


            updateList.getItems().add(emp);

        }



    } catch (SQLException ex) {
        System.out.println("Error in database connection: \n" + ex.getMessage());
    }
}
  }










public class Employees {

private int id;
private String name;
private int age;
private String address;
private int salary;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public int getSalary() {
    return salary;
}

public void setSalary(int salary) {
    this.salary = salary;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public Employees() {

}

public Employees(String name, int age, String address, int salary) {
    this.name = name;
    this.age = age;
    this.address = address;
    this.salary = salary;
}

public Employees(int id, String name, int age, String address, int salary) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.address = address;
    this.salary = salary;
}

@Override
public String toString() {
    return "ID: " + getId() + " Name: " + getName() + " Age: " + getAge() + " Address: " + getAddress() + " Salary: " + getSalary() + "KM\n----------------------------------------------------------------------------------------------\n";
}

}

1 个答案:

答案 0 :(得分:0)

将监听器添加到value的<{1}}属性:

ChoiceBox