I have the following snippet:
Connection connection = getConnection(schemaName);
Statement stmt = connection.createStatement();
String sql = "SELECT * FROM " + tableName;
ResultSet rs = stmt.executeQuery(sql);
now what I want to achieve is building a
Map<String , List<String>>
where key is the columnName and value is list of columnValues. Here is my code:
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
List<String> columnNames = new LinkedList<>();
for (int i = 1; i <= columnCount; i++) {
columnNames.add(rsmd.getColumnName(i));
}
Map<String, List<String>> columnNameToValuesMap = new HashMap<String, List<String>>();
for (String columnName : columnNames) {
List<String> values = new ArrayList<>();
try {
while (rs.next()) {
values.add(rs.getString(columnName));
}
} catch (SQLException e) {
e.printStackTrace();
}
columnNameToValuesMap.put(columnName, values);
}
The issue is that after the first columnName iteration, rs.next() is empty. So basically The result map contains the values for only the first column. How can I reuse the same resultSet for each iteration ? Why its empty after the first one ?
getColumnLabelinstead ofgetColumnName