Convert Java resultset to String array

littleK picture littleK · Nov 6, 2009 · Viewed 49.2k times · Source

I am writing a program that will query an MS access database, return the query as a result set, and then I want to ultimately convert that result set into a String array, so that I can pass it into the constructor of a Swing JComboBox - so the ComboBox will list the items returned by the query.

I have been able to store the rows of the result set into an ArrayList, and then convert that ArrayList into an object array, and the combobox will list the correct items, but as objects. I simply cannot ever cast that ArrayList to a String array. Does anyone know if this is possible? Here is some of my code...

// Convert the Resultset into an array list

public ArrayList<ArrayList<Object>> Results2Array(ResultSet rs) throws SQLException {
    ResultSetMetaData metaData = rs.getMetaData();
    int columns = metaData.getColumnCount();

    ArrayList<ArrayList<Object>> al = new ArrayList<ArrayList<Object>>();

    while (rs.next()) {
        ArrayList<Object> record = new ArrayList<Object>();

        for (int i = 1; i <= columns; i++) {
            Object value = rs.getObject(i);
            record.add(value);
        }
        al.add(record);
    }
    return al;
}

// Convert ArrayList to Object Array, and pass into GUI

ArrayList<String> Locations = new ArrayList<String>();
ArrayList<String> Months = new ArrayList<String>();
ArrayList<String> Years = new ArrayList<String>();

try {
    DB.loadDriver();
    DB.makeConnection();
    DB.buildStatement();

    Locations = DB.getLocations();
    Months = DB.getMonths();
    Years = DB.getYears();

    Object[] arrLocations = Locations.toArray();
    Object[] arrMonths = Months.toArray();
    Object[] arrYears = Years.toArray();

    dbGUI ui = new dbGUI(arrLocations, arrMonths, arrYears);
    ui.setVisible(true);

Can anyone offer any suggestions? Thanks!


UPDATE:

Here is the stack trace that I am receiving:

java.lang.ArrayStoreException
    at java.lang.System.arraycopy(Native Method)
    at java.util.Arrays.copyOf(Unknown Source)
    at java.util.ArrayList.toArray(Unknown Source)
    at kidsfirstdb.Main.main(Main.java:23)

Answer

Yishai picture Yishai · Nov 6, 2009
   String[] arrLocations = locations.toArray(new String[0]);

Is the correct answer. The reason for your exception is that all the objects are in fact not strings.

You need to change this:

   Object value = rs.getObject(i);

to this:

   String value = rs.getString(i);

or this:

   String value = rs.getObject(i).toString();

That last one will need a null check if you can be returning null columns.

Note that the toString() representation may not be exactly what you are looking for in all cases, but it will get you started.

Edit: If you are filling a combo box, you are going to need one column per row, no? If not, you need to represent the whole row as a string in some fashion. Then you put that in the value and put the value directly in the final array list, so your loop needs to look like this:

    ArrayList<String> al = new ArrayList<String>();
    while (rs.next()) {
            ArrayList<String> record = new ArrayList<String>();
            for (int i = 1; i <= columns; i++) {
                    String value = rs.String(i);
                    record.add(value);
            }
            String value = methodWhichConvertsArrayListToStringTheWayYouNeedItFormatted(record);
            al.add(value);
    }        
    return al;