Wednesday, May 16, 2012

getting elements from mysql table to arraylist

i have an ArrayList that looks like this:



[
1 2011-05-10 1 22.0,
2 2011-05-10 2 5555.0,
3 2011-05-11 3 123.0,
4 2011-05-11 2 212.0,
5 2011-05-30 1 3000.0,
6 2011-05-30 1 30.0,
7 2011-06-06 1 307.0,
8 2011-06-06 1 307.0,
9 2011-06-06 1 307.0,
10 2011-06-08 2 3070.0,
11 2011-06-03 2 356.0,
12 2011-05-10 2 100.0,
13 2011-05-30 1 3500.0,
14 2011-05-10 3 1000.0,
15 2011-05-10 3 1000.0,
16 2011-05-07 1 5000.0,
17 2011-05-07 4 500.0,
18 2011-08-07 3 1500.0,
19 2011-08-08 6 11500.0,
20 2011-08-08 4 11500.0,
21 2011-08-08 7 11500.0,
22 2011-06-07 8 3000.0]


Here is the code how i got this arraylist:



@Override
public ArrayList<Expenses> getExpenses() {
ArrayList<Expenses> expenses = new ArrayList<Expenses>();
try {
Statement stmt = myConnection.createStatement();
ResultSet result = stmt.executeQuery("SELECT * FROM expenses");
while(result.next()){

Expenses expense = new Expenses();
expense.setNum(result.getInt(1));
expense.setPayment(result.getString(2));
expense.setReceiver(result.getInt(3));
expense.setValue(result.getDouble(4));

expenses.add(expense);

}
}
catch (SQLException e){
System.out.println(e.getMessage());
}
return expenses;
}


but what i want to get to an arraylist so that each element of array wasn't the line of the table (what i have now), but every individual element of the table should be the element of array ( [1, 2011-05-10, 1, 22.0, 2, 2011-05-10, 2, 5555.0, 3, 2011-05-11, 3, 123.0,]. Can anyone help me with that?





No comments:

Post a Comment