countryroadscat
countryroadscat

Reputation: 1750

Getting all records from database to Vaadin Table

I'm trying to load records from database table to Vaadin table.

I'm getting all records from table process like this:

public ResultSet selectRecordsFromDbUserTable() throws SQLException {

    Connection dbConnection = null;
    Statement statement = null;
    ResultSet rs = null;

    String selectTableSQL = "SELECT * from process";

    try {
        dbConnection = getDBConnection();
        statement = dbConnection.createStatement();
        System.out.println(selectTableSQL);
        // execute select SQL stetement
         rs = statement.executeQuery(selectTableSQL);

    } catch (SQLException e) {
        System.out.println(e.getMessage());
    } finally {
        if (statement != null) {
            statement.close();
        }
        if (dbConnection != null) {
            dbConnection.close();
        }
    }
    return rs;
}

And it is working well. In ResultSet rs I'm getting all rows which I need.

How to load them into Vaadin Table?

Upvotes: 1

Views: 990

Answers (1)

Akash Thakare
Akash Thakare

Reputation: 23012

First of all add container properties

table.addContainerProperty("Id", Long.class, 1L);
table.addContainerProperty("Name", String.class, "");
//......
//Add other columns for table which are container properties

Loop through ResultSet

int itemId=1;
while(rs.next()){
   table.addItem(new Object[]{rs.getLong("id"),rs.getString("name")},itemId++);
}

Upvotes: 2

Related Questions