1

Excuse: To begin I want to tell you that im not an expert java programmer at all and do not know all the ins and outs of arrayLists and iteration. So far I've come a long way but I somehow do not really understand my problem because I've little experience with Lists in java.

Problem: I've tried to put my sql query results(ResultSet (I Think this is also a array list, so im probably using a double array list which is not needed.)) into a ArrayList, after that I've tried to print each row into my console but I'm having a problem where the first row does not get printed.

Solutions: I'm good as long as the following two things can happen: 1. Put the results in a normal array (So no ArrayList), This makes it easier for me because I know exactly how to handle arrays. 2. You guys can tell me why the first row does not get printed

My Database: I have two colums, the first one is the primary key (ID) and the second one is a command (String(Varchar) I want to execute once I get it.

My code:

public List<String> getCommands(){

    String sql = "SELECT * FROM query";
    try {
        PreparedStatement stmt = CONNECTION.prepareStatement(sql);
        ResultSet results = stmt.executeQuery();

        if (results.next()) {

            List<String> row = new ArrayList<String>();
            while(results.next()) {
                row.add(results.getString(2));
            }

            return row;

        } else {
            return null;
        }

    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;

}

List<String> results = SQL.getCommands();

if(results != null){
    getLogger().info("Found at least 1 command");

    for (Iterator<String> i = results.iterator(); i.hasNext();) {
        String row = i.next();
        getLogger().info("command: " + row);
    }

}else{
    getLogger().info("Can't find commands");
}

My output: It prints everything exept the first row in the database.

2
  • you are calling results.next() twice, so it skips the first row. You should remove the if clause. Commented Aug 12, 2017 at 19:38
  • It is not arraylist iteration. Commented Aug 12, 2017 at 19:58

3 Answers 3

1

Have a look at ResultSet::next, it says:

Moves the cursor froward one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.

As you do an if (results.next()) before the while loop, the first row of the result is skipped.

Sign up to request clarification or add additional context in comments.

Comments

0

you call results.next() twice before adding the result to your list. remove the if (results.next()) { statement and everything should be fine.

next() must be called once before accessing the first row, but you are calling it twice (in the if and in the while statement) so the cursor is already in the second row when you start reading the rows content.

4 Comments

This fixes the issue,Thanks a lot. do you have any idea how i can check if there are no rows at all to return a null?
it is better to be null save and return an empty list to prevent null checks and null pointer exceptions. But if you want your null result you can check your list.isEmpty() after adding the elements in the while loop. If the list is empty no results where returned and you can then return null instead of the list.
@user3645480 .next() returns true or false based on if there are more rows in resultSet or the current one is the last one.
Thanks! you totally fixed my issues. I feel ashamed that i forgot that calling the results.next() in an if() would process the results too.
0

When you check if (results.next()) results.next() is actually being called, so the pointer to rows is pointing to the first element of result set. And when you get to while(results.next()), next() is being called again and pointing to the second element, so you can delete if(resultset.next()).

1 Comment

Thanks, your answer is very helpful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.