简体   繁体   中英

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 :

"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. 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 :

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 )

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. 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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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