Thread: throw SQLException on no rs.next()?
Hi- it's me the JDBC newbie again and I have a simple request. Could a call to resultset.getX() throw some exception if rs.next() has not yet been called? I just got horribly burned by missing this simple thing and I think it may be helpful to other newbies to have this extra bit of information. Thanks. -- ><><><><><><><><><><><>< AgentM agentm@cmu.edu
I believe it throws a NullPointerException. AgentM wrote: > Hi- it's me the JDBC newbie again and I have a simple request. Could a > call to resultset.getX() throw some exception if rs.next() has not yet > been called? I just got horribly burned by missing this simple thing and > I think it may be helpful to other newbies to have this extra bit of > information. Thanks.
Yes, it sure does. The way I usually handle stepping through a ResultSet is with a structure like this: /* create REsultSet results */ while (results.next()) { /*do something with results*/ } You're right about this being a common newbie gotcha. I'll add it to the newbie FAQ. -NF > -----Original Message----- > From: pgsql-jdbc-owner@postgresql.org > [mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of AgentM > Sent: Tuesday, April 23, 2002 12:36 AM > To: pgsql-jdbc@postgresql.org > Subject: [JDBC] throw SQLException on no rs.next()? > > > Hi- it's me the JDBC newbie again and I have a simple request. Could > a call to resultset.getX() throw some exception if rs.next() has not > yet been called? I just got horribly burned by missing this simple > thing and I think it may be helpful to other newbies to have this > extra bit of information. Thanks. > -- > ><><><><><><><><><><><>< > AgentM > agentm@cmu.edu > > ---------------------------(end of broadcast)--------------------------- > TIP 6: Have you searched our list archives? > > http://archives.postgresql.org >
I thought I fixed it so that it doesn't throw an NPE, but I could be mistaken? Dave On Tue, 2002-04-23 at 08:09, Nick Fankhauser wrote: > Yes, it sure does. The way I usually handle stepping through a ResultSet is > with a structure like this: > > /* create REsultSet results */ > while (results.next()) > { > /*do something with results*/ > } > > You're right about this being a common newbie gotcha. I'll add it to the > newbie FAQ. > > -NF > > > > -----Original Message----- > > From: pgsql-jdbc-owner@postgresql.org > > [mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of AgentM > > Sent: Tuesday, April 23, 2002 12:36 AM > > To: pgsql-jdbc@postgresql.org > > Subject: [JDBC] throw SQLException on no rs.next()? > > > > > > Hi- it's me the JDBC newbie again and I have a simple request. Could > > a call to resultset.getX() throw some exception if rs.next() has not > > yet been called? I just got horribly burned by missing this simple > > thing and I think it may be helpful to other newbies to have this > > extra bit of information. Thanks. > > -- > > ><><><><><><><><><><><>< > > AgentM > > agentm@cmu.edu > > > > ---------------------------(end of broadcast)--------------------------- > > TIP 6: Have you searched our list archives? > > > > http://archives.postgresql.org > > > > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org > >
----- Original Message ----- From: "Dave Cramer" <Dave@micro-automation.net> > I thought I fixed it so that it doesn't throw an NPE, but I could be > mistaken? > which version of the driver? the original 7.2dev driver that comes with the rpms throws NPE best, Marin ---- "...what you brought from your past, is of no use in your present. When you must choose a new path, do not bring old experiences with you. Those who strike out afresh, but who attempt to retain a little of the old life, end up torn apart by their own memories. "
Dave- My response was based on my experience when I started (about a year ago) so this may be changed. However, at the time it looked to me like it *should* throw an NPE in this situation, so I'm suprised that it would be a candidate for change. -Nick > -----Original Message----- > From: Dave Cramer [mailto:Dave@micro-automation.net] > Sent: Tuesday, April 23, 2002 7:23 AM > To: nickf@ontko.com > Cc: AgentM; pgsql-jdbc@postgresql.org > Subject: Re: [JDBC] throw SQLException on no rs.next()? > > > I thought I fixed it so that it doesn't throw an NPE, but I could be > mistaken? > > Dave > On Tue, 2002-04-23 at 08:09, Nick Fankhauser wrote: > > Yes, it sure does. The way I usually handle stepping through a > ResultSet is > > with a structure like this: > > > > /* create REsultSet results */ > > while (results.next()) > > { > > /*do something with results*/ > > } > > > > You're right about this being a common newbie gotcha. I'll add it to the > > newbie FAQ. > > > > -NF > > > > > > > -----Original Message----- > > > From: pgsql-jdbc-owner@postgresql.org > > > [mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of AgentM > > > Sent: Tuesday, April 23, 2002 12:36 AM > > > To: pgsql-jdbc@postgresql.org > > > Subject: [JDBC] throw SQLException on no rs.next()? > > > > > > > > > Hi- it's me the JDBC newbie again and I have a simple request. Could > > > a call to resultset.getX() throw some exception if rs.next() has not > > > yet been called? I just got horribly burned by missing this simple > > > thing and I think it may be helpful to other newbies to have this > > > extra bit of information. Thanks. > > > -- > > > ><><><><><><><><><><><>< > > > AgentM > > > agentm@cmu.edu > > > > > > ---------------------------(end of > broadcast)--------------------------- > > > TIP 6: Have you searched our list archives? > > > > > > http://archives.postgresql.org > > > > > > > > > ---------------------------(end of broadcast)--------------------------- > > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org > > > > > > >
Nick, I think it should throw a SQLException, since that is what most ppl are catching, I don't really think any driver should ever NPE as a result of misuse. Dave On Tue, 2002-04-23 at 08:29, Nick Fankhauser wrote: > Dave- > > My response was based on my experience when I started (about a year ago) so > this may be changed. > > However, at the time it looked to me like it *should* throw an NPE in this > situation, so I'm suprised that it would be a candidate for change. > > -Nick > > > -----Original Message----- > > From: Dave Cramer [mailto:Dave@micro-automation.net] > > Sent: Tuesday, April 23, 2002 7:23 AM > > To: nickf@ontko.com > > Cc: AgentM; pgsql-jdbc@postgresql.org > > Subject: Re: [JDBC] throw SQLException on no rs.next()? > > > > > > I thought I fixed it so that it doesn't throw an NPE, but I could be > > mistaken? > > > > Dave > > On Tue, 2002-04-23 at 08:09, Nick Fankhauser wrote: > > > Yes, it sure does. The way I usually handle stepping through a > > ResultSet is > > > with a structure like this: > > > > > > /* create REsultSet results */ > > > while (results.next()) > > > { > > > /*do something with results*/ > > > } > > > > > > You're right about this being a common newbie gotcha. I'll add it to the > > > newbie FAQ. > > > > > > -NF > > > > > > > > > > -----Original Message----- > > > > From: pgsql-jdbc-owner@postgresql.org > > > > [mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of AgentM > > > > Sent: Tuesday, April 23, 2002 12:36 AM > > > > To: pgsql-jdbc@postgresql.org > > > > Subject: [JDBC] throw SQLException on no rs.next()? > > > > > > > > > > > > Hi- it's me the JDBC newbie again and I have a simple request. Could > > > > a call to resultset.getX() throw some exception if rs.next() has not > > > > yet been called? I just got horribly burned by missing this simple > > > > thing and I think it may be helpful to other newbies to have this > > > > extra bit of information. Thanks. > > > > -- > > > > ><><><><><><><><><><><>< > > > > AgentM > > > > agentm@cmu.edu > > > > > > > > ---------------------------(end of > > broadcast)--------------------------- > > > > TIP 6: Have you searched our list archives? > > > > > > > > http://archives.postgresql.org > > > > > > > > > > > > > ---------------------------(end of broadcast)--------------------------- > > > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org > > > > > > > > > > > > > > > ---------------------------(end of broadcast)--------------------------- > TIP 5: Have you checked our extensive FAQ? > > http://www.postgresql.org/users-lounge/docs/faq.html > >
Hmm... It looks like Sun's API specifies that it can only throw a SQLException, so I guess this is correct. It just seems a bit odd that a SQLException would occur when a database error hasn't occurred. Am I correct in thinking that the interaction with the database ends after the ResultSet is fetched, or does the .next() actually move a cursor through a structure that lives on the database side? (This is just a curiosity question, because I agree that if the spec says SQLException, that's what you have to do.) -Nick > -----Original Message----- > From: Dave Cramer [mailto:Dave@micro-automation.net] > Sent: Tuesday, April 23, 2002 7:41 AM > To: nickf@ontko.com > Cc: AgentM; pgsql-jdbc@postgresql.org > Subject: Re: [JDBC] throw SQLException on no rs.next()? > > > Nick, > > I think it should throw a SQLException, since that is what most ppl are > catching, I don't really think any driver should ever NPE as a result of > misuse. > > Dave > On Tue, 2002-04-23 at 08:29, Nick Fankhauser wrote: > > Dave- > > > > My response was based on my experience when I started (about a > year ago) so > > this may be changed. > > > > However, at the time it looked to me like it *should* throw an > NPE in this > > situation, so I'm suprised that it would be a candidate for change. > > > > -Nick > > > > > -----Original Message----- > > > From: Dave Cramer [mailto:Dave@micro-automation.net] > > > Sent: Tuesday, April 23, 2002 7:23 AM > > > To: nickf@ontko.com > > > Cc: AgentM; pgsql-jdbc@postgresql.org > > > Subject: Re: [JDBC] throw SQLException on no rs.next()? > > > > > > > > > I thought I fixed it so that it doesn't throw an NPE, but I could be > > > mistaken? > > > > > > Dave > > > On Tue, 2002-04-23 at 08:09, Nick Fankhauser wrote: > > > > Yes, it sure does. The way I usually handle stepping through a > > > ResultSet is > > > > with a structure like this: > > > > > > > > /* create REsultSet results */ > > > > while (results.next()) > > > > { > > > > /*do something with results*/ > > > > } > > > > > > > > You're right about this being a common newbie gotcha. I'll > add it to the > > > > newbie FAQ. > > > > > > > > -NF > > > > > > > > > > > > > -----Original Message----- > > > > > From: pgsql-jdbc-owner@postgresql.org > > > > > [mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of AgentM > > > > > Sent: Tuesday, April 23, 2002 12:36 AM > > > > > To: pgsql-jdbc@postgresql.org > > > > > Subject: [JDBC] throw SQLException on no rs.next()? > > > > > > > > > > > > > > > Hi- it's me the JDBC newbie again and I have a simple > request. Could > > > > > a call to resultset.getX() throw some exception if > rs.next() has not > > > > > yet been called? I just got horribly burned by missing this simple > > > > > thing and I think it may be helpful to other newbies to have this > > > > > extra bit of information. Thanks. > > > > > -- > > > > > ><><><><><><><><><><><>< > > > > > AgentM > > > > > agentm@cmu.edu > > > > > > > > > > ---------------------------(end of > > > broadcast)--------------------------- > > > > > TIP 6: Have you searched our list archives? > > > > > > > > > > http://archives.postgresql.org > > > > > > > > > > > > > > > > > ---------------------------(end of > broadcast)--------------------------- > > > > TIP 1: subscribe and unsubscribe commands go to > majordomo@postgresql.org > > > > > > > > > > > > > > > > > > > > > > > ---------------------------(end of broadcast)--------------------------- > > TIP 5: Have you checked our extensive FAQ? > > > > http://www.postgresql.org/users-lounge/docs/faq.html > > > > > >
Nick, I have to take Sun's side on this one. From my POV the programmer is expecting to get a SQLException so that is what should be thrown. NPE's can be thrown anywhere, which can be somewhat difficult to handle. To answer your question though... at this point interaction with the database is finished once the result set is fetched, but that may change and should change in the future to handle cursors And I checked; in the current code it throws a SQLException Dave On Tue, 2002-04-23 at 08:53, Nick Fankhauser wrote: > Hmm... > > It looks like Sun's API specifies that it can only throw a SQLException, so > I guess this is correct. > > It just seems a bit odd that a SQLException would occur when a database > error hasn't occurred. Am I correct in thinking that the interaction with > the database ends after the ResultSet is fetched, or does the .next() > actually move a cursor through a structure that lives on the database side? > > (This is just a curiosity question, because I agree that if the spec says > SQLException, that's what you have to do.) > > -Nick > > > > -----Original Message----- > > From: Dave Cramer [mailto:Dave@micro-automation.net] > > Sent: Tuesday, April 23, 2002 7:41 AM > > To: nickf@ontko.com > > Cc: AgentM; pgsql-jdbc@postgresql.org > > Subject: Re: [JDBC] throw SQLException on no rs.next()? > > > > > > Nick, > > > > I think it should throw a SQLException, since that is what most ppl are > > catching, I don't really think any driver should ever NPE as a result of > > misuse. > > > > Dave > > On Tue, 2002-04-23 at 08:29, Nick Fankhauser wrote: > > > Dave- > > > > > > My response was based on my experience when I started (about a > > year ago) so > > > this may be changed. > > > > > > However, at the time it looked to me like it *should* throw an > > NPE in this > > > situation, so I'm suprised that it would be a candidate for change. > > > > > > -Nick > > > > > > > -----Original Message----- > > > > From: Dave Cramer [mailto:Dave@micro-automation.net] > > > > Sent: Tuesday, April 23, 2002 7:23 AM > > > > To: nickf@ontko.com > > > > Cc: AgentM; pgsql-jdbc@postgresql.org > > > > Subject: Re: [JDBC] throw SQLException on no rs.next()? > > > > > > > > > > > > I thought I fixed it so that it doesn't throw an NPE, but I could be > > > > mistaken? > > > > > > > > Dave > > > > On Tue, 2002-04-23 at 08:09, Nick Fankhauser wrote: > > > > > Yes, it sure does. The way I usually handle stepping through a > > > > ResultSet is > > > > > with a structure like this: > > > > > > > > > > /* create REsultSet results */ > > > > > while (results.next()) > > > > > { > > > > > /*do something with results*/ > > > > > } > > > > > > > > > > You're right about this being a common newbie gotcha. I'll > > add it to the > > > > > newbie FAQ. > > > > > > > > > > -NF > > > > > > > > > > > > > > > > -----Original Message----- > > > > > > From: pgsql-jdbc-owner@postgresql.org > > > > > > [mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of AgentM > > > > > > Sent: Tuesday, April 23, 2002 12:36 AM > > > > > > To: pgsql-jdbc@postgresql.org > > > > > > Subject: [JDBC] throw SQLException on no rs.next()? > > > > > > > > > > > > > > > > > > Hi- it's me the JDBC newbie again and I have a simple > > request. Could > > > > > > a call to resultset.getX() throw some exception if > > rs.next() has not > > > > > > yet been called? I just got horribly burned by missing this simple > > > > > > thing and I think it may be helpful to other newbies to have this > > > > > > extra bit of information. Thanks. > > > > > > -- > > > > > > ><><><><><><><><><><><>< > > > > > > AgentM > > > > > > agentm@cmu.edu > > > > > > > > > > > > ---------------------------(end of > > > > broadcast)--------------------------- > > > > > > TIP 6: Have you searched our list archives? > > > > > > > > > > > > http://archives.postgresql.org > > > > > > > > > > > > > > > > > > > > > ---------------------------(end of > > broadcast)--------------------------- > > > > > TIP 1: subscribe and unsubscribe commands go to > > majordomo@postgresql.org > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > ---------------------------(end of broadcast)--------------------------- > > > TIP 5: Have you checked our extensive FAQ? > > > > > > http://www.postgresql.org/users-lounge/docs/faq.html > > > > > > > > > > > >
Dave Cramer wrote: > I thought I fixed it so that it doesn't throw an NPE, but I could be > mistaken? > > Dave You may have it's been a long time since I accidentally forgot to call next(). > On Tue, 2002-04-23 at 08:09, Nick Fankhauser wrote: > >>Yes, it sure does. The way I usually handle stepping through a ResultSet is >>with a structure like this: >> >>/* create REsultSet results */ >>while (results.next()) >>{ >> /*do something with results*/ >>} >> >>You're right about this being a common newbie gotcha. I'll add it to the >>newbie FAQ. >> >>-NF >> >> >> >>>-----Original Message----- >>>From: pgsql-jdbc-owner@postgresql.org >>>[mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of AgentM >>>Sent: Tuesday, April 23, 2002 12:36 AM >>>To: pgsql-jdbc@postgresql.org >>>Subject: [JDBC] throw SQLException on no rs.next()? >>> >>> >>>Hi- it's me the JDBC newbie again and I have a simple request. Could >>>a call to resultset.getX() throw some exception if rs.next() has not >>>yet been called? I just got horribly burned by missing this simple >>>thing and I think it may be helpful to other newbies to have this >>>extra bit of information. Thanks. >>>-- >>> >>>><><><><><><><><><><><>< >>> >>>AgentM >>>agentm@cmu.edu >>> >>>---------------------------(end of broadcast)--------------------------- >>>TIP 6: Have you searched our list archives? >>> >>>http://archives.postgresql.org >>> >> >> >>---------------------------(end of broadcast)--------------------------- >>TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org >> >> > > > > > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster >