【发布时间】:2014-06-05 13:10:15
【问题描述】:
我想我已经变成了代码盲。
我目前正在和朋友做一个小项目,我们正在使用 JDBC 从 mySQL 数据库中进行选择。
我想要的是,在运行 select 语句后,我会得到某种“二维”列表,其中包含所有数据。
我想我想要像这样返回的东西 -
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);
最适合用于“复制”表的数据类型是 ArrayLists、Arrays 还是 HashMaps?
将
ResultSet转换为二维数据类型的最佳方法是什么?- 在遍历
ResultSet时,如何移动到下一列?
【问题讨论】:
-
如果您想在每个列名下包含所有列数据,那么
Map<String, List<Object>>就可以了。这意味着您只能逐列循环。表格通常逐行工作。所以也许List<Map<String, Object>>会更合适。对于最有用的解决方案,我会使用GuavaTable。
标签: java mysql jdbc arraylist resultset