I think my original email may have been somewhat vague, as the solutions
being presented are not for the problem i'm experiencing.  For this i
appologise.

ok, the problem is not with retrieving the results from the resultset, the
problem is with replacing the place holders in the query with other values,
so if i had (taking Chris' example):

String value;
ResultSet res;
PreparedStatement stm = con.prepareStatement("select value from table where
number=?");
for(int i = 0;i < 5;i++) {
  stm.setInt(1,i);
  res = stm.executeQuery();             //the error is occuring here, on
second itereation
  while(res.next()) {
    value = res.getString(1);
  }
}

This only happens with jdk1.2, 1.1 works fine.

Shah.
-----Original Message-----
From: Chris Pratt [mailto:[EMAIL PROTECTED]]
Sent: Friday, July 23, 1999 12:29 AM
To: [EMAIL PROTECTED]
Subject: Re: JDBC PreparedStatement


It sounds as though you expect the setString(1,"new value") call to actually
re-submit the statement.  That's not what they are for, they PREPARE the
statement which you can choose to execute or not.  If you need to get a
bunch of rows using a prepared statement, you can do something like:

String value;
ResultSet res;
PreparedStatement stm = con.prepareStatement("select value from table where
number=?");
for(int i = 0;i < 5;i++) {
  stm.setInt(1,i);
  res = stm.executeQuery();
  while(res.next()) {
    value = res.getString(1);
  }
}

    (*Chris*)

----- Original Message -----
From: Shaheen Hussain <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, July 13, 1999 6:34 AM
Subject: JDBC PreparedStatement


> Hey all,
>
> I have just noticed odd behaviour with prepared statements in JDK1.2.
>
> I have a preparedStatement which I create at the beginning of a function.
> However if I substitue the place holders for values, within a loop, I get
> the following error on second iteration:
>
> java.sql.SQLException: Invalid state for getResultSet
>
> However if I reinitialise the statement within the loop, that is, create a
> new object for each iteration, it works fine.
>
> Surely any new parameter values should clear the previous value, shouldn't
> it?
>
> Secondly, the function containing the above code is refusing to throw the
> error back to the calling function.  It contains a try/finally block.
>
> Shah.
>
> p.s. This code works fine with 1.1
>
>
___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to