An example of generating a serial number in Java (using a relational database)

Source: Internet
Author: User

In actual development, there may be a need to generate a serial number according to certain rules (for example, according to the company code and date to generate 4-digit serial number) We can combine the company and date as a business code , the value of the business code and sequence stored in the database, Each time you need to generate a serial number of the company and date to generate the business code to the database inside to check, if there is a record, the record will be the corresponding sequence value returned, and then the value of the sequence of one, if according to the corresponding business code The smallest value corresponding to the serial number is returned when the corresponding record is not queried, and then the corresponding record is inserted in the database

This scenario uses a k,value like BerkeleyDB to implement the database is the best choice (following the example of using a traditional relational database to implement, the example of using BerkeleyDB will be given later)

ImportJava.math.BigDecimal;ImportOrg.quickbundle.project.RmProjectHelper;ImportOrg.quickbundle.project.common.vo.RmCommonVo;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;/*** Serial Number generator *@authorAdministrator **/ Public classserianumbergenerator {Logger Logger= Loggerfactory.getlogger (serianumbergenerator.class); Private Static intmax_value=9999; Private Static intMin_value = 1; /*** Take the next serial number according to the business identifier *@paramBusinesscode *@return     */     Public  synchronizedstring Getnextval (String businesscode) {intCurrentValue; intNextValue; Rmcommonvo Comonvo=NULL; Try{comonvo= Rmprojecthelper.getcommonserviceinstance (). Doqueryforobject ("Select Current_value from serial_number WHERE KEY = '" + Businesscode+ "'"); }Catch(Org.springframework.dao.EmptyResultDataAccessException ex) {Logger.error ("The result set is empty, Springjdbc throws an exception, does not affect program execution ..."); }                //The key does not exist (stored in)        if(Comonvo = =NULL|| Comonvo.size () = = 0) {CurrentValue=Min_value; NextValue= CurrentValue +1; Rmprojecthelper.getcommonserviceinstance (). DoUpdate ("INSERT into Serial_number (KEY, Current_value) VALUES ('" +businesscode+ "', '" +nextvalue+ "')"); }        //        Else{CurrentValue= ((BigDecimal) comonvo.get ("Current_value") . Intvalue (); //has reached the maximum value            if(max_value==CurrentValue) {NextValue=Min_value; }Else{NextValue= Currentvalue+1; } rmprojecthelper.getcommonserviceinstance (). DoUpdate ("Update serial_number set current_value= '" +nextvalue+ "' WHERE key= '" +businesscode+ "'"); } String Finalvalue= This. formatString (CurrentValue); returnFinalvalue; }         PublicSerianumbergenerator () {}/*** Convert numbers to strings (high 0 when less than 4 bits) *@paramInput *@return     */     PublicString formatString (intinput)        {String result; //when greater than 1000, convert directly to string return        if(Input > 1000) {result= input+ ""; }Else{//depending on the number of digits, the 0 of the different front complements            intLength = (input + ""). Length (); if(Length = = 1) {result= "000" +input; }Else if(Length ==2) {result= "00" +input; }Else{result= "0" +input; }        }        returnresult; }                    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.