我想将两个表的内容合并到一个列表中。我是编程新手,我正在开展一个学校项目。我需要将两个表中的数据显示在应用程序的一个表中。当前代码有效但如果我在数据库中有两行,则应用程序表将显示两行,如果我有三行,它将显示九次,依此类推。我不想重复。我希望你能理解我的问题。
我的模型中有这个:
public static ObservableList<archivesModel> takeArchives () {
ObservableList<archivesModel> archives = FXCollections.observableArrayList();
try {
Base.connect();
PreparedStatement st= connection.prepareStatement("SELECT * FROM destination,rent");
ResultSet rs= st.executeQuery();
while (rs.next()) {
archivesModel z;
z = new archivesModel(
rs.getString(FIRST_DESTINATION),
rs.getString(LAST_DESTINATION),
rs.getInt(AMOUNT),
rs.getString(FIRST_DATE),
rs.getString(LAST_DATE));
archives .add(z);
}
Base.disconnected();
} catch (SQLException ex) {Logger.getLogger(archivesModel.class.getName()).log(Level.SEVERE, null, ex);
}
return archives;
}
在我的控制器中我有
public void initialize(URL url, ResourceBundle rb) {
ObservableList<archivesModel> data = archivesModel.takeArchives();
first_destination.setCellValueFactory(new PropertyValueFactory<archivesModel, String>("first_destination"));
last_destination.setCellValueFactory(new PropertyValueFactory<archivesModel, String>("last_destination"));
amount.setCellValueFactory(new PropertyValueFactory<archivesModel, Integer>("amount"));
first_date.setCellValueFactory(new PropertyValueFactory<archivesModel, String>("first_date"));
last_date.setCellValueFactory(new PropertyValueFactory<archivesModel, String>("last_date"));
archivesRentTbl.setItems(data);
}
答案 0 :(得分:2)
您还需要考虑这两个表之间的关系。 假设两个表都有密钥“rent_id”,您可以使用以下sql查询加入两个表:
SELECT * FROM destination a JOIN rent b ON a.rent_id = b.rent_id