jQuery in Action, 2nd edition*
The moose likes JSP and the fly likes how to automatically generate a sequence of numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to automatically generate a sequence of numbers " Watch "how to automatically generate a sequence of numbers " New topic
Author

how to automatically generate a sequence of numbers

Rituparna Duttagupta
Ranch Hand

Joined: Feb 08, 2011
Posts: 55

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..


With regards,
rituparna
Rituparna Duttagupta
Ranch Hand

Joined: Feb 08, 2011
Posts: 55

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

Joined: Jan 08, 2009
Posts: 1449
    
    6

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.


Swastik
debraj mallick
Ranch Hand

Joined: Mar 08, 2011
Posts: 188

Hi Rituparna,

why don't you use the database for this purpose, like :
Rituparna Duttagupta
Ranch Hand

Joined: Feb 08, 2011
Posts: 55

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

Joined: Jan 08, 2009
Posts: 1449
    
    6

exception!!, but why?
debraj mallick
Ranch Hand

Joined: Mar 08, 2011
Posts: 188

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

Joined: Feb 08, 2011
Posts: 55

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

Joined: Mar 08, 2011
Posts: 188

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to automatically generate a sequence of numbers
 
Similar Threads
how to split a number into two multipliyers
Can not find my error ? Help
who can get the simplest code?
Question about getting arraylist's size
Counting chars during file read.