如何用java通过循环输入不同物料号得到新的流水号

如何用java通过循环输入不同物料号得到新的流水号

条码标签: P:A155V:11123;M:10884217-00/PCS;B:20090902VE;S:000000271974860; PO:5905618100/0040:Q:/10000/17/20:D:2019-10-18;YX:2020-09-07;
解析条码: M: 代表物料号10884217-00D: 代表生产时间2019-10-18 S:000000271974860
需求:产生生产批次格式为:生产日期六位(191018)+四位流水号一一1910180001
要求:相同物料生产日期为同一天的批次相同,不同物料生产日期相同的批次流水码累加+1

相同物料,同一天批次号,可以跳跃吗?
如果可以,在表里建个序列就可以了,值从1-9999循环。每次从序列里取一个值,在拼上当前日期,当做批次号。

如果 数据库是oracle,或db2,可以直接使用序列,如果是mysql,请自行百度一下,也有类似功能

  1. 建立序列:建立一个名为“tesSeq”的序列,每次增长1,从1开始,最小1,最大9999,超过9999回到1,缓存20个值

create sequence 
testSeq
increment by 1 
start with 1 
minvalue 1
maxvalue 9999
cycle
cache 20
  1. 已oracle数据库为例,代码仅供参考

package com.test.sequence;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

public class Test {

    private String jdbcUrl = "jdbc:oracle:thin:@127.0.0.1:1521:dbname";
    private String userName = "xxxx";
    private String password = "xxxxxxx";
    
    public static void main(String[] args) {
        Test t = new Test();
        String sequenceNO = t.getSequence("testSeq");
    }
    
    /**
     * 
     * @param seqName  序列名
     * @return
     */
    public String getSequence(String seqName){
        
        //获取数据库连接
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        //从seqName对应序列里取值
        String sql = "select "+seqName+".nextval from dual";
        
        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            rs=pstmt.executeQuery();
            //序列值
            int seqValue = rs.getInt(1);
            //补足4位流水号
            String seqNo = padding(seqValue);
            
            //当前6位日期
            String date = getDate();
            return date + seqNo;
        } catch (Exception e) {
            // TODO: handle exception
        }finally{
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            if(pstmt != null){
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    private String padding(int seqValue) {
        if(seqValue < 10){
            //一位数,补3个0
            return "000"+seqValue;
        }
        if(seqValue < 100){
            //两位数,补2个0
            return "00"+seqValue;
        }
        if(seqValue < 1000){
            //三位数,补1个0
            return "0"+seqValue;
        }
        //四位数不补0
        return seqValue+"";
    }

    /**
     * 返回当前日期 
     */
    private String getDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        
        return sdf.format(new Object());
    }

    private Connection getConnection() throws Exception{
        Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
        Connection conn = DriverManager.getConnection(jdbcUrl, userName, password);
        return conn;
    }
}