I'm searching for this problem since days and I can't seem to find a proper answer. So I have an ArrayList of Arrays in form of Objects (ArrayList) filled with the result set of a SQL string. My goal is to convert the result of only one row into a one-dimensional Array of Objects.
I have tried several times my research results from this website, but the only knowledge I've made was how to convert a one-dimensional ArrayList to a simple array. I hope you guys can help me in some ways. Thanks!
My table is looking like this:
sizeid | diameter
1 | 32
2 | 40
With the SQL-command "SELECT diameter FROM size" I'll get those two numbers 32 and 40, stored in a simple 2x1 table.
| 32 |
| 40 |
Now I want to have them in a one-dimensional Array.
the method to convert:
private Object[] getSizes() {
query = new SQLquery();
query.setSQLstring("SELECT diameter FROM size");
query.runQuery();
List<Object> sizeList = query.getRowData();
Object[] sizes = new Object[sizeList.size()];
// here I want to convert the List to an array..
return sizes;
}
the method to generate the ArrayList of the class SQLquery:
public void outputResult() {
try {
if (result != null) {
ResultSetMetaData meta = result.getMetaData();
columns = meta.getColumnCount();
columnNames = new Object[columns];
for (int i=1; i<=columnNames.length; i++) {
columnNames[i-1] = meta.getColumnName(i);
}
List<Object> rowData = new ArrayList<Object>();
while (result.next()) {
Object[] row = new Object[columns];
for (int j=0; j<= columns; j++) {
row[j-1] = result.getString(j);
}
rowData.add(row);
}
}
} catch (SQLexception e) {
System.out.println(e);
}
}
and the simple get-method of the class SQLquery:
public List<Object> getRowData() {
return rowData;
}
for (int j=0; j<= columns; j++) row[j-1] = result.getString(j);I believejshould start from 1.