Statement selectStatement = connection.createStatement();
        ResultSet selectResultSet = selectStatement.executeQuery(query);
        logger.info("sql query " + selectStatement.toString());
        ResultSetMetaData rsmd = selectResultSet.getMetaData();
        int columnsNumber = rsmd.getColumnCount();
        String[] row = new String[columnsNumber];

        int ctr = 0;
        logger.info("Reading from database...");
        while(selectResultSet.next())
        {
            for(int i=0;i<columnsNumber;i++){
                row[i] = selectResultSet.getString(i+1);
            }
            System.out.println();
            dataFromQuery.put(ctr++, row);
        }
        selectResultSet.close();


从HashMap读回时,它仅打印最后一行'n'次。

最佳答案

您缺少的是-while循环中的row = new String[columnsNumber];,这实际上会导致覆盖数组对象的值,并且您将同一对象添加到HashMap

...
while(selectResultSet.next()){
     row = new String[columnsNumber];
     for(int i=0;i<columnsNumber;i++){
         row[i] = selectResultSet.getString(i+1);
     }
     System.out.println();
     dataFromQuery.put(ctr++, row);
}
...

07-27 18:16