我想将两个表的内容合并到一个列表中。我是编程新手,正在开展一个学校项目。我需要将两个表中的数据显示在应用程序的一个表中。当前代码有效,但如果我在数据库中有两行,应用程序表将显示两行两次,如果我有三行,它将显示九次,依此类推。我不想重复。希望您能理解我向您提出的问题。
我的模型中有这个:
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;
}
在我的 Controller 中我有
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);
}
最佳答案
您还需要考虑这两个表之间的关系。 假设两个表都有键“rent_id”,您可以使用以下 sql 查询连接两个表:
SELECT * FROM destination a JOIN rent b ON a.rent_id = b.rent_id
关于java - 将两个表连接到一个列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45782592/