2016-05-10 2 views
1

私はこのエラーを取得しています:なぜjava.sql.SQLExceptionが発生するのですか?ResultSetが開かれていません。操作 '次へ'は許可されません。 java derbyデータベース?

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();は何とかエラーの背後にある知っているが、私は「無効なカーソル状態 - 現在のロー」を取得する別のResultSetを取得、その後、声明からResultSetを取得し、あなたがStatementを持っていること

答えて

3

なし。これは、自動的に最初ResultSetcloses

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は何を変更するかを指示します:2番目のステートメントを作成し、2番目のステートメントを使用して2番目のResultSetを取得します。

関連する問題