2

I've been working on a web-service that returns an arraylist. How can I add the returning arraylist to jtable and display?

    ArrayList customerDetails = new ArrayList();
    try {
        String sqlQuery = "SELECT * FROM customer WHERE AccountNumber="+accountNumber;
        PreparedStatement stmt = DatabaseConnection.dBconn().prepareStatement(sqlQuery);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {

            customerDetails.add(rs.getString("Name"));
            customerDetails.add(rs.getString("DoB"));
            customerDetails.add(rs.getString("Address"));
            customerDetails.add(rs.getString("Mobile"));
            customerDetails.add(rs.getString("Email"));
            customerDetails.add(rs.getString("AccountType"));
            customerDetails.add(rs.getString("AccountNumber"));
            customerDetails.add(rs.getString("SortCode"));
            customerDetails.add(rs.getString("Balance"));
            customerDetails.add(rs.getString("Card"));

        }
        return customerDetails;

    } catch (SQLException err) {
        System.out.println(err.getMessage());
    }
    return customerDetails;
Lakshan G
  • 366
  • 3
  • 9
  • Let's start with the fact that your `ArrayList` is not structured as a row/columns grouping, you will need a `List` within a `List`, where the outer list is the rows and the inner list are the column values – MadProgrammer Apr 22 '17 at 03:00
  • Your problem is add ArrayList to JTable, so try [this solution](http://stackoverflow.com/questions/26973577/create-jtable-with-arraylist) – TuyenNTA Apr 22 '17 at 03:08
  • `I've been working on a web-service that returns an arraylist.` - why does it return an ArrayList? If you are getting all the data from the database why not just return a `DefaultTableModel` containing all the data. Then you just create your `JTable` using the table model. – camickr Apr 22 '17 at 03:23

1 Answers1

1

Let's start with the fact that your ArrayList is not structured as a row/columns grouping, you will need a List within a List, where the outer list is the rows and the inner list are the column values

While we're at it, let's also make use of the PreparedStatement properly and manage the resources so they are closed properly while we're at it

ArrayList<List<String>> customerDetails = new ArrayList<>(25);
String sqlQuery = "SELECT * FROM customer WHERE AccountNumber=?";
try (PreparedStatement stmt = DatabaseConnection.dBconn().prepareStatement(sqlQuery)) {
    stmt.setString(1, accountNumber);
    try (ResultSet rs = stmt.executeQuery()) {

        while (rs.next()) {
            List<String> rowDetails = new ArrayList<>(10);
            rowDetails.add(rs.getString("Name"));
            rowDetails.add(rs.getString("DoB"));
            rowDetails.add(rs.getString("Address"));
            rowDetails.add(rs.getString("Mobile"));
            rowDetails.add(rs.getString("Email"));
            rowDetails.add(rs.getString("AccountType"));
            rowDetails.add(rs.getString("AccountNumber"));
            rowDetails.add(rs.getString("SortCode"));
            rowDetails.add(rs.getString("Balance"));
            rowDetails.add(rs.getString("Card"));

            customerDetails.add(rowDetails);
        }
    }

} catch (SQLException err) {
    System.out.println(err.getMessage());
}
return customerDetails;

Have a look at Using Prepared Statements and The try-with-resources Statement for more details

Now, we need a TableModel which can support it, at very basic level...

public class ListTableModel extends AbstractTableModel {

    private List<List<String>> rows;
    private List<String> columnNames;

    public ListTableModel(List<String> columnNames, List<List<String>> rows) {
        this.rows = new ArrayList<>(rows);
        this.columnNames = columnNames;
    }

    @Override
    public int getRowCount() {
        return rows.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.size();
    }

    @Override
    public String getColumnName(int column) {
        return columnNames.get(column);
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        Class type = String.class;
        return type;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        List<String> rowData = rows.get(rowIndex);
        return rowData.get(columnIndex);
    }
}

This takes a List for the column names and a List<List> for the row data.

Personally, I'd prefer to wrap the data into some kind of Plain Old Java Object (POJO) as it encapsulates the data and provides greater flexibility when displaying it (ie, I need to display all the properties of the object if I don't want to)

Take a look at How to Use Tables for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366