为什么我得到java.sql.SQLException:ResultSet未打开.不允许执行“下一个"操作. java derby数据库? [英] Why do I get java.sql.SQLException: ResultSet not open. Operation 'next' not permitted. java derby database?

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

问题描述

我收到此错误:

java.sql.SQLException:ResultSet未打开.操作下一个"不是 允许的.确认自动提交功能已关闭.

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

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

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:

默认情况下,每个Statement对象只能打开一个ResultSet对象 同时.因此,如果读取一个ResultSet对象是 与另一个的阅读交织在一起,每个必须已经生成 通过不同的Statement对象.语句中的所有执行方法 如果隐式关闭了一个语句的当前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.

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数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆