autonumber generation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mallz
    New Member
    • Mar 2007
    • 52

    #1

    autonumber generation

    hi,

    i need to automatically generate a sequential number for each record that i insert into the database from my jsp page. how do i do this? im using oracle as my database.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    By sequential, you mean the first will have the number 1, the second will have 2, the eighty-second will have 82, etc?

    How are you inserting the records? You can use a static variable initialized to 1 to start - this is the record's number. Every time you finish adding a record, increment the variable.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by Ganon11
      By sequential, you mean the first will have the number 1, the second will have 2, the eighty-second will have 82, etc?

      How are you inserting the records? You can use a static variable initialized to 1 to start - this is the record's number. Every time you finish adding a record, increment the variable.
      Write a utility method called generateRefNumb er. You can then put it in a servlet and call it everytime you create a record in the DB. Create a table in your database called Generated and use it to store the value of the last generated ref number. If you need to generate say, 5 sequential non-interleaving refNumbers then that bean would have 6 fields(the other for the key)
      Your generate ref would then look something like this


      Code:
       if(tableName.equalsIgnoreCase("Client")) { //generating for the client table
      	int previous = Integer.parseInt(genL.getClient());// If key is a String
      	generateTable.setClient(""+(previous + 1));
      	ref = "A";
      	if((previous + 1) < 10) {
      	 ref += "0";
      	}
      	ref = ref + "" + (previous + 1);
         }

      Comment

      • cwfent
        New Member
        • Sep 2007
        • 14

        #4
        I was wondering if anyone could help me out with this problem. I have a script that has a bunch of code in it. For some reason the coder decided to use a hex for a id number (it's just a member number, nothing secret) anyway the code section that I want to change is this :

        function createUniqueID( $table, $column)
        {
        $maxTries = 10;

        while(1)
        {
        if($maxTries <= 0)
        return false;

        $uniqueID = substr(md5(uniq id(rand(), true)), 0, 8);

        // check if this token does not exist in the table already
        $sql = "select $column from $table where $column="._q($u niqueID);
        $rs = (directory where code is located)::execu te($sql, __FILE__, __LINE__);
        if(!$rs)
        {
        showMsg(L_G_DBE RROR, 'error');
        return false;
        }

        if($rs->EOF)
        return $uniqueID;

        $maxTries--;
        }

        return false;
        }

        I just want a 5 digit sequential number. Can someone tell me how to change this. I realize that auto_increment would work in the db, but, there is so much code that interrelates in the script I don't believe that removing this string would work properly. Thanks for any help.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          1) This is not a Java related question, which I can tell from those $'s all over the place. This is the Java forum. Please find the proper forum to ask your question in.

          2) This is someone else's thread; please ask your questions in your own thread.

          Comment

          • cwfent
            New Member
            • Sep 2007
            • 14

            #6
            Originally posted by Ganon11
            1) This is not a Java related question, which I can tell from those $'s all over the place. This is the Java forum. Please find the proper forum to ask your question in.

            2) This is someone else's thread; please ask your questions in your own thread.

            Hi,

            I'm sorry I did a general forum search and found the thread about sequential number sequence. I hadn't noticed it was in a Java section and I thought I was allowed to ask a question about the same problem, which I thought it was. I am sorry for the inconvenience and have asked the question in the appropriate section with my own thread.

            Comment

            • kreagan
              New Member
              • Aug 2007
              • 153

              #7
              Originally posted by mallz
              hi,

              i need to automatically generate a sequential number for each record that i insert into the database from my jsp page. how do i do this? im using oracle as my database.
              Why don't you do this using your database? Databases have the ability to auto-increment a field for every row. This increment is autonomous and specific for each table.

              In java, you can just keep a static global veriable which increments every time you generate an new insert.

              Comment

              Working...