2012-03-01 47 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