user3645480
user3645480

Reputation: 13

java - arraylist iteration, doesnt print out first row

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.

Upvotes: 1

Views: 520

Answers (3)

Gavrilo Adamovic
Gavrilo Adamovic

Reputation: 2795

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()).

Upvotes: 0

Simulant
Simulant

Reputation: 20122

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.

Upvotes: 0

user3151902
user3151902

Reputation: 3176

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.

Upvotes: 1

Related Questions