本文介绍了为什么我得到java.sql.SQLException:ResultSet未打开.不允许执行“下一个"操作. java derby数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

当我试图从数据库创建实例时.

when I'm trying to create instances from a db.

当前代码:

         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块并关闭该语句.从我在网上看到的内容可以解决其他问题,但我的运气不好.

I have tried turning off auto commit like it says in the exception and also adding a finally block and closing the statement. From what I can see online that fixed others issues but no luck with mine.

我知道resultCat.next();在某种程度上位于错误的背后,但是如果没有错误,我会得到无效的光标状态-没有当前行"

I know the resultCat.next(); is behind the error somehow but I get an "Invalid cursor state - no current row" without it

推荐答案

您有一个Statement,从该语句获取一个ResultSet,然后获取另一个ResultSet.这会自动关闭第一个ResultSet:

You have a Statement, obtain a ResultSet from the statement, then obtain another ResultSet. This automatically closes the first ResultSet:

因此,当您在第一个ResultSet上调用next时,将引发异常. Javadoc还告诉您要更改的内容:创建第二条语句,并使用该语句获得第二个ResultSet.

So when you call next on the first ResultSet an exception is raised. The Javadoc also tells you what to change: Create a second statement and use that to obtain the second ResultSet.

这篇关于为什么我得到java.sql.SQLException:ResultSet未打开.不允许执行“下一个"操作. java derby数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 11:34