简体   繁体   English

JDBC-填充2D双数组的问题

[英]JDBC - Issue populating 2D Double Array

I am retrieving a result set from a table in a PostgreSQL database via JDBC connection which looks as follows : 我正在通过JDBC连接从PostgreSQL数据库中的一个表中检索一个结果集,如下所示:

"501.511346, 503.472045, 505.447591, 501.192024, 504.137371, 500.790352, 506.726137, 508.266889, 506.879129, 503.036632"
"506.233533, 504.731789, 507.639155, 507.187112, 501.124606, 508.558729,504.037900, 505.246124, 505.314278, 500.101784"
"509.075946, 505.219091, 505.953530, 501.323737, 509.691705, 500.110447, 505.315793, 503.922326, 505.259513, 507.176139"
"505.161837, 501.063333, 503.402369, 503.944111, 502.329003, 501.593900, 508.902708, 503.022083, 505.456198, 500.821379"
"501.136874, 507.282628, 501.076124, 504.313085, 502.092685, 504.601038, 508.905852, 507.602457, 502.363248, 501.228881"
"507.790364, 500.034138, 501.491094, 507.916393, 501.683380, 503.665745, 507.908477, 504.317156, 504.921323, 508.663319"
"501.013448, 501.443771, 500.449935, 501.570703, 501.112750, 507.936670, 506.626647, 504.746571, 501.478473, 505.641617"
"501.553313, 502.492668, 504.332290, 503.666099, 506.629541, 500.592711, 500.896930, 502.970319, 501.116483, 504.481986"
"502.187111, 502.145824, 509.532465, 500.348489, 503.842839, 508.749411, 500.718773, 508.422647, 508.683536, 505.489182"
"509.526615, 504.127884, 503.502820, 506.184630, 507.451289, 507.783623, 503.639020, 506.767338, 506.089402, 506.095711"
"505.050268, 504.379683, 508.883864, 508.821655, 506.592373, 507.762016, 503.664888, 507.020359, 502.166903, 504.881668"
"503.641401, 502.152418, 501.656423, 509.706045, 505.032023, 509.798445, 507.760368, 502.113529, 507.615546, 506.032260"
"506.855262, 508.673852, 503.902280, 507.339752, 509.106561, 503.817907, 504.675336, 502.857339, 505.761561, 505.328955"
"509.216366, 503.236089, 509.705565, 500.589984, 500.818828, 505.180683, 503.657360, 505.528898, 509.506768, 502.728268"
"507.932913, 505.611162, 500.728144, 502.882686, 500.979144, 506.991179, 500.943360, 506.693182, 508.148536, 508.699568"
"508.946498, 501.745562, 502.265949, 503.192731, 502.030673, 507.678642, 500.089286, 504.847314, 507.204066, 500.696839"
"507.151703, 505.898623, 508.330869, 504.758373, 503.307971, 500.713862, 506.714869, 503.989853, 508.461387, 507.387292"
"508.271230, 507.405013, 502.357225, 506.591855, 504.613121, 504.768269, 501.416726, 505.613160, 501.375562, 501.625350"
"504.183084, 507.494983, 505.040833, 505.075503, 504.161549, 500.649583, 507.194630, 503.392844, 505.336177, 508.217170"
"508.924550, 503.218437, 504.534036, 505.692131, 508.263250, 503.923185, 507.255442, 506.786683, 501.893640, 503.278230"
"506.812050, 500.469390, 506.258835, 500.542808, 502.774340, 506.703418, 504.855838, 508.988294, 504.825021, 503.711878"
"504.940812, 508.431433, 507.301498, 505.920257, 501.246557, 501.256791, 500.599958, 502.064444, 500.933950, 501.989907"
"500.374969, 504.333468, 501.590108, 504.684638, 501.577986, 507.462334, 503.769062, 501.015270, 506.213296, 504.483580"
"504.733616, 506.757830, 503.770166, 505.921616, 505.463712, 503.765199, 502.936621, 502.479458, 507.342924, 508.919453"
"509.786083, 504.510082, 501.687019, 509.471297, 503.188882, 507.377933, 508.997906, 509.912943, 504.786779, 503.013641"
"503.474216, 504.125196, 505.592858, 506.826637, 505.118236, 500.803693, 502.614498, 500.703702, 506.820342, 504.832302"
"500.387248, 504.569592, 502.562114, 504.589547, 508.552309, 509.541123, 500.961317, 501.182442, 508.204940, 505.628783"
"501.791401, 502.100932, 501.150022, 509.534479, 508.850065, 502.757766, 507.866282, 500.849425, 509.927472, 500.098215"
"504.735886, 509.656796, 500.461879, 500.453160, 508.434995, 506.671135, 500.992930, 505.550642, 507.093285, 503.311201"

Unfortunately each row in this result set is one long comma separated string. 不幸的是,该结果集中的每一行都是一个长逗号分隔的字符串。 This isn't the issue however, as I parse each row returned into an array of doubles in my java code. 但是,这不是问题,因为我将返回的每一行解析为Java代码中的double数组。 My ultimate goal is to create a 2D array of doubles from this result set. 我的最终目标是从该结果集中创建一个二维双精度数组。 Below is the method I have written to do this, as it stands currently: 以下是我目前编写的用于执行此操作的方法:

    public double[][] getMatrix(int x, int y) {

    int row = getRowDim(x, y); //separate method to get row dimension
    int col = getColDim(x, y); //separate method to get column dimension
    double[][] matrix = new double[row][col];   
    String[] intermediateArray  = new String[col];
    String px = "";
    try {
        conn = dataSource.getConnection();
    } catch (SQLException e1) {
        e1.printStackTrace();
    }
    try {
            PreparedStatement p = conn.prepareStatement(getMatrix); //getMatrix query called from SQL prop file
            p.setInt(1, x);
            p.setInt(2, y);
            ResultSet r = p.executeQuery();
            int count=0;
            while(r.next())
            {
                px = r.getString("prices");
                intermediateArray = px.split(",", -1);
            }
            parsed = new double[intermediateArray.length]; 
            for (int i = 0; i<intermediateArray.length; i++) {
                parsed[i] = Double.valueOf(intermediateArray[i]);
            }
                for(int i=0;i<row;i++){
                    for(int j=0;j<col;j++){
                        if(count==parsed.length) break;
                        matrix[i][j]=parsed[(count)];
                        count++;
                    }
                }
                conn.close();

        } catch (SQLException e) {
            logger.error("***** SQL EXCEPTION *****");
                 e.printStackTrace();
    }
    return matrix;
}

This does actually return a matrix with the correct dimension, however only the last row of my result set is being written to the first row of the resultant matrix, as such : 实际上,这确实会返回具有正确维度的矩阵,但是只有我的结果集的最后一行被写入结果矩阵的第一行,如下所示:

     504.74     509.66     500.46     500.45     508.43     506.67     500.99     505.55     507.09     503.31
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00
   0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00

with zero values being populated everywhere else. 零值填充到其他地方。 I'm quite sure I've mis positioned one of the loops somewhere, but for the life of me can't spot where. 我很确定我将循环之一定位在某个地方,但是对于我的一生来说,我找不到位置。 If anyone can point me in the right direction, i'd be quite grateful. 如果有人能指出我正确的方向,我将不胜感激。 If I've left out any necessary details, please ask, thanks. 如果我有任何必要的细节,请询问,谢谢。

If you are using Java 8 you can simply use : 如果您使用的是Java 8,则可以简单地使用:

String[] intermediateArray  = new String[col];
int index = 0;
while(r.next()){
    intermediateArray[i++] = r.getString("prices");
}
double[][] matrix = Arrays.stream(intermediateArray)
        .map(s -> Stream.of(s.split(",")).mapToDouble(Double::parseDouble).toArray())
        .toArray(double[][]::new);

The idea is : put all the results in an array or a List then use my piece of code to convert this array of String to a 2d array of double ( ideone demo ) 这个想法是:将所有结果放入数组或列表中,然后使用我的代码段将此String数组转换为double的2d数组( ideone demo

There are a few things that need to be changed in your code. 您的代码中需要更改一些内容。 You can't know for certain the number of rows to expect until you finish processing the result set (even if you're explicitly querying for specific rows, you'd rather make the code safe). 在完成结果集的处理之前,您无法确定预期的行数(即使您明确查询特定行,也希望使代码安全)。 Therefore you may need to use a flexible-length data structure. 因此,您可能需要使用灵活长度的数据结构。

Your problem though was that you were using the same variable and overwriting it in the while loop. 但是,您的问题是您使用了相同的变量并在while循环中将其覆盖。 Here's a slightly modified version of the same code: 这是相同代码的稍作修改的版本:

int row = getRowDim(x, y);
int col = getColDim(x, y);

double[][] matrix; //length of array cannot be guaranteed before reading the result set.

try {
    conn = dataSource.getConnection();
} catch (SQLException e1) {
    e1.printStackTrace();
}

List<double[]> matrixList = new ArrayList<>(); //rather use an array list to avoid unexpected index errors

try {
    PreparedStatement p = conn.prepareStatement(getMatrix);
    p.setInt(1, x);
    p.setInt(2, y);
    ResultSet r = p.executeQuery();

    while(r.next())
    {
        String px = r.getString("prices");
        String[] intermediateArray  = px.split(",", -1);

        double[] parsed = new double[intermediateArray.length];
        for (int i = 0; i < intermediateArray.length; i++) {

            //use Double.parseDouble(String) to avoid unnecessary boxing/unboxing
            parsed[i] = Double.parseDouble(intermediateArray[i]);
        }

        matrixList.add(parsed);
    }

    matrix = new double[matrixList.size()][];
    conn.close();

    int index = 0;
    for(double[] currentRow: matrixList) {
        matrix[index++] = currentRow;
    }

    return matrix;
} catch (SQLException e) {
    logger.error("***** SQL EXCEPTION *****");
    e.printStackTrace();
    return new double[0][0];
}

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

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