我怎样才能在数据库中计算这些变量?

时间:2010-01-28 04:31:10

标签: java loops count

Colud你能帮我解决这个问题吗?

我有4个变量

  1. XTSM介于0~200
  2. 之间
  3. XTS介于0~2之间
  4. XRX为0~3
  5. XHAN是0~7
  6. ZHTYPE就是其中之一:(1)TCHF_HLF(2)TCHSD(3)TCHFULL
  7. 他们与这种形状有关。

    XTSM->XTS->XRX->XHAN->ZHTYPE(Just Logical)

    这意味着:

    • 每个XTSM都有3个XTS(0-2)
    • 每个XTS都有4个XRX(0-3)
    • 每个XRX都有8个XCHAN(0-7)
    • 并且每行的类型为ZHTYPE

    我有一个包含14000行的DB文件;在这个文件中指定了这些变量, 例如,XTSM介于0~200之间,但通常小于200,可能例如指定为0~60但是从不大于200; 对XTS,XRX和XCHAN来说当然是......所以我们必须把所有变量都变成动态的;

    这是文件的一部分(例如UPDATED): http://www.4shared.com/file/210566155/c080d93a/db_online.html

    我想得到这个输出? (我希望显示XTSM和XTS输出,并且在背景中必须计算所有这些) 请帮帮我

    ----Type------------------Total---------------------Total of ZHTYPE-----------------
    XTSM:0/XTS:0               in this case is : 29     TCHF_HLF:28 TCHSD:1 TCHFULL:0
    XTSM:0/XTS:1               No. of found   
    XTSM:0/XTS:2               No. of found   
    XTSM:1/XTS:0               No. of found   
    XTSM:1/XTS:1               No. of found   
    XTSM:1/XTS:2               No. of found 
    

    我不知道,但我认为这可能是这个;不是吗? 这只是伪代码:

    for(i ; i< (Total of XTSM) ; i++){
            for(j ; j< (Total of XTS) ; j++){
                for(k ; k< (Total of XRX) ; k++){
                    for(l ; l< (Total of XCHAN) ; l++)
                        Print( Total of XTSM:x/XTS:y);
                }
            }
    
    }
    

    感谢您的帮助......

3 个答案:

答案 0 :(得分:3)

请参阅下面的解决方案。您提供的测试文件test1.txt似乎在文件开头有一些特殊的字符。

<强>解决方案

public class Test {

    public static void main(String[] args) throws IOException {
        Test test = new Test();
        test.execute();
    }

    private static String TYPE_XTSM = "XTSM";
    private static String TYPE_XTS = "XTS";
    private static String TYPE_XRX = "XRX";
    private static String TYPE_XHAN = "XHAN";

    private void execute() throws IOException {
        InputStream in = null;
        BufferedReader br = null;
        TreeMap<Integer, TreeMap<Integer, Integer>> xtsmMap = new TreeMap<Integer, TreeMap<Integer, Integer>>();
        try {
            in = Test.class.getResourceAsStream("test1.txt");
            br = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = br.readLine()) != null) {
                Record rec = new Record(line);
                processRecord(xtsmMap, rec);
            }
        } finally {
            if (br != null) {
                br.close();
            }
        }
        printResults(xtsmMap);
    }

    private void processRecord(
            TreeMap<Integer, TreeMap<Integer, Integer>> xtsmMap, Record rec) {
        TreeMap<Integer, Integer> xtsMap;
        if (xtsmMap.containsKey(rec.getXtsm())) {
            xtsMap = xtsmMap.get(rec.getXtsm());
        } else {
            xtsMap = new TreeMap<Integer, Integer>();
            xtsmMap.put(Integer.valueOf(rec.getXtsm()), xtsMap);
        }
        if (xtsMap.containsKey(rec.getXts())) {
            Integer count = xtsMap.get(rec.getXts());
            xtsMap.put(Integer.valueOf(rec.getXts()), Integer.valueOf(count
                    .intValue() + 1));
        } else {
            xtsMap.put(Integer.valueOf(rec.getXts()), Integer.valueOf(1));
        }
    }

    private void printResults(
            TreeMap<Integer, TreeMap<Integer, Integer>> xtsmMap) {
        System.out.println("Type\t\tTotal");
        Set<Integer> xtsmSet = xtsmMap.navigableKeySet();
        for (Integer xtsm : xtsmSet) {
            TreeMap<Integer, Integer> xtsMap = xtsmMap.get(xtsm);
            Set<Integer> xtsSet = xtsMap.navigableKeySet();
            for (Integer xts : xtsSet) {
                Integer count = xtsMap.get(xts);
                String outputLine = TYPE_XTSM + ":" + xtsm + "/" + TYPE_XTS
                        + ":" + xts + "\t" + count;
                System.out.println(outputLine);
            }
        }
    }

    private static class Record {

        private Integer xtsm, xts, xrk, xhan;

        Record(String line) {
            StringTokenizer st = new StringTokenizer(line, "/");
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                String type = token.substring(0, token.indexOf(":"));
                String valueStr = token.substring(token.indexOf(":") + 1, token
                        .length());
                Integer value = Integer.valueOf(valueStr);
                if (TYPE_XTSM.equals(type)) {
                    xtsm = value;
                } else if (TYPE_XTS.equals(type)) {
                    xts = value;
                } else if (TYPE_XRX.equals(type)) {
                    xrk = value;
                } else if (TYPE_XHAN.equals(type)) {
                    xhan = value;
                }
            }
        }

        public Integer getXtsm() {
            return xtsm;
        }

        public Integer getXts() {
            return xts;
        }

        public Integer getXrk() {
            return xrk;
        }

        public Integer getXhan() {
            return xhan;
        }
    }
}

<强>输出

Type        Total
XTSM:0/XTS:0    29
XTSM:0/XTS:1    29
XTSM:0/XTS:2    29
XTSM:1/XTS:0    29
XTSM:1/XTS:1    29
XTSM:1/XTS:2    29
XTSM:2/XTS:0    29
XTSM:2/XTS:1    29
XTSM:2/XTS:2    29
XTSM:3/XTS:0    14
XTSM:3/XTS:1    14
XTSM:3/XTS:2    14
XTSM:4/XTS:0    13
XTSM:4/XTS:1    13
XTSM:4/XTS:2    13
XTSM:5/XTS:0    14
XTSM:5/XTS:1    14
XTSM:5/XTS:2    14
XTSM:6/XTS:0    21
XTSM:6/XTS:1    21
XTSM:6/XTS:2    21
XTSM:7/XTS:0    29
XTSM:7/XTS:1    29
XTSM:7/XTS:2    29
XTSM:8/XTS:0    14
XTSM:8/XTS:1    21
XTSM:9/XTS:0    21
XTSM:9/XTS:1    21
XTSM:9/XTS:2    21
XTSM:10/XTS:0   14
XTSM:10/XTS:1   14
XTSM:10/XTS:2   14
XTSM:11/XTS:0   14
XTSM:11/XTS:1   14
XTSM:11/XTS:2   14
XTSM:12/XTS:0   14
XTSM:12/XTS:1   14
XTSM:12/XTS:2   14
XTSM:13/XTS:0   29
XTSM:13/XTS:1   29
XTSM:13/XTS:2   29
XTSM:14/XTS:0   29
XTSM:14/XTS:1   29
XTSM:15/XTS:0   29
XTSM:15/XTS:1   29
XTSM:15/XTS:2   29
XTSM:16/XTS:0   29
XTSM:16/XTS:1   29
XTSM:16/XTS:2   29
XTSM:17/XTS:0   29
XTSM:17/XTS:1   29
XTSM:17/XTS:2   29
XTSM:18/XTS:0   29
XTSM:18/XTS:1   29
XTSM:18/XTS:2   29
XTSM:19/XTS:0   29
XTSM:19/XTS:1   29
XTSM:19/XTS:2   29
XTSM:21/XTS:0   29
XTSM:21/XTS:1   29
XTSM:21/XTS:2   29
XTSM:22/XTS:0   29
XTSM:22/XTS:1   29
XTSM:22/XTS:2   29
XTSM:23/XTS:0   29
XTSM:23/XTS:1   29
XTSM:23/XTS:2   29
XTSM:24/XTS:0   29
XTSM:24/XTS:1   29
XTSM:24/XTS:2   29
XTSM:25/XTS:0   29
XTSM:25/XTS:1   29
XTSM:25/XTS:2   29
XTSM:26/XTS:0   14
XTSM:26/XTS:1   14
XTSM:26/XTS:2   14
XTSM:28/XTS:0   15
XTSM:28/XTS:1   15
XTSM:28/XTS:2   15
XTSM:29/XTS:0   13
XTSM:29/XTS:1   13
XTSM:29/XTS:2   13
XTSM:30/XTS:0   14
XTSM:30/XTS:1   14
XTSM:31/XTS:0   14
XTSM:31/XTS:1   13
XTSM:31/XTS:2   13
XTSM:32/XTS:0   13
XTSM:32/XTS:1   14
XTSM:32/XTS:2   13
XTSM:33/XTS:0   14
XTSM:33/XTS:1   14
XTSM:33/XTS:2   14
XTSM:34/XTS:0   14
XTSM:34/XTS:1   14
XTSM:34/XTS:2   14
XTSM:35/XTS:0   29
XTSM:35/XTS:1   29
XTSM:35/XTS:2   29
XTSM:36/XTS:0   29
XTSM:36/XTS:1   21
XTSM:36/XTS:2   21
XTSM:37/XTS:0   14
XTSM:37/XTS:1   14
XTSM:37/XTS:2   14
XTSM:38/XTS:0   14
XTSM:38/XTS:1   14
XTSM:38/XTS:2   14
XTSM:39/XTS:0   21
XTSM:39/XTS:1   21
XTSM:39/XTS:2   21
XTSM:40/XTS:0   29
XTSM:40/XTS:1   29
XTSM:40/XTS:2   7
XTSM:41/XTS:0   29
XTSM:41/XTS:1   29
XTSM:41/XTS:2   29

答案 1 :(得分:2)

好的,我想我还在努力理解这个问题。但我认为,当您访问数据库时,您可以保存XTSM,XTS,XRX和XHAN的总数。

或者只是计算XTSM为“?”的COUNT。 (在PreparedStatment中)。我明白,对于已经很大的数据库来说,这可能是很多费用。

无论如何,你也可以在java中执行此操作,因为在for的最后一个循环中,i == COUNT。 顺便说一句,您的伪代码需要在XTSM中定义XTS,因为如果不是它将遍历数据库中的所有XTS而不是所有与您的特定XTSM相关的XTS。

更多的想法,在你的四个(我一直想说:)),你可以添加一些ifs到计数,所以你应该知道你是否正在循环一个与XTSM有逻辑关系的XTS并且在循环结束时再次i = COUNT。

稍微多一点的信息将是非常好的,因为我认为我的问题不长。 你如何从数据库中检索数据? 你有任何课程存储吗?也许你可以用它来做一些计算方法。

我认为这是一个简单的类

public class Foo {     int XTSM;     int XTS;     int XRX;     int XHAN; }

然后将所有行存储在List(或向量或地图,可迭代的东西)中,计算列表中具有XTSM = x和XTS = y的元素 这会或多或少地影响你想做的事吗?

编辑:也许这段代码有帮助,不知道它是不是你要找的......我想也许有些PL(如果你在ORACLE中)可以更快地完成它。无论如何,一些jave代码......

public class Foo {
int XTSM; 
int XTS; 
int XRX; 
int XHAN;
LinkedList lk;//maybe not a good idea to have it here
//but hey, it's a 30seconds thought...

public void getItAll() throws Exception{//mostly SQLException, but any you need
    Connection conn=null;
    //make the connection

    String query="SELECT XTSM, XTS, XRX, XHAN " +
            " FROM your_table" +
            " WHERE 1=1" +
            " AND 1=1";//add stuff you need to the query

    Statement stmt= conn.createStatement();
    ResultSet rst = stmt.executeQuery(query);
    Foo f = null;
    while(rst.next()){
        f = new Foo();
        //yeah, you should have getters and setters
        f.XTSM = rst.getInt("XSTM");
        f.XTS = rst.getInt("XST");
        f.XRX = rst.getInt("XRX");
        f.XHAN = rst.getInt("XHAN");
        this.lk.add(f);
        //this gives you all the table in a list (especifically a linkedlist
    }
}
public void calculateItAll() throws Exception{//some exception, probably IO?
    Foo f = null;
    int count =0;
    for(int i=0; i< this.lk.size(); i++){//we iterate through all the table
        f = new Foo();
        f = (Foo)this.lk.get(i);
        count = 0;
        for(int j=0; j<=200;j++){//XSTM
            if(f.XTSM == j){//I keep thinking that getters are good
                for(int k=0; k<=2;k++){//XST
                    if(f.XTS == k){//do the getters, really
                        for(int m=0;m<=3;m++){//XRX
                            if(f.XRX==m){//add funny comment about getters
                                for(int n=0;n<=7;n++){//XHAN
                                    if(f.XHAN==n){//man, so good that is almost finished
                                        count++;
                                    }//end of if
                                }//end of XHAN
                            }//end of if
                        }//end of XRX
                    }//end of if
                }//end ofXST
            }//end of if
        }//end of XSTM
        //here you write the count and all the Foo somewhere
    }
}}

答案 2 :(得分:1)

在Java中执行此操作并没有多大意义,因为您所展示的只是记录处理和最少的文本操作。正如Kaleb Brasee在评论中指出的那样,COBOL可能是合适的。但我不知道COBOL。

这是一个快速的Perl解决方案:

my $records = {};

for (<>) {
  next unless m{XTSM:(\d+)/XTS:(\d+)/XRX:(\d+)/XHAN:(\d+)};
  $$records{"XTSM:$1/XTS:$2"} ||= [];
  push @{$$records{"XTSM:$1/XTS:$2"}}, "$3/$4";
}

print "Type\t\t\tTotal\n";

for (sort keys %$records) {
  print "$_\t\t" . scalar @{$$records{$_}} . "\n";
} 

使用提供的文件输出示例:

Type                    Total
XTSM:0/XTS:0            29
XTSM:0/XTS:1            29
XTSM:0/XTS:2            29
XTSM:1/XTS:0            29
XTSM:1/XTS:1            29
XTSM:1/XTS:2            29
XTSM:10/XTS:0           14
XTSM:10/XTS:1           14