简体   繁体   English

如何从列表中分配数字-JDBC JAVA SQL

[英]How to get assign a number from a list - JDBC JAVA SQL

I am having trouble with my database where I want to select from a range of stock (not a primary key) and assign them to the users in the system. 我在数据库中遇到麻烦,我想从一系列库存中选择(不是主键)并将其分配给系统中的用户。

For example, I have a field called stockCodes which consists of codes like CAB001-CAB100. 例如,我有一个名为stockCodes的字段,其中包含诸如CAB001-CAB100之类的代码。 This is not the primary key, and the primary key is in fact stockID. 这不是主键,而主键实际上是stockID。

The table looks like this... 桌子看起来像这样...

ID | Stock Codes
--------------------
1  | CAB001-CAB100
2  | CBA100-CBA200

Now, how do I break down the stock codes so that I get a list like this in Java... 现在,我该如何分解股票代码,以便在Java中获得像这样的列表...

CAB001 CAB001

CAB002 CAB002

CAB003 CAB003

CAB004 CAB004

CAB005 CAB005

... ...

Please help. 请帮忙。

You would have to parse the text -- do they always follow the same format of 3 letters and 3 digits? 您将不得不解析文本-它们是否始终遵循相同的格式,即3个字母和3个数字? If so, something to this effect would work: 如果是这样,则可以执行以下操作:

/**
 * Populate a full list of stocks from a given range
 * 
 * @param stockRange the stock range pulled from the DB, in this format: "XXX###-XXX###"
 * @return list of all stocks in the specified range
 */
public List<String> getFullStockRange(final String stockRange) {
    final String[] values = stockRange.split("-"); 
    final Integer first = Integer.parseInt(values[0].substring(3));
    final Integer last = Integer.parseInt(values[1].substring(3));
    final String prefix = values[0].substring(0, 3);

    final List<String> list = new LinkedList<>();

    for (int i = first; i<= last; i++) {
        final String entry = String.format("%s%03d", prefix, i);
        list.add(entry);
    }

    return list;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM