pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
problem to get nextval of a sequence
6+ messages / 3 participants
[nested] [flat]

* problem to get nextval of a sequence
@ 2004-12-28 12:45  Daniel Schaedler <postgres-jdbc@daniel.schaedler.name>
  0 siblings, 2 replies; 6+ messages in thread

From: Daniel Schaedler @ 2004-12-28 12:45 UTC (permalink / raw)
  To: pgsql-jdbc

Hi List

I try to get the 'nextval' of a sequence:
***
   String query = "SELECT nextval('nodes_nodeid_seq')";
   pstmt = conn.prepareStatement(query);
   logger.debug("statement: " + pstmt.toString());
   ResultSet ress = pstmt.executeQuery();
   long newID = ress.getLong("nextval");  // <-- Exception
***

I get the following Exception:
***
org.postgresql.util.PSQLException: ResultSet not positioned properly, 
perhaps you need to call next.
	at 
org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkResultSet(AbstractJdbc2ResultSet.java:2380)
	at 
org.postgresql.jdbc2.AbstractJdbc2ResultSet.getString(AbstractJdbc2ResultSet.java:1821)
	at 
org.postgresql.jdbc2.AbstractJdbc2ResultSet.getFixedString(AbstractJdbc2ResultSet.java:2326)
	at 
org.postgresql.jdbc2.AbstractJdbc2ResultSet.getLong(AbstractJdbc2ResultSet.java:1935)
	at 
org.postgresql.jdbc2.AbstractJdbc2ResultSet.getLong(AbstractJdbc2ResultSet.java:2158)
	at org.daniel.pws.store.PostgresStore.newNode(PostgresStore.java:307)
	at org.daniel.pws.store.StoreTester.main(StoreTester.java:71)
2004-12-28 13:41:57,983 - not able to get the new nodeID
****

executing the query in PostgresSQL directly works, I get the correct 
return value.
The Query itself seems to be executet, as I get other exceptions if I 
misspell the columnname (e.g.).
I'm working with Postgres-jdbc: pg80b1-1.308.jdbc3.jar and JDK 1.5.0

someone an Idea?

wishing all a Happy new Year
Daniel

Attachments:

  smime.p7s (application/x-pkcs7-signature, 3.4K) download

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: problem to get nextval of a sequence
@ 2004-12-28 13:08  Dave Cramer <pg@fastcrypt.com>
  parent: Daniel Schaedler <postgres-jdbc@daniel.schaedler.name>
  1 sibling, 0 replies; 6+ messages in thread

From: Dave Cramer @ 2004-12-28 13:08 UTC (permalink / raw)
  To: Daniel Schaedler <postgres-jdbc@daniel.schaedler.name>; +Cc: pgsql-jdbc

Daniel,

You have to call ress.next() before you get the value.

Dave

Daniel Schaedler wrote:

> Hi List
>
> I try to get the 'nextval' of a sequence:
> ***
>   String query = "SELECT nextval('nodes_nodeid_seq')";
>   pstmt = conn.prepareStatement(query);
>   logger.debug("statement: " + pstmt.toString());
>   ResultSet ress = pstmt.executeQuery();
>   long newID = ress.getLong("nextval");  // <-- Exception
> ***
>
> I get the following Exception:
> ***
> org.postgresql.util.PSQLException: ResultSet not positioned properly, 
> perhaps you need to call next.
>     at 
> org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkResultSet(AbstractJdbc2ResultSet.java:2380) 
>
>     at 
> org.postgresql.jdbc2.AbstractJdbc2ResultSet.getString(AbstractJdbc2ResultSet.java:1821) 
>
>     at 
> org.postgresql.jdbc2.AbstractJdbc2ResultSet.getFixedString(AbstractJdbc2ResultSet.java:2326) 
>
>     at 
> org.postgresql.jdbc2.AbstractJdbc2ResultSet.getLong(AbstractJdbc2ResultSet.java:1935) 
>
>     at 
> org.postgresql.jdbc2.AbstractJdbc2ResultSet.getLong(AbstractJdbc2ResultSet.java:2158) 
>
>     at org.daniel.pws.store.PostgresStore.newNode(PostgresStore.java:307)
>     at org.daniel.pws.store.StoreTester.main(StoreTester.java:71)
> 2004-12-28 13:41:57,983 - not able to get the new nodeID
> ****
>
> executing the query in PostgresSQL directly works, I get the correct 
> return value.
> The Query itself seems to be executet, as I get other exceptions if I 
> misspell the columnname (e.g.).
> I'm working with Postgres-jdbc: pg80b1-1.308.jdbc3.jar and JDK 1.5.0
>
> someone an Idea?
>
> wishing all a Happy new Year
> Daniel


-- 
Dave Cramer
http://www.postgresintl.com
519 939 0336
ICQ#14675561




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: problem to get nextval of a sequence
@ 2004-12-28 13:19  Guillaume Cottenceau <gc@mnc.ch>
  parent: Daniel Schaedler <postgres-jdbc@daniel.schaedler.name>
  1 sibling, 1 reply; 6+ messages in thread

From: Guillaume Cottenceau @ 2004-12-28 13:19 UTC (permalink / raw)
  To: Daniel Schaedler <postgres-jdbc@daniel.schaedler.name>; +Cc: pgsql-jdbc

Daniel Schaedler <postgres-jdbc 'at' daniel.schaedler.name> writes:

> Hi List
> 
> I try to get the 'nextval' of a sequence:
> ***
>    String query = "SELECT nextval('nodes_nodeid_seq')";
>    pstmt = conn.prepareStatement(query);
>    logger.debug("statement: " + pstmt.toString());
>    ResultSet ress = pstmt.executeQuery();
>    long newID = ress.getLong("nextval");  // <-- Exception
> ***
> 
> I get the following Exception:
> ***
> org.postgresql.util.PSQLException: ResultSet not positioned properly,
> perhaps you need to call next.

"perhaps you need to call next".

Have you tried calling next?

-- 
Guillaume Cottenceau



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: problem to get nextval of a sequence
@ 2004-12-28 13:32  Daniel Schaedler <postgres-jdbc@daniel.schaedler.name>
  parent: Guillaume Cottenceau <gc@mnc.ch>
  0 siblings, 1 reply; 6+ messages in thread

From: Daniel Schaedler @ 2004-12-28 13:32 UTC (permalink / raw)
  To: Guillaume Cottenceau <gc@mnc.ch>; +Cc: pgsql-jdbc

Guillaume Cottenceau wrote:
> Daniel Schaedler <postgres-jdbc 'at' daniel.schaedler.name> writes:
> 
> 
>>Hi List
>>
>>I try to get the 'nextval' of a sequence:
>>***
>>   String query = "SELECT nextval('nodes_nodeid_seq')";
>>   pstmt = conn.prepareStatement(query);
>>   logger.debug("statement: " + pstmt.toString());
>>   ResultSet ress = pstmt.executeQuery();
>>   long newID = ress.getLong("nextval");  // <-- Exception
>>***
>>
>>I get the following Exception:
>>***
>>org.postgresql.util.PSQLException: ResultSet not positioned properly,
>>perhaps you need to call next.
> 
> 
> "perhaps you need to call next".
> 
> Have you tried calling next?

uhhh....
....now yes...and it works.

When I expect only one set of data, I never call next. That's why I 
ignored the statement.

thanks anyway.
Daniel

Attachments:

  smime.p7s (application/x-pkcs7-signature, 3.4K) download

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: problem to get nextval of a sequence
@ 2004-12-28 14:05  Dave Cramer <pg@fastcrypt.com>
  parent: Daniel Schaedler <postgres-jdbc@daniel.schaedler.name>
  0 siblings, 1 reply; 6+ messages in thread

From: Dave Cramer @ 2004-12-28 14:05 UTC (permalink / raw)
  To: Daniel Schaedler <postgres-jdbc@daniel.schaedler.name>; +Cc: Guillaume Cottenceau <gc@mnc.ch>; pgsql-jdbc

Daniel,

The spec says that you must call next to position the cursor. What 
happens if nothing comes back? ie an empty result set ?

Dave

Daniel Schaedler wrote:

> Guillaume Cottenceau wrote:
>
>> Daniel Schaedler <postgres-jdbc 'at' daniel.schaedler.name> writes:
>>
>>
>>> Hi List
>>>
>>> I try to get the 'nextval' of a sequence:
>>> ***
>>>   String query = "SELECT nextval('nodes_nodeid_seq')";
>>>   pstmt = conn.prepareStatement(query);
>>>   logger.debug("statement: " + pstmt.toString());
>>>   ResultSet ress = pstmt.executeQuery();
>>>   long newID = ress.getLong("nextval");  // <-- Exception
>>> ***
>>>
>>> I get the following Exception:
>>> ***
>>> org.postgresql.util.PSQLException: ResultSet not positioned properly,
>>> perhaps you need to call next.
>>
>>
>>
>> "perhaps you need to call next".
>>
>> Have you tried calling next?
>
>
> uhhh....
> ....now yes...and it works.
>
> When I expect only one set of data, I never call next. That's why I 
> ignored the statement.
>
> thanks anyway.
> Daniel
>
>
>

-- 
Dave Cramer
http://www.postgresintl.com
519 939 0336
ICQ#14675561




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: problem to get nextval of a sequence
@ 2004-12-28 14:56  Guillaume Cottenceau <gc@mnc.ch>
  parent: Dave Cramer <pg@fastcrypt.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Guillaume Cottenceau @ 2004-12-28 14:56 UTC (permalink / raw)
  To: pg@fastcrypt.com; +Cc: Daniel Schaedler <postgres-jdbc@daniel.schaedler.name>; pgsql-jdbc

Dave Cramer <pg 'at' fastcrypt.com> writes:

> Daniel,
> 
> The spec says that you must call next to position the cursor. What
> happens if nothing comes back? ie an empty result set ?

With previous versions of postgres (and/or the jdbc driver?) it
was possible to get results with the cursor positioned before the
first row; it returned results from the first row. That's
probably the reason of the buggy code of Daniel.

-- 
Guillaume Cottenceau



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2004-12-28 14:56 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-12-28 12:45 problem to get nextval of a sequence Daniel Schaedler <postgres-jdbc@daniel.schaedler.name>
2004-12-28 13:08 ` Dave Cramer <pg@fastcrypt.com>
2004-12-28 13:19 ` Guillaume Cottenceau <gc@mnc.ch>
2004-12-28 13:32   ` Daniel Schaedler <postgres-jdbc@daniel.schaedler.name>
2004-12-28 14:05     ` Dave Cramer <pg@fastcrypt.com>
2004-12-28 14:56       ` Guillaume Cottenceau <gc@mnc.ch>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox