Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Result not expect as exected  RSS feed

 
tushar attar
Ranch Hand
Posts: 39
Chrome Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having issue with the output of code below:



I want when window is opened, product id (p_id) is fetched from database which denotes last id stored (autogenerated and primary key), +1 it and displays output.
ABove code always displays 1 when window is opened(as if while loop is not executed). Please let me know where is the error. Thanks
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does that statement give when run against the database directly?

The other thing you could do is stick some debugging logs in there to see where it gets to and what values come out.

What goes on inside that selectStatement method?
 
tushar attar
Ranch Hand
Posts: 39
Chrome Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply, I have found out error, Its a silly mistake from my side only, I haven't put any rows in database.

Also one situation is arising, I have created a table and selected a primary key field as autogenerated. Now the count has reached upto 35 and i have deleted all rows in table still if I add a new row, new id is 36 not 1. Is there any way that I can restart counting for that table?
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tushar attar wrote:I want when window is opened, product id (p_id) is fetched from database which denotes last id stored (autogenerated and primary key), +1 it and displays output.

If product id is auto generated then last id of product will be maximum among all previous Id stored then you can use MAX function in query to fetch max id, It will be the last product id stored in database.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use whatever tool comes with your db for auto generated ids.
They are not prone to clashes as would be the case with using MAX plus 1.

Is there a particular reason you have to have the ids starting at 1?
 
tushar attar
Ranch Hand
Posts: 39
Chrome Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using MySQL on workbench. Just for test purpose i have entered a few number of rows and now the count has increased, but when i am done with testing and development of sotware i want to restart all counters from 1, so that 1st product entered is of ID 1.

Hope i made sense.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't actually see the need.

In any case, have a look at the accepted answer on this SO thread:
http://stackoverflow.com/questions/8923114/how-to-reset-auto-increment-in-mysql
 
tushar attar
Ranch Hand
Posts: 39
Chrome Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much
 
Paul Clapham
Sheriff
Posts: 22482
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:I don't actually see the need.


Agreed. If you care about the values of autogenerated keys then you're doing something wrong. Quite often it's the "caring about" part which is being done wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!