以下代碼從我的數據庫中獲取我需要的信息,但不會打印出所有信息。首先我知道它是從表中獲取所有正確的信息,因爲我已經在SQL開發人員中嘗試了查詢。結果集返回3行,但我只能打印2?
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
String query = "SELECT menu.menu_id, menu_title, dish.dish_id, dish_name, dish_description, dish_price, menu.week_no "
+ "FROM menu, dish, menu_allocation "
+ "WHERE menu.active = '1' "
+ "AND menu.menu_id = menu_allocation.menu_id "
+ "AND dish.dish_id = menu_allocation.dish_id "
+ "AND menu.week_no IN (09, 10, 11)";
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
MenuList list = null;
while (rs.next()) {
list = new MenuList(rs);
System.out.println(rs.getRow());
}
for (int pos = 0; pos < list.size(); pos++) {
Menu menu = list.getMenuAt(pos);
System.out.println(menu.getDescription());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
}
}
}
來自終端的輸出如下所示:
3 //Number of rows
Fish and Chips //3rd row
Chocolate Cake //2nd row
//Here should be 1st row
BUILD SUCCESSFUL (total time: 2 seconds)
即使它說有三排它只有印刷兩個。任何人都可以看到上述問題嗎?
如果你有更多的信息加入到做正確的事是編輯您的問題或添加評論。不要發佈一些答案,這不是一個答案,因爲看着你的問題的人可能看不到它。 – 2012-03-01 13:49:14