结果集返回3行,但我只能打印2行?

时间:2012-03-01 13:36:11

标签: java sql web-applications

下面的代码从我的数据库中获取我需要的信息,但不会打印出所有信息。首先我知道它从表中获取了所有正确的信息,因为我在sql开发人员中尝试了查询。

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)

即使它说有三行,它只打印了两行。有人可以看看上面是否有问题吗?

3 个答案:

答案 0 :(得分:2)

如果没有看到MenuList课程的代码,很难确定,但我认为您不需要循环ResultSet,而MenuList会为您做到这一点。

由于MenuList构造函数将ResultSet中的rs作为参数,它可能会循环遍历ResultSet以创建其条目。由于您已在循环的rs.next()中调用whileMenuList会错过第一个结果。

我认为你应该替换所有这些:

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

使用:

MenuList list = new MenuList(rs);

答案 1 :(得分:0)

我建议您使用调试器,以便了解您的程序正在做什么。

您似乎只保留最后一行,所以当您有3行时,您只保留最后一行。看来你从最后一行得到了两个值。

答案 2 :(得分: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) {
        }
    }

}