Java while (rs.next()) 没有返回所有结果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19857527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:46:24  来源:igfitidea点击:

while (rs.next()) not returning all results?

javasqlresultset

提问by Steven

I'm retrieving multiple results from a table. If there are two records that are found in the table, only one is returned. If there are three results, only two are returned. I'm not sure why this is happening as I thought that the "while (rs.next())" method would iterate through each result. Any thoughts / comments appreciated. Thanks.

我正在从表中检索多个结果。如果在表中找到两条记录,则只返回一条记录。如果有三个结果,则只返回两个。我不确定为什么会发生这种情况,因为我认为“while (rs.next())”方法会遍历每个结果。任何想法/评论表示赞赏。谢谢。

package Test;

import java.sql.*;

public class DatabaseConnection {
//Create a GUI object
static GUI GUI = new GUI();
//Initialize String array
static String[] arr = new String[5];
//Connection string to Access Database
static String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/Users/StevenM/Desktop/TestProject.mdb";

public static void main(String[] args) throws ClassNotFoundException, SQLException{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}

static void SelectAllFromActor(String Query) throws SQLException, ClassNotFoundException{
    Connection conn = DriverManager.getConnection(database,"","");
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery(Query); 
    if(!rs.next()){
        GUI.NoResults();
    }
    else{
        GUI.SetActorModel();
        GUI.clearResults();
        while (rs.next()){
            arr[0] =(rs.getString("Title") +" ");
            arr[1] =(rs.getString("First_Name") +" ");
            arr[2] =(rs.getString("Last_Name") +" ");
            arr[3] =(rs.getString("Gender") +" ");
            arr[4] =(rs.getString("ID") +" ");
            GUI.AddToTable(arr,"Actor");
        }
    }
}   

采纳答案by Anthony Grist

You're calling rs.next()in the ifstatement, which retrieves the first record. Then in your while loop it calls rs.next()however many times, but you won't ever get the first record because it's already been retrieved.

您正在调用检索第一条记录rs.next()if语句。然后在您的 while 循环中,它会调用rs.next()很多次,但是您永远不会获得第一条记录,因为它已经被检索到了。

Rework it like this:

像这样重新工作:

boolean records = false;
while(rs.next()) {
    records = true;
    // code
}
if(!records)
    GUI.NoResults();

回答by LionC

You call rs.next6 lines above your while-statement:

rs.next在 -while语句上方调用6 行:

if(!rs.next()){
    GUI.NoResults();
}

This way you already "process" the first result, so you always have one result less than expected. Use first()instead:

通过这种方式,您已经“处理”了第一个结果,因此您总是得到一个比预期少的结果。使用first()来代替:

if(!rs.first()){
    GUI.NoResults();
}

回答by Orel Eraki

You should put rs.hasNext()at the condition above, and at the while-loopleave it as it is. When using next()the iterator moves to the next result, if you do it twice you lose a record.

你应该把rs.hasNext()上面的条件放在上面,然后保持while-loop原样。当使用next()迭代器移动到下一个结果时,如果你这样做两次,你就会丢失一条记录。