Wednesday, 21 August 2013

ResultSet into generic List of Lists

ResultSet into generic List of Lists

I need to add a ResultSet to a list of lists. The string passed to the
method is an SQL select statement. The DB connection methods work
perfectly with all other methods in this class so that's not the problem
here. I know I can replace some of the ArrayList declarations with List
but I don't think that matters in this case.
public static ArrayList<ArrayList> selectStatement(String string) throws
SQLException {
ArrayList<ArrayList> listOfLists = null;
ArrayList list;
String[] record = null;
try {
rs = null;
dBConnectionOpen();
rs = st.executeQuery(string);
ResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
while (rs.next()) {
list = null;
record = new String[columns];
for (int i = 1; i < columns; i++) {
record[i - 1] = rs.getString(i);
}
list = new ArrayList(Arrays.asList(record));
listOfLists.add(list);
}
} catch (Exception e) {
} finally {
dBConnectionClose();
}
return listOfLists;
}
I have done this before, but for some reason just it won't work this time.
What am I missing here?

No comments:

Post a Comment