Auto-increment number (based on SSH)

Source: Internet
Author: User

Based on struts2 + hibernate + spring, database fields are automatically increased in the format of time + self-increasing data.

 

(1) query the database table. The maximum value of the attribute is Oracle10g)

 

/**
* The largest auto-increment number in the query (Format: Time + auto-increment)

* Used as an account card or other auto-increment operations (Format: Time + auto-increment)
* @ Param STR table Field

* @ Param tables Table Name

* @ Return refers to the largest number.
*/
Private string getmaxzhkbh (final string STR, final string tables ){
String maxcompute (stringcontactgethibernatetemplate(cmd.exe cute (New hibernatecallback (){
Public object doinhibernate (session) throws hibernateexception, sqlexception {
String hql = "select max (" + STR + ") from" + tables + "as w where substr (" + STR + ", 0,8) =" + timestamp. gettime ();
Query q = session. createquery (hql );
List all = Q. List ();
If (all! = NULL & all. Size ()> 0 ){
Return all. Get (0 );
}
Return NULL;
}

});
Return Max;
}

 

(2) The number of auto-increment operations is generally less than 001,002,003, that is, when the number of digits increases is not enough to add zero. Of course, you have defined the number of digits.

 

/**
* Zero-padding is not enough.
* @ Param a number
* @ Param LEN Length
* @ Return
*/
Public string format (int A, int Len ){
String temp = string. valueof ();
While (temp. Length () <Len ){
Temp = "0" + temp;
}
Return temp;
}

(3) The following are the methods to be called in the action. When the maximum value is queried for the first operation of the database, it does not exist. Therefore, you must specify a value 001. Later, you can query the maximum value, and then increase the maximum value by one.

/**
* Generate auto-increment number
* @ Param STR table Field
* @ Param tables Table Name
* @ Return the new number

*/
Public String gettimestamp (string STR, string tables ){
String max = getmaxzhkbh (STR, tables );
String stamp = NULL;
If (max = NULL ){
Stamp = timestamp. gettime () + "-" + "001 ";
} Else {
String sub = max. substring (max. lastindexof ("-") + 1 );
Int I = integer. parseint (sub) + 1;
String temp = This. Format (I, 3 );
Stamp = timestamp. gettime () + "-" + temp;

}
Return stamp;

}

(4) timestamp. gettime () is to get the time format content, the Code is as follows.

Public class timestamp extends hibernatedaosupport {
 
Public timestamp (){

}
 
/**
* Gets the time.
*
* @ Return the time
*/
Public static string gettime (){
String temp = NULL;
Simpledateformat SDF = new simpledateformat ("yyyymmdd ");
Temp = SDF. Format (new date ());
Return temp;
}
}

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.