我想我已经变成代码盲了。
我目前正在和一个 friend 一起做一个小项目,我们正在使用 JDBC 从 mySQL 数据库中进行选择。
我想要的是在运行 select 语句后,我得到某种返回所有数据的“2D”列表。
我想我想要像这样返回的东西 -
array[columnName][所选列内的所有数据]
伪代码
// 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
时,如何移动到下一列?
最佳答案
您可以将 hashmap 用于键值对,其中您将结果集元数据和值作为结果集值作为键。
HashMap<String, Object> p = new HashMap<String, Object>();
p.put("ResultSetkey1","ResultSetvalue1");
p.put("ResultSetkey2","ResultSetvalue2");
我还想说使用 ResultsetUtils ResultsetUtils
关于java - 将 ResultSet 转换为 2DArray/HashMap/Lists of Lists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23197923/