[英]Fetching Next and previous data on button click
我是Java的新手,我想分別單擊下一個和上一個按鈕的按鈕來獲取數據庫的下一個和上一行。
private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {
st = con.createStatement();
for(i=1;i<=5;++i){
rs = st.executeQuery("select ID,EName from attendence where ID="+(i));
while(rs.next()) {
t1.setText(rs.getString("ID"));
}
}
我不明白如何迭代和獲取下一行,並在上一個按鈕上單擊它需要獲取上一行。 我認為我需要保持計數器來存儲當前位置。 但是該怎么做。請幫助如何在“下一個”和“上一個”按鈕上進行編碼。當第一行出現時,上一個應該無法使用。 在此先感謝您。
您可以一次獲取所有員工記錄,並將它們作為java對象存儲在列表中,然后可以維護所顯示員工的當前索引,並使用當前索引向前和向后移動。
注意:您可以分塊讀取記錄,而不是一次讀取所有記錄,以提高性能
希望這可以幫助。
public class Navigator {
private List<Employee> employees = null;
private currentIndex = 0;
public Navigator(){
populate employee list here using data base resultset
}
public Employee getCurrent() {
return employees.get(currentIndex):null;
}
public Employee getNext() {
return employees.get(++currentIndex);
}
public Employee getPrev(){
return employees.get(--currentIndex);
}
}
然后從您執行的按鈕操作中調用此類的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.