• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to automatically generate a sequence of numbers

 
Ranch Hand
Posts: 55
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
i have created a jsp page displaying a form that have a field Booking ID, that should be an unique field(PK) in the BookingDetails table.
Now i want to generate a number that get displayed in the Booking ID text automatically and increase by 1 on every time the page is called, i.e., first time the Booking Id will be say 001, then 2nd time 002 and so on.....
I have written a class for that (which is not working)..

it is displaying "B00-9999" each time.
how could i get the number starting from 0001 and increment it on every page call. I don't have any idea.
Some one help please..
 
Rituparna Duttagupta
Ranch Hand
Posts: 55
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, it should have been on the JSP forum, i put it here by mistake. I am extremely sorry for the inconvenience caused due to this mistake.
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each time your page is hit, the method is called and it generates the same number, so what you need is to always get a newly generated id. Better to say you probably need to get it from your underlying database. For e.g in oracle you can use sequence. Other databases might be having some different ways.

 
Ranch Hand
Posts: 188
Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rituparna,

why don't you use the database for this purpose, like :
 
Rituparna Duttagupta
Ranch Hand
Posts: 55
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i use sql, can it be done on sql? now what will happen when i insert the first booking detail in the table? the id gets populated by 1 prior to any insertion?
will it not produce an exception?
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
exception!!, but why?
 
debraj mallick
Ranch Hand
Posts: 188
Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rituparna,

I didn't understand what exception you are talking about.
If you have any exception then please give us the exceptions. So that we could help you.
 
Rituparna Duttagupta
Ranch Hand
Posts: 55
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Debraj,
No i am not getting any exception, i was just asking that.
I am trying to fetch the last ID from the database and increment it by 1. then store it in database as the next id.
Don't know whether it'll work, let's see.
ok, tell me, is it possible to fetch the last Booking id? I have set the Booking_ID field as hierarchyid type in sql database.


Now, if i auto increment the id field that means i dont have to insert that field from within a jsp, since it is automatically inserted, right? Or is it that i have to insert it manually?
 
debraj mallick
Ranch Hand
Posts: 188
Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Rituparna,

I am trying to fetch the last ID from the database and increment it by 1. then store it in database as the next id.
autoincrement (in primary key, please see my earlier post) means when a new record is inserted, it will automatically incremented by one (also you set the increment by your desire sequence).
so for your "Booking_ID" will automatically incremented you don't have to insert it manually.
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic