ilovkatie ilovkatie - 2 months ago 38
Java Question

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
?

Answer

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++);
}