2012-03-01 43 views
0

以下代碼從我的數據庫中獲取我需要的信息,但不會打印出所有信息。首先我知道它是從表中獲取所有正確的信息,因爲我已經在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) 

即使它說有三排它只有印刷兩個。任何人都可以看到上述問題嗎?

+0

如果你有更多的信息加入到做正確的事是編輯您的問題或添加評論。不要發佈一些答案,這不是一個答案,因爲看着你的問題的人可能看不到它。 – 2012-03-01 13:49:14

回答

2

很難確定沒有看到MenuList類的代碼,但我不認爲你需要循環使用ResultSet,因爲MenuList會爲你做這件事。

由於MenuList構造函數將ResultSetrs,因爲它很可能環比ResultSet來創建其條目的參數。正如你已經在while中呼叫rs.next()那樣MenuList錯過了第一個結果。

我想你應該取代這一切:

MenuList list = null; 
while (rs.next()) { 
    list = new MenuList(rs); 
    System.out.println(rs.getRow()); 
} 

有了:

MenuList list = new MenuList(rs); 
0

我建議你使用一個調試器,以便了解你的程序正在做什麼。

您似乎只保留加載的最後一行,所以當您有3行時,您只保留最後一行。看起來你從最後一行獲得了兩個值。

0
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[3] list = null; 
     int idx = 0;         //Add index 
     while (rs.next()) {      
      list[idx] = new MenuList(rs);    //use index 
      idx++;          //increment index 
      System.out.println(rs.getRow()); 
     } 
     for (int pos = 0; pos < list.size(); pos++) { 
      Menu menu = list.getMenuAt(pos);//Don't know that 
                 //get menu by index 
      System.out.println(menu.getDescription()); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      rs.close(); 
      stmt.close(); 
      conn.close(); 
     } catch (SQLException e) { 
     } 
    } 

} 
+0

如果這與OP發佈的代碼相同,請刪除。如果你「修復」它,請[編輯],*描述修復*,然後*修復你的格式*。 – Will 2012-03-01 13:57:59