• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to automatically generate a sequence of numbers

 
Rituparna Duttagupta
Ranch Hand
Posts: 55
Firefox Browser Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
Firefox Browser Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Swastik Dey
Rancher
Posts: 1601
5
Android Java Java ME
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
debraj mallick
Ranch Hand
Posts: 188
Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rituparna,

why don't you use the database for this purpose, like :
 
Rituparna Duttagupta
Ranch Hand
Posts: 55
Firefox Browser Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 1601
5
Android Java Java ME
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
exception!!, but why?
 
debraj mallick
Ranch Hand
Posts: 188
Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
Firefox Browser Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic