为什么我得到 java.sql.SQLException: ResultSet not open。不允许操作 'next'。 java德比数据库?

Why do I get java.sql.SQLException: ResultSet not open. Operation 'next' not permitted. java derby database?

我收到这个错误:

java.sql.SQLException: ResultSet not open. Operation 'next' not permitted. Verify that autocommit is off.

当我尝试从数据库创建实例时。

当前代码:

         try
            {
            connection.setAutoCommit(false);
                stmt = connection.createStatement();
                results = stmt.executeQuery("SELECT * from animalTable");
                int AnimalCat = -1;
                System.out.print(connection.getAutoCommit());
                //getting error on line below
                while(results.next()) 
                {
                    int ID = results.getInt(1);
                    int Age = results.getInt(2);
                    String Name  = results.getString(3);
                    String AType = results.getString(4);
                    String Breed = results.getString(5);
                    AnimalCat = results.getInt(6);
                    int Adoption = results.getInt(7);
                    String Gender = results.getString(8);
                    String Description = results.getString(9);
                    if(Gender == "Male"){
                        gen = true;
                    }

                    animal = new Animal(Age, AType, gen, Breed, Description, Name);
                    animalList.add(animal);
                    if(AnimalCat != -1){
                        ResultSet resultCat = stmt.executeQuery("SELECT * from CategoryTable where ID = " + AnimalCat);
                       //without this line below i get a cursor error
                        resultCat.next();
                        System.out.println(resultCat.getInt(1) +"\n\n  " + resultCat.getString(2));
                        String Category = resultCat.getString(2);
                         if(Category == "Lost"){
                           Date input = resultCat.getDate(3);
                           LocalDate date = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                           ResultSet personData = stmt.executeQuery("SELECT * from PersonTable where ID = " + resultCat.getInt(4));
                           Person person = new Person(personData.getString(2), personData.getString(3), personData.getString(4), personData.getString(5));
                           Category lost = new Lost(date, resultCat.getString(5), person);
                           animal.setAnimalCat(lost);
                           personList.add(person);
                         }
                    }
                }
                results.close();
                stmt.close();  
            }
            catch (SQLException sqlExcept)
            {
                sqlExcept.printStackTrace();
            }

我试过像在异常中所说的那样关闭自动提交,还添加了一个 finally 块并关闭了语句。从我在网上看到的情况来看,解决了其他问题,但我的运气不好。

我知道 resultCat.next(); 是错误的原因,但没有它我得到了 "Invalid cursor state - no current row"

你有一个Statement,从语句中获得一个ResultSet,然后获得另一个ResultSet。这会自动 closes 第一个 ResultSet:

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.

因此,当您在第一个 ResultSet 上调用 next 时,会引发异常。 Javadoc 还告诉您要更改的内容:创建第二个语句并使用它来获取第二个 ResultSet。