将ResultSet转换为2DArray / HashMap /列表列表

时间:2014-04-21 12:57:00

标签: java mysql jdbc arraylist resultset

我想我已成为代码盲。

我目前正和朋友做一个小项目,我们正在使用JDBC从mySQL数据库中进行选择。

我想要的是,在运行select语句之后,我得到了所有数据的某种“2D”列表。

我想我想要像这样回来的东西 - array[columnName][all of the data inside the selected column]


伪代码

        // Count all rows from a column 
        // Count the columns selected

        // Adds the column names to 'array'
        for(int i = 1; i <= columnCount; i++)
            columns.add(resultSetMeta.getColumnName(i));

        // Adds the results of the first column to the 'array'
        // How can I add the results from n columns?
        for(int i = 1; i <= rowCount; i++ ) {
            while (resultSet.next())
                rows.add(resultSet.getString(i));
        }

        columnsAndRows.put(columns, rows);

  1. 用于“复制”表格的最合适的数据类型是什么 - ArrayLists,Arrays或HashMaps?

  2. ResultSet转换为2D数据类型的最佳方法是什么?

  3. 在迭代ResultSet时,如何移至下一列?

1 个答案:

答案 0 :(得分:2)

您可以将hashmap用于键值对,其中键入您的结果集元数据和值作为结果集值。

HashMap<String, Object> p = new HashMap<String, Object>();
p.put("ResultSetkey1","ResultSetvalue1");
p.put("ResultSetkey2","ResultSetvalue2");

另外我想说使用ResultsetUtils ResultsetUtils

相关问题