db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Knut Anders Hatlen <Knut.Hat...@Sun.COM>
Subject Re: Error: Invalid cursor state - no current row
Date Mon, 29 Oct 2007 11:46:10 GMT
Francesco Rosa <info@francescorosa.net> writes:

> Hello,
> i have the database with two tables Opzioni, Preventivi.
>
> This is my code;
>
> public class DerbySample {
>  public static void main(String args[]) {
>      // esempio collegamento a derby con modifiche
>      try {
>         Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
>         String connectionString =
> "jdbc:derby:/Users/harlock/Documents/soft/EsempiJava_15/dati;create=false";
>         Connection con = DriverManager.getConnection(connectionString,
> "", "");
>         String sql = "select * from preventivi where prvID = 1";
>         ResultSet rsPreventivi =
> con.createStatement(ResultSet.TYPE_FORWARD_ONLY,
> ResultSet.CONCUR_UPDATABLE).executeQuery(sql);
>         rsPreventivi.next();
>         sql = "select * from opzioni";
>         //----
>         ResultSet rsOpzioni =
> con.createStatement(ResultSet.TYPE_FORWARD_ONLY,
> ResultSet.CONCUR_READ_ONLY).executeQuery(sql);
>         while (rsOpzioni.next()) {
>            System.out.println(rsOpzioni.getString("opzUtente"));
>         }
>         rsOpzioni.close();
>         //----
>         rsPreventivi.updateString("prvUtente", "fran");
>         rsPreventivi.updateRow();
>         rsPreventivi.close();
>         System.out.println("bye bye");
>      } catch (Exception ex) {
>         System.out.println(ex.toString());
>      }
>
>   }
> }
>
> Run my sample program i receive the error: Invalid cursor state - no
> current row.
> If comment the line;
>         ResultSet rsOpzioni =
> con.createStatement(ResultSet.TYPE_FORWARD_ONLY,
> ResultSet.CONCUR_READ_ONLY).executeQuery(sql);
>         while (rsOpzioni.next()) {
>            System.out.println(rsOpzioni.getString("opzUtente"));
>         }
>         rsOpzioni.close();
>
> my program work correctly!!!
>
> Why? I don't understand!!!

Since you are running with auto-commit turned on, rsOpzioni.close() will
commit the open transaction, which means that all locks are released and
rsPreventivi will lose its position. You have a number of options:

  1) turn off auto-commit (con.setAutoCommit(false)) and perform commit
  explicitly (con.commit())

  2) reposition rsPreventivi after rsOpzioni has been closed

  3) perform rsPreventivi.updateString()+updateRow() before
  rsOpzioni.close()

HTH,
-- 
Knut Anders

Mime
View raw message