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

Java 结果集返回3行,但我只能打印2行?,java,sql,web-applications,Java,Sql,Web Applications,下面的代码从我的数据库中获取我需要的信息,但并没有打印出所有信息。首先,我知道它正在从表中获取所有正确的信息,因为我已经在SQLDeveloper中尝试了该查询 public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = getConnection();

下面的代码从我的数据库中获取我需要的信息,但并没有打印出所有信息。首先,我知道它正在从表中获取所有正确的信息,因为我已经在SQLDeveloper中尝试了该查询

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行,但只保留最后一行。看起来您从最后一行得到了两个值。

如果没有看到
MenuList
类的代码,很难确定,但是我认为您不需要循环查看
ResultSet
,因为
MenuList
为您做了这件事

由于
MenuList
构造函数将
rs
中的
ResultSet
作为参数,它可能会在
ResultSet
上循环以创建其条目。正如您已经在循环的
中调用了
rs.next()
,而
菜单列表将错过第一个结果

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

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

publicstaticvoidmain(字符串[]args){
连接conn=null;
语句stmt=null;
结果集rs=null;
试一试{
conn=getConnection();
String query=“选择menu.menu\u id、menu\u title、dish.dish\u id、dish\u name、dish\u description、dish\u price、menu.week\u no”
+“从菜单、菜式、菜单分配”
+“其中menu.active='1'”
+“和menu.menu\u id=menu\u分配.menu\u id”
+“和dish.dish\u id=菜单\u分配.dish\u id”
+"及(09,10,11)周";;
stmt=conn.createStatement();
rs=stmt.executeQuery(查询);
MenuList[3]list=null;
int idx=0;//添加索引
while(rs.next()){
列表[idx]=新菜单列表(rs);//使用索引
idx++;//增量索引
System.out.println(rs.getRow());
}
对于(int pos=0;pos
如果您有更多信息要添加,正确的做法是编辑您的问题或添加评论。不要将某个不是答案的答案发布,因为查看您的问题的人可能看不到它。如果这与OP发布的代码相同,请删除。如果您“修复”了它,请描述修复,然后修复您的格式。
MenuList list = null;
while (rs.next()) {
    list = new MenuList(rs);
    System.out.println(rs.getRow());
}
MenuList list = new MenuList(rs);
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) {
        }
    }

}