JDBC how to get all records in a single object?

JDBC how to get all records in a single object?
let's say I'm taking all I need from the database like this (query is a variable with the SQL query)
ResultSet rs = st.executeQuery(query);
but to build up every time here's a design I don't want
while (rs.next()) {

because you never know how many times I need them.
you can pick them up for example in the form of the array of objects, or in any other more s-cook form ?
PS if not, please fikalii not to throw, because with java-oops just getting started
July 9th 19 at 11:27
2 answers
July 9th 19 at 11:29
Made myself this way:
private ArrayList<hashmap<string, object>> resultSetToArrayList(ResultSet rs) throws SQLException {
 ResultSetMetaData md = rs.getMetaData();
 int columns = md.getColumnCount();
 ArrayList<hashmap<string, object>> list = new ArrayList<>();
 while (rs.next()) {
 HashMap<string, object> row = new HashMap<>(columns);
 for (int i = 1; i <= columns; ++i) {
 row.put(md.getColumnName(i), rs.getObject(i));
 return list;

ArrayList<hashmap<string, object>> list = resultSetToArrayList(rs);
 for (HashMap<string, object> row : list) {
July 9th 19 at 11:31
Well, apparently it is necessary to make array and record all the data cycle, then anywhere use.
good idea, but as far as I know, you first need to reserve it for the memory, and how to reserve memory for something whose size you don't know? - loren_Grimes commented on July 9th 19 at 11:34

Find more questions by tags JavaJDBCMySQL