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

Source: Internet
Author: User

Package Com.jiaoyiping.berkeleydb;import com.sleepycat.je.*; import Com.sleepycat.utilint.stringutils;import org.slf4j.logger;import Org.slf4j.loggerfactory;import java.io.UnsupportedEncodingException;/** * Created with IntelliJ idea. * User: KJ a flat * date:14-7-9 * Time: PM 1:27 * To change this template use File | Settings | File Templates. * 1. Direct deposit of key and value * 2. Stores the object (unused) of the combination of key and value*/ Public classserialnumbergenerator {Logger Logger= Loggerfactory.getlogger (serialnumbergenerator.class); Private Static intMax_value =9999; Private Static intMin_value =1; /** * For any request, always first take out the current value to return, and then add a value in the database * @param key Business PRIMARY KEY * @return Current value*/     Publicstring Getnextvalue (String key) {string CurrentValue= This. Readcurrentvaluefromdatabase (key);        String result; if(CurrentValue = =NULL||"". Equals (CurrentValue)) {Result= This. formatString (Min_value);  This. Instrtintodatabase (Key, (min_value+1)+"");        Berkeleydbutil.getdatabase (). Getenvironment (). sync (); }        Else{            intIntcurrentvalue =Integer.parseint (CurrentValue); Result= This. formatString (Intcurrentvalue); //Store Next ValueString NextValue =""; if(Intcurrentvalue = =max_value) {NextValue= min_value+""; }Else{NextValue= (intcurrentvalue+1) +""; }             This. Instrtintodatabase (Key,nextvalue);        Berkeleydbutil.getdatabase (). Getenvironment (). sync (); }        returnresult; }     Publicstring Readcurrentvaluefromdatabase (String key) {Databaseentry Thekey=NewDatabaseentry (Stringutils.toutf8 (key)); Databaseentry Thevalue=NewDatabaseentry (); TransactionConfig Txnconfig=NewTransactionConfig (); Txnconfig.setserializableisolationvoid (true); Database Database=berkeleydbutil.getdatabase (); //Transaction txn = Database.getenvironment (). BeginTransaction (null,txnconfig);Operationstatus status = Database.Get(NULL, Thekey,thevalue,lockmode.default); //the corresponding key does not exist        if(Status = =operationstatus.keyempty) {            return NULL; }        Else if(Status = =operationstatus.success) {            byte[] data =Thevalue.getdata ();            String result; Try{result=NewString (data,"UTF-8"); } Catch(unsupportedencodingexception e) {e.printstacktrace (); //To change body of catch statement use File | Settings | File Templates.result =""; }            returnresult; }        return NULL; }     Public voidinstrtintodatabase (String key,string value) {databaseentry Thekey=NewDatabaseentry (Stringutils.toutf8 (key)); Databaseentry Thevalue=Newdatabaseentry (Stringutils.toutf8 (value)); TransactionConfig Txnconfig=NewTransactionConfig (); Txnconfig.setserializableisolationvoid (true); Database Database=berkeleydbutil.getdatabase (); //Transaction txn = Database.getenvironment (). BeginTransaction (null,txnconfig);Operationstatus status = Database.put (NULL, Thekey,thevalue); if(Status = =operationstatus.success) {Logger.info ("saved successfully"); }//else if (status = = Operationstatus.keyexist) {//logger.info ("");//        }    }     PublicString formatString (intinput) {String result=""; if(Input > +) {result= input +""; }Else{            intLength = (input+""). Length (); if(Length = =1) {result=" the"+input; }            Else if(Length = =2) {result="xx"+input; }Else{result="0"+input; }        }        returnresult; }     Public Static voidMain (string[] args) {Serialnumbergenerator generator=NewSerialnumbergenerator (); String result= Generator.getnextvalue ("Jiao"); System. out. println ("======"+result+"======"); }}

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.