my dog learned polymorphism*
The moose likes JDBC and the fly likes Incrementing primary key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Incrementing primary key" Watch "Incrementing primary key" New topic
Author

Incrementing primary key

Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
i have a primary key pid in a class called project...

I have a struts action class called createproject action where i set the pid for the project...

so I wrote a query



and i want to go through result set and increment it likewise as:



it's not going into while loop...

When i run the query it works fine

what should be the change??
Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
Pradeep Adibatla wrote: while(rs.next())


This doesn't make any sense as your SQL query will yield a single result row...


OCJP 6 (93%)
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
As Lorand said, there is only one record returned every time you run your query. When the if statement is executed, you visit the first row, then the while statement is executed, but since there are no more rows to visit, it will always yield false.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3697
    
    5

Are you aware rs.next() isn't a read-only call? Anytime you use it, you move the pointer in the result set. In order to hit the while loop in your code, you'd have to have at least 2 records from the result.


My Blog: Down Home Country Coding with Scott Selikoff
 
wood burning stoves
 
subject: Incrementing primary key
 
Similar Threads
ResultSet Problem
I have a problem (help need in SQL and ResultSet)
Struts2MySQL
Record Locking in multi-user
Hashtable Hell