一个noctrl类的实现
?
?import java.sql.*;
?import com.liming.db.*;
?import com.liming.util.*;
?import java.text.*;
?
?/**
? * <p>title: 编号控制档</p>
? * <p>description: </p>
? * <p>copyright: copyright (c) 2005</p>
? * <p>company: liming network system</p>
? * @author yao kui
? * @version 1.0
? */
?
?public class noctrl {
? /**
? * 调试模式
? * 0=非调试模式;1=调试模式
? */
? public static int debugmode = 0;
?
? public static string curr_date = "curr_date";//编码规则,当前日期eg:20050501
? public noctrl() {
? }
? /**
? * @deprecated
? * @see currno
? * @param tablecode
? * @param columncode
? * @return
? */
? public static string currno(string tablecode,string columncode){
? connection con = null;
? statement stmt = null;
? dbmanager dbmgr = new dbmanager();
?
? try{
? string sqlstr =
? "select * from no_ctrl";
? string where = " where table_code='"+tablecode+"'";
? if(columncode != null) where += " and column_code='"+columncode+"'";
? sqlstr += where;
? apptools.printstack(sqlstr);
?
? if (debugmode==0) {
? con = dbmgr.getconnect(jndiname.jndioracle);
? }
? else {
? con = dbmgr.getconndirect();
? }
? stmt = con.createstatement();
?
? resultset rst = stmt.executequery(sqlstr);
? if(rst.next()){
? string prefix = coverfixstring(rst.getstring("prefix"));
? string postfix = coverfixstring(rst.getstring("postfix"));
? int currno = rst.getint("curr_no");
? int numlen = rst.getint("num_len");
? stmt.executeupdate("update no_ctrl set curr_no=curr_no+1"+where);
? return prefix+lpad(integer.tostring(currno),"0",numlen)+postfix;
? }else{
? return null;
? }
? }
? catch(sqlexception se) {
? apptools.printstack("操作编号控制档失败!");
? apptools.printstack(se);
? return null;
? }
? catch(exception e) {
? apptools.printstack("操作编号控制档失败!");
? apptools.printstack(e);
? return null;
? }
? finally
? {
? dbmgr.release(con,stmt);
? }
? }
? private static string coverfixstring(string fix){
? if(fix == null) return "";
? java.text.simpledateformat df = new simpledateformat("yyyymmdd");
? long date = system.currenttimemillis();
? string datestr = df.format(new date(date));
?
?
? /*if(curr_date.equals(fix)){
? java.text.simpledateformat df = new simpledateformat("yyyymmdd");
? long date = system.currenttimemillis();
? return df.format(new date(date));
? }*/
? return fix.replaceall(curr_date,datestr);
? }
? //将字符串左补串到一定长度
? private static string lpad(string src,string pad,int len){
? if(src.length() >= len) return src;
? int padlen = len - src.length();
? int padtimes = padlenpad.length() + 1;
? stringbuffer sb = new stringbuffer();
? for(int i=0;i<padtimes;i++){
? sb.append(pad);
? }
? return sb.tostring().substring(0,padlen)+src;
? }
? public static void main(string args){
? system.out.println(noctrl.lpad("15","0",5));
?
? system.out.println(noctrl.lpad("aaas15","0",5));
? system.out.println(noctrl.lpad("15","ab",5));
? system.out.println(noctrl.coverfixstring(null));
?
? system.out.println(noctrl.coverfixstring("asdff"));
? system.out.println(noctrl.currno("in_box","in_no"));
? }
?
? /*上面的currno方法对于业务逻辑后面报错的情况,会出现跳号的情况。对于要保证不跳号的业务则不可以。
? 下面是将取号与更新分开,延迟更新来保证不跳号的情况。首先业务取得的号(在出现新增页面则就要显示编号的情况)
? 只是表示“大致”的编号,在更新时一个事务中重新取号与更新编号。
? */
? public static string currno(string tablecode,string columncode){
? return currno(tablecode,columncode,false);
? }
?
? /**
? *更新序号,在业务新增数据中一起执行,为了保证多用户时也正确,不会重复。取得号后更新时加上原号
? * 作为条件,正如多用户更新加上版本(version)字段一样。
? * @param tablecode
? * @param columncode
? * @param currnum
? * @return
? */
? public static string updatenosql(string tablecode,string columncode,string currnum){
? return updatenosql(tablecode,columncode,currnum,1);
? }
?
? /*对于上面的取与更新在一个事务中进行。只取一个号,取时不用加锁。
? 对于如果后台要进行插入(多条),取回多个号,最好是连续时。如
? for(){
? insert(no,..);
? }
? 由于在一个事务中,所以no_ctrl没有更新,对自己或其它用户,取下一个号都不对。
? 所以最好使用加锁(for update)读出第一个号,最后的更新语句传导入使用的个数。
? var no = 加锁读出第一个号。
? for(i=0;i<xxx;i++){
? insert(no+i,...);
? }
? update(curr_no=curr_no+xxx)。
? */
? public static string currno(string tablecode,string columncode,boolean lock){
? connection con = null;
? statement stmt = null;
? dbmanager dbmgr = new dbmanager();
? string rt = new string2;
? try{
? string sqlstr =
? "select * from no_ctrl";
? string where = " where table_code='"+tablecode+"'";
? if(columncode != null) where += " and column_code='"+columncode+"'";
? sqlstr += where;
? if(lock) sqlstr += " for update"; //**lock
? apptools.printstack(sqlstr);
?
? if (debugmode==0) {
? con = dbmgr.getconnect(jndiname.jndioracle);
? }
? else {
? con = dbmgr.getconndirect();
? }
? stmt = con.createstatement();
?
? resultset rst = stmt.executequery(sqlstr);
? if(rst.next()){
? string prefix = coverfixstring(rst.getstring("prefix"));
? string postfix = coverfixstring(rst.getstring("postfix"));
? int currno = rst.getint("curr_no");
? int numlen = rst.getint("num_len");
? //stmt.executeupdate("update no_ctrl set curr_no=curr_no+1"+where);//延迟更新
? rt0 = prefix+lpad(integer.tostring(currno),"0",numlen)+postfix;
? rt1 = integer.tostring(currno);
? }
? return rt;
? }
? catch(sqlexception se) {
? apptools.printstack("操作编号控制档失败!");
? apptools.printstack(se);
? return null;
? }
? catch(exception e) {
? apptools.printstack("操作编号控制档失败!");
? apptools.printstack(e);
? return null;
? }
? finally
? {
? dbmgr.release(con,stmt);
? }
? }
? /**
? * 返回更新no_ctrl的语句
? * @param tablecod
闽公网安备 35060202000074号