Ollie
Ollie

Reputation: 1134

Turning a ResultSet into 2DArray/HashMap/Lists of Lists

I think I've become code-blind.

I'm currently doing a small project with a friend and we're using JDBC to select from a mySQL database.

What I want is that after running the select statement, I get some sort of '2D' list of all the data back.

I think I want something returned like this - array[columnName][all of the data inside the selected column]


Pseudo Code

        // 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. What is the most appropriate data type to use to 'replicate' a table - ArrayLists, Arrays, or HashMaps?

  2. What's the best way of making a ResultSet into a 2D datatype?

  3. When iterating through a ResultSet, how can I move to the next column?

Upvotes: 1

Views: 654

Answers (1)

Sanjay Rabari
Sanjay Rabari

Reputation: 2081

you can use hashmap for key value pairs, where as key you put your resultset metadata and values as resultset values.

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

Also I would like to say use ResultsetUtils ResultsetUtils

Upvotes: 2

Related Questions