Выскакивает ошибка Operation not allowed after ResultSet closed

столкнулся с такой проблемой: "Operation not allowed after ResultSet closed". Как вы видите, я проводил быстрые проверки, выводя в консоль метки о текущем состоянии программы:

checkpoint

false

we are in the loop

false

we are here

1231231231 потерял единичку своей еды, теперь у него 99 еды

Следуя из этого и репортам об ошибке это исключение выскакивает потому что после первого вызова result.next(), result закрывается.

connection = DriverManager.getConnection(URL, USER, PASSWORD);
statement = connection.createStatement();
try {
                while (true) {
                        System.out.println("checkpoint");
                        System.out.println(statement.isClosed());
                        System.out.println("we are in the loop");
                        statement.executeUpdate("update users set is_dead = 1 where food = 1");
                        ResultSet result = statement.executeQuery("select * from users where registration_time is not null && is_dead = 0");
                        System.out.println(result.isClosed());
                        while (result.next()) {
                            int food = result.getInt("food");
                            int id = result.getInt("id");
                            food -= 1;
                            System.out.println("we are here");
                            statement.executeUpdate("update users set food = '" + food + "' where id = " + id);
                            System.out.println(id + " потерял единичку своей еды, теперь у него " + food + " еды");
                        }

                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        System.out.println("Остановка не удалась");
                        e.printStackTrace();
                    }
                }
            } catch(SQLException e){
                    e.printStackTrace();
            }

Ответы (2 шт):

Автор решения: tim bars

Всё дело в том, что любое выполнение нового SQL через тот же объект Statment (т.е. любой statement.execute...()) закрывает предыдущий возвращённый ResultSet.

Цитата из документации по Statment:

By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists.

В качестве решения для statement.executeUpdate("update users set food = '" + food + "' where id = " + id); вы, например, можете использовать другой объект Statment

→ Ссылка
Автор решения: Ivan

Рабочий код, который делает все то же самое, но без ошибок теперь

while (true) {
                        System.out.println("checkpoint");
                        System.out.println(foodStatement.isClosed());
                        System.out.println("we are in the loop");
                        foodStatement.executeUpdate("update users set is_dead = 1 where food = 0");
                        foodStatement.executeUpdate("update users set food = food - 1 where registration_time is not null && is_dead = 0");
                        System.out.println("все получилось");
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        System.out.println("Остановка не удалась");
                        e.printStackTrace();
                    }
                }
→ Ссылка